Setting Background Drawable with states to android buttons

Tarek Eldeeb picture Tarek Eldeeb · Mar 8, 2013 · Viewed 19.6k times · Source

I am building a multiple choice questionnaire android program. I have the following in my onCreate() method

btnArray = new Button[5];
btnArray[0] = (Button) findViewById(R.id.bOp1);
btnArray[1] = (Button) findViewById(R.id.bOp2);
btnArray[2] = (Button) findViewById(R.id.bOp3);
btnArray[3] = (Button) findViewById(R.id.bOp4);
btnArray[4] = (Button) findViewById(R.id.bOp5);

Typeface othmanyFont = Typeface.createFromAsset(getAssets(),
        "fonts/amiri.ttf");
Drawable shape = getResources().getDrawable(R.drawable.optionbutton);

for(int i=0;i<5;i++){
    btnArray[i].setTypeface(myFont);
    ((Button)btnArray[i]).setBackgroundDrawable(shape); //Button-4 only turns red
    btnArray[i].setOnClickListener(this);
}  

The drawable resource "optionbutton.xml" defines a red colour gradient for the "pressed state", and a grey for other states. It looks as follows

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_pressed="true" >
    <shape>
    <gradient
      android:startColor="#bf1d00"
      android:endColor="#801300"
      android:angle="270" />
        <corners android:radius="10dp" />
    <stroke
      android:width="1dp"
      android:color="#71c2eb" />
    </shape>
  </item>
  <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <gradient android:startColor="#FFFFFF" 
                android:endColor="#999"
                android:angle="270" />
            <corners android:radius="10dp" />
            <stroke android:width="1px" android:color="#0070b7" />
        </shape>
  </item>
</selector>

The selector actually works, but on the last applied button only. No matter which button from the 5 is pressed, the last (not the one pressed) button only gets its background red.

As a debugging step: I tried to unroll the for-loop and change order of applying the background, the last applied button only gets red background:

((Button)btnArray[0]).setBackgroundDrawable(shape);
((Button)btnArray[1]).setBackgroundDrawable(shape);
((Button)btnArray[2]).setBackgroundDrawable(shape);
((Button)btnArray[4]).setBackgroundDrawable(shape);
((Button)btnArray[3]).setBackgroundDrawable(shape); //Button-3 only turns red

This is confusing! What's wrong with my implementation?

Answer

DjHacktorReborn picture DjHacktorReborn · Mar 8, 2013

you can set shape in one xml and button state in another then try here is xml for button to get different state

 <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:drawable="@drawable/save_button_active" android:state_focused="true" android:state_pressed="true"/>
    <item android:drawable="@drawable/save_button_active" android:state_focused="false" android:state_pressed="true"/>
    <item android:drawable="@drawable/save_button_active" android:state_focused="true"/>
    <item android:drawable="@drawable/save_button_inactive" android:state_focused="false" android:state_pressed="false"/>
    </selector>