Change "button" color by clicking on it or validating text fill

Asked

Viewed 5,015 times

3

Hello guys I’m having trouble getting the button to change color when the fields EditTexts are filled and return the initial color when the fields are empty and also change color when pressed.

Excerpt from code present in onCreate:

senha.addTextChangedListener(new TextWatcher(){
    @Override
    public void afterTextChanged(Editable s) {
        if(!validar(matricula.getText().toString(), senha.getText().toString())){
            //btn_Entrar.setBackgroundColor(Color.parseColor("#c9c9c9"));
            btn_Entrar.setClickable(false);
        }else if(!matricula.getText().toString().equals("")){
            btn_Entrar.setBackgroundColor(Color.parseColor("#2D89db"));
            btn_Entrar.setClickable(true);
        }
    }
    public void beforeTextChanged(CharSequence s, int start,int count, int after) {}
    public void onTextChanged(CharSequence s, int start,int before, int count) {}
});

Property in XML:

android:background="@drawable/shape_edit_text"

shape_edit_text:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/shape_buttonlogin_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/shape_buttonlogin_normal"/>
</selector>

shape_buttonlogin_pressed:

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#ff0000"/>
</shape>

shape_buttonlogin_normal:

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#c9c9c9" />
</shape>

1 answer

3

When you use the XML attribute background is giving a StateListDrawable as background of Button. Already when you use Button.setBackgroundColor is overwriting the background with a ColorDrawable and thereby loses state behavior to a fixed color, causing the problem.

Soon as solution using the State List Drawable which you have already set for your button, we can add the state state_enabled to achieve the objective.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Pressionado e Habilitado -->
    <item android:state_pressed="true" android:state_enabled="true">
        <shape>
            <solid android:color="#ff0000"/>
        </shape>
    </item>
    <!-- Habilitado e não pressionado -->
    <item android:state_enabled="true" android:state_pressed="false">
        <shape>
            <solid android:color="#2D89db" />
        </shape>
    </item>
    <!-- Desabilitado -->
    <item android:state_enabled="false">
        <shape>
            <solid android:color="#c9c9c9" />
        </shape>
    </item>
</selector>

Simplifying your verification logic would be:

senha.addTextChangedListener(new TextWatcher(){
    @Override
    public void afterTextChanged(Editable s) {
        if(!validar(matricula.getText().toString(), senha.getText().toString())){
            btn_Entrar.setEnabled(false);
        } else if(!matricula.getText().toString().equals("")){
            btn_Entrar.setEnabled(true);
        }
    }

    public void beforeTextChanged(CharSequence s, int start,int count, int after) {}
    public void onTextChanged(CharSequence s, int start,int before, int count) {}
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.