0

I want to change background of a button when it is clicked. I tried to use a selector. But It didn't work. Here is the selector (add_grp_slctr.xml):

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

And the button :

       <Button
            android:id="@+id/addGrpBtn"
            android:layout_width="55dp"
            android:layout_height="45dp"
            android:layout_gravity="center"
            android:background="@drawable/add_grp_slctr"
            android:onClick="addGrpDialogOpen" />

add_grp_d and add_grp are images(png).

MilesDyson
  • 738
  • 1
  • 11
  • 33

5 Answers5

3

I tried a similar code which will be white by default, black when pressed on an onclick of a button:

//***This is the btn_selector which is to be declared in drawable folder***

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@android:color/black" /> <!-- pressed -->
    <item android:drawable="@android:color/white" /> <!-- default -->
</selector>

and called this on the button.xml -->

 android:background="@drawable/btn_selector"

Hope this would help..:)

Pihu
  • 1,041
  • 11
  • 35
1

go through the http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList once. Also state_focussed for button only works when you are focussing the button using a hardware-keyboard. As for your case

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
     <item android:state_pressed="false" android:state_selected="false" android:drawable="@drawable/button_default"/>
     <item android:state_pressed="true" android:state_selected="false" android:drawable="@drawable/button_default" />
     <item android:state_pressed="false" android:state_selected="true" android:drawable="@drawable/button_selected"/>
     <item android:state_pressed="true" android:state_selected="true" android:drawable="@drawable/button_selected" />
     <item android:drawable="@drawable/button_selected" /> 
</selector>
Vishnu Prabhu
  • 449
  • 1
  • 5
  • 19
0

Use your selector

change your code like this

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

My Code

<item android:drawable="@drawable/shadow_design_click" android:state_pressed="true"/>
<item android:drawable="@drawable/shadow_design" android:state_focused="true"/>
<item android:drawable="@drawable/shadow_design"/>

</selector>
Karthick pop
  • 616
  • 3
  • 16
0

I think you should change your selector a bit. Check this answer here.

Community
  • 1
  • 1
loumaros
  • 433
  • 3
  • 7
0

Instead of passing cuastom XML file, if you just want to change the color of you button then you can try with the following way.

Button lineColorCode = (Button)findViewById(R.id.button1);

Now inside button's click event use following code.

int color = Color.parseColor("#AE6118"); //The color u want             
lineColorCode.setColorFilter(color);
InnocentKiller
  • 5,234
  • 7
  • 36
  • 84