Android : How to update the selector(StateListDrawable) programmatically Android : How to update the selector(StateListDrawable) programmatically android android

Android : How to update the selector(StateListDrawable) programmatically


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.


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 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.