Replace selector images programmatically

The question:

I have an ImageView that has a drawable image resource set to a selector. How do I programmatically access the selector and change the images of the highlighted and non-highlighted state?

Here is a code of selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="" android:id="@+id/iconSelector">
  <!-- pressed -->
  <item android:state_pressed="true" android:drawable="@drawable/btn_icon_hl" />
  <!-- focused -->
  <item android:state_focused="true" android:drawable="@drawable/btn_icon_hl" />
  <!-- default -->
  <item android:drawable="@drawable/btn_icon" />

I want to be able to replace btn_icon_hl and btn_icon with other images.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

As far as I’ve been able to find (I’ve tried doing something similar myself), there’s no way to modify a single state after the StateListDrawable has already been defined. You can, however, define a NEW one through code:

StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed},
states.addState(new int[] {android.R.attr.state_focused},
states.addState(new int[] { },

And you could just keep two of them on hand, or create a different one as you need it.

Method 2

I had the same problem and went a step further to solve it. The only problem however is you can not specify the NavStateListDrawable in xml, so you have to set the background of your UI element through code. The onStateChange method must then be overriden to ensure that every time the level of the main drawable is changed, that you also update the level of the child level list.

When constructing the NavStateListDrawable you have to pass in the level of the icon you wish to display.

public class NavStateListDrawable extends StateListDrawable {

    private int level;

    public NavStateListDrawable(Context context, int level) {

        this.level = level;
        //int stateChecked = android.R.attr.state_checked;
        int stateFocused = android.R.attr.state_focused;
        int statePressed = android.R.attr.state_pressed;
        int stateSelected = android.R.attr.state_selected;

        addState(new int[]{ stateSelected      }, context.getResources().getDrawable(R.drawable.nav_btn_pressed));
        addState(new int[]{ statePressed      }, context.getResources().getDrawable(R.drawable.nav_btn_selected));
        addState(new int[]{ stateFocused      }, context.getResources().getDrawable(R.drawable.nav_btn_focused));

        addState(new int[]{-stateFocused, -statePressed, -stateSelected}, context.getResources().getDrawable(R.drawable.nav_btn_default));


    protected boolean onStateChange(int[] stateSet) {

        boolean nowstate = super.onStateChange(stateSet);

            LayerDrawable defaultDrawable = (LayerDrawable)this.getCurrent();

            LevelListDrawable bar2 =  (LevelListDrawable)defaultDrawable.findDrawableByLayerId(;
        }catch(Exception exception)


        return nowstate;

For all of the different navigation button drawable states i have something like the following.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="">

   <item android:drawable="@drawable/top_bar_default" >


    <item android:id="@+id/nav_icons" android:bottom="0dip">
        <level-list xmlns:android="">
            <item android:maxLevel="0" >
                    android:gravity="center" />
            <item android:maxLevel="1" >
                    android:gravity="center" />
            <item android:maxLevel="2" >
                    android:gravity="center" />
            <item android:maxLevel="3" >
                    android:gravity="center" />



I was going to post this as a question and answer, but seeing as you’ve asked the very question, here you go. Note, this saves you a hell of a lot of xml file definitions. i went down from about 50-100 xml definitions down to about 4!.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment