How to change the button color when pressed?



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



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

    });

    https://stackoverflow.com/questions/17189715/android-button-color-changing-on-onclick




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2