How to change the color of the button when pressed?

Asked

Viewed 460 times

0

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

  • Look at this example I asked in another question, does exactly that: https://answall.com/a/260618/88202

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 wear it like 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

Browser other questions tagged

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