Change button color when clicking on it or when validating text fill

3

Hello everyone, I'm having a hard time making the button change color when the% co_of% s fields are filled in and return to the initial color when the fields are empty and also change color when pressed.

Code snippet in EditText :

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) {}
});

XML Property:

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>
    
asked by anonymous 26.10.2014 / 07:39

1 answer

3

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

As a solution using the State List Drawable you already set for your button, we can add the state_enabled state to reach the goal.

<?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>

Simply simplifying your verification logic, you would:

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) {}
});
    
26.10.2014 / 22:35