0

I want to create a selector drawable with #000000 for selected and #FFFFFF for unselected state.

How can I create a drawable programmatically?

Currently I am doing it as following:

StateListDrawable states = new StateListDrawable();
ColorDrawable cdPress = new ColorDrawable(0xFF0000);
ColorDrawable cdUnPress = new ColorDrawable(0x0101DF);

states.addState(new int[] { android.R.attr.state_selected}, cdPress);
states.addState(new int[] {-android.R.attr.state_selected}, cdUnPress);

view.setBackgroundDrawable(states);
view.setSelected(isSelected);
Sufian
  • 6,405
  • 16
  • 66
  • 120
Usman Afzal
  • 558
  • 1
  • 6
  • 13
  • 2
    First you need to create `Drawable` from your color and then create `StateListDrawable` and set this `Drawable` with it's `State`. and then at last set this `StateListDrawable` as `BackgroundDrawable` to your `View`. – M D Jul 02 '14 at 06:43
  • I know this But I have facing problem in this – Usman Afzal Jul 02 '14 at 06:55
  • StateListDrawable states = new StateListDrawable(); ColorDrawable cdPress = new ColorDrawable(0xFF0000); ColorDrawable cdUnPress = new ColorDrawable(0x0101DF); states.addState(new int[] { android.R.attr.state_selected },cdPress); states.addState(new int[] { -android.R.attr.state_selected },cdUnPress); view.setBackgroundDrawable(states); view.setSelected(isSelected); – Usman Afzal Jul 02 '14 at 06:56
  • StateListDrawable states = new StateListDrawable(); ColorDrawable cdPress = new ColorDrawable(0xFF0000); ColorDrawable cdUnPress = new ColorDrawable(0x0101DF); states.addState(new int[] { android.R.attr.state_selected },cdPress); states.addState(new int[] { -android.R.attr.state_selected },cdUnPress); view.setBackgroundDrawable(states); view.setSelected(isSelected); – Usman Afzal Jul 02 '14 at 06:56
  • @UA post your all code in your question then ill solve your issue..... – M D Jul 02 '14 at 07:00

1 Answers1

1

create stateListDrawable and pass to the view

   StateListDrawable stateListDrawable=new StateListDrawable();
   stateListDrawable.addState(new  int[]{android.R.attr.state_pressed}getColorDrawable(Colorcode));
   stateListDrawable.addState(new int[]{android.R.attr.state_focused},getColorDrawable(Colorcode));
    ...
    mView.setBackground(stateListDrawable);
    ...
    }
    private static Drawable getColorDrawable(int colorCode) {
            return new ColorDrawable(colorCode);
    }
Santhosh
  • 1,867
  • 2
  • 16
  • 23
  • states.addState(new int[] { android.R.attr.state_selected }, getResources().getDrawable(new ColorDrawable(0xFF0000))); – Usman Afzal Jul 02 '14 at 07:12
  • The method getDrawable(int) in the type Resources is not applicable for the arguments (ColorDrawable) – Usman Afzal Jul 02 '14 at 07:12
  • The method getDrawable(int) in the type Resources is not applicable for the arguments (ColorDrawable) – Usman Afzal Jul 02 '14 at 07:15
  • sorry i have modified..check it – Santhosh Jul 02 '14 at 07:17
  • states.addState(new int[] { android.R.attr.state_pressed }, getColorDrawable(getResources().getColor(R.color.white))); But not in the case stateListDrawable.addState(new int[]{android.R.attr.state_pressed}getColorDrawable(0xFFFFFF)); – Usman Afzal Jul 02 '14 at 07:32