How to change the color of the button when pressed? [duplicate]

0

I need to change the color of a button when it is pressed, how could it be done?

    
asked by anonymous 06.12.2017 / 03:14

1 answer

0

buttoncolor.xml

<?xml version="1.0" encoding="utf-8"?> 
  <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
      <item android:state_focused="true" android:state_pressed="true" 
            android:drawable="@drawable/bgalt" /> 
      <item android:state_focused="false" android:state_pressed="true" 
            android:drawable="@drawable/bgalt" /> 
      <item android:drawable="@drawable/bgnorm" /> 
  </selector>

Now use this:

b1 = (Button) findViewById(R.id.b1);
b1.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
         b2.setBackgroundDrawable(getResources().getDrawable(R.drawable.whatever));
    }
});

b2 = (Button) findViewById(R.id.b2);
b2.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
         b1.setBackgroundDrawable(getResources().getDrawable(R.drawable.whatever));
    }
});

Reference: StackOverflow in English

    
06.12.2017 / 10:47