In my app, I have the selector below.
Is possible to create a similar selector, by color instead of by image-drawable, but at runtime?
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_window_focused="false"
android:drawable="@drawable/ic_button_orange_normal" />
<item
android:state_pressed="true"
android:drawable="@drawable/ic_button_orange_pressed" />
<item
android:state_focused="true"
android:drawable="@drawable/ic_button_orange_focused" />
<item
android:drawable="@drawable/ic_button_orange_normal" />
</selector>
I have tried the function below to create the selector in runtime, but I need to create a round button, not square.
Is this possible?
private StateListDrawable makeSelector(int color) { StateListDrawable res = new StateListDrawable(); res.setExitFadeDuration(400); res.setAlpha(45); ShapeDrawable d = new ShapeDrawable(); res.addState(new int[] { android.R.attr.state_pressed }, new ColorDrawable(ColorUtilities.decrease(color, 0x003030))); res.addState(new int[] {}, new ColorDrawable(Color.TRANSPARENT)); return res; }
Edit again... I have found the way to create round selectors...
private static ShapeDrawable getRoundShapeDrawable(int color) { ShapeDrawable shape_drawable = new ShapeDrawable(new OvalShape()); shape_drawable.getPaint().setColor(color); return shape_drawable; } public static StateListDrawable getRoundShapeSelector(int normal_color, int pressed_color) { ShapeDrawable normal_shape_drawable = getRoundShapeDrawable(normal_color), pressed_shape_drawable = getRoundShapeDrawable(pressed_color); StateListDrawable state_list_drawable = new StateListDrawable(); state_list_drawable.addState(new int[] { android.R.attr.state_pressed }, pressed_shape_drawable); state_list_drawable.addState(new int[] { android.R.attr.state_focused }, pressed_shape_drawable); state_list_drawable.addState(new int[] { }, normal_shape_drawable); return state_list_drawable; }