1
I need to set Selector dynamically  

I took help from this How to set an image as a Background to a Button using StateListDrawable (dynamically or programmatically or through code)

When i try to set default image its not working

state.addState(new int[] { },selectedthumb);

Please help me

What i have tried

StateListDrawable state = new StateListDrawable();
state.addState(new int[] { android.R.attr.state_selected },
                        selectedthumb);
state.addState(new int[] { android.R.attr.state_focused },  thumb);

state.addState(new int[] {  },thumb);
Community
  • 1
  • 1
isha
  • 349
  • 2
  • 12

2 Answers2

1
public void selector(Button b,int selectedthumb,int thumb)
    {
        StateListDrawable states = new StateListDrawable();
        states.addState(new int[] {- android.R.attr.state_pressed }, getResources().getDrawable(selectedthumb));
        states.addState(new int[] {}, getResources().getDrawable(thumb));
        b.setBackgroundDrawable(states);
    }
sud
  • 505
  • 1
  • 4
  • 12
0

Check this answer 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);
Community
  • 1
  • 1
Mustanser Iqbal
  • 5,017
  • 4
  • 18
  • 36