Reputation: 558
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);
Upvotes: 0
Views: 1703
Reputation: 1867
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);
}
Upvotes: 1