I want to update the selector for a button programmatically.
I can do this with the xml file which is given below
I am going to answer your question "How to update the selector for a BUTTON programmatically?" by proposing to switch a button for a LinearLayout
with embedded ImageView
and TextView
. There are a number of benefits to doing this, especially if you will later decide to customize your views. There is no loss of functionality resulting from this switch. You will still be able to attach same event listeners you can attach to a button, but will be able to avoid the buttons/tabs styling nightmares. Here is a relevant code from the layout.xml
<LinearLayout
android:id="@+id/button"
style="@style/ButtonStyle">
<ImageView
android:id="@+id/background"
android:src="@drawable/custom_image"/>
<TextView
style="@style/TextStyle"
android:text="Custom Button"
android:id="@+id/text"/>
</LinearLayout>
Next, I have a selector file called custom_image.xml located in the drawable folder. Here is the content of the selector file
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/disabled_img" android:state_enabled="false" />
<item android:drawable="@drawable/unselected_img" android:state_selected="false" />
<item android:drawable="@drawable/selected_img" android:state_selected="true" />
</selector>
The three source image files (disabled_img.png, unselected_img.png, selected_img.png) are also located in the drawable folder.
Now back to your Java code. There is no need for the funky StateListDrawable
garbage for many reasons. First, it just looks ugly, and is hard to maintain. But most importantly it goes against the principles of keeping your logic separate from your presentation. If you are managing your drawable resources in Java code, you know you are doing something fundametally wrong.
Here is what I am proposing instead. Whenever you want your button to be selected, you just pop this one-liner in there:
((LinearLayout)findViewById(R.id.button)).setSelected(true);
Or whenever you want the button to be in the disabled state, here is another one-liner:
((ImageView)findViewById(R.id.background)).setEnabled(false);
Please notice that in this last example I am specifying the disabled state on the ImageView inside the LinearLayout. For some reason whenever you change the enabled / disabled state of the LinearLayout, the selector is not being triggered. It works fine when you do it on the ImageView instead.
Very late response here but in case anyone else is having problems setting a StateListDrawable programmatically. Then as with the XML files the order in which you set the states into the StateListDrawable is important.
For example this will work as expected:
StateListDrawable sld = new StateListDrawable();
sld.addState(new int[] { android.R.attr.state_pressed }, new ColorDrawable(Color.GRAY));
sld.addState(new int[] {}, new ColorDrawable(Color.GREEN));
This won't:
StateListDrawable sld = new StateListDrawable();
sld.addState(new int[] {}, new ColorDrawable(Color.GREEN));
sld.addState(new int[] { android.R.attr.state_pressed }, new ColorDrawable(Color.GRAY));
I don't know how you are adding the StateListDrawable
, since the code is not here. But be sure to be check the documentation and the adding the setState()
.
You can set the properties from the View, such as yourView.setEnabled(true)
I hope that helps
Not enough rep to comment but the accepted answer did not work for me.
My goal was for designers to set enabled, disabled, and pressed background colors on some button. I intended for them to use it to test colors on different displays.
I noticed some other people mentioned it did not work for them either.
The states that need to be defined are
Here is some code that will give you an idea of the states. I am using Color.Parse() to generate ints for the colors then I pass them to this method to get the StateListDrawable.
private StateListDrawable createDrawable(int enabled, int pressed, int disabled) {
StateListDrawable stateListDrawable = new StateListDrawable();
stateListDrawable.addState(new int[] { -android.R.attr.state_pressed, android.R.attr.state_enabled }, new ColorDrawable(enabled));
stateListDrawable.addState(new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled }, new ColorDrawable(pressed));
stateListDrawable.addState(new int[] { -android.R.attr.state_enabled }, new ColorDrawable(disabled));
return stateListDrawable;
}
You need to use the negative value of the needed state. E.g.:
states.addState(new int[] {-android.R.attr.state_enabled},R.drawable.btn_disabled);
Notice the "-" sign before android.R.attr.state_enabled
.