0

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;   
}
The Matrix
  • 1,268
  • 2
  • 14
  • 23
  • You can define color in your `android:drawable=` attribute as `android:drawable="@color/your_color"` or `android:drawable="#ffffff"` – Pankaj Jan 19 '16 at 17:29
  • http://stackoverflow.com/questions/4551959/how-to-set-the-background-color-for-button-in-android-during-run-time – Amit Vaghela Jan 19 '16 at 17:29

2 Answers2

1

You can use "StateListDrawable"

StateListDrawable

and add state in it. Let me know if you face problem in implementing this.

Ashish Rajvanshi
  • 466
  • 4
  • 15
0

If you want to create a round button. Then you can use layer-list then add this drawable xml as a Layer.

Eg. How to create round button.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:bottom="0dp"
        android:left="0dp"
        android:right="0dp"
        android:top="0dp">
        <shape android:shape="rectangle" >
            <corners android:radius="40dp" />
            <solid android:color="#fff" />
        </shape>
    </item>

    <item
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp"
        android:top="1dp">
        <shape android:shape="rectangle" >
            <corners android:radius="40dp" />
            <solid android:color="#d3d3d3" />
        </shape>
    </item>

    <item
        android:bottom="1dp"
        android:left="2dp"
        android:right="1dp"
        android:top="2dp">
        <shape android:shape="rectangle" >
            <corners android:radius="40dp" />
            <solid android:color="#EBEBEB" />
        </shape>
    </item>
    <item
        android:id="@+id/wl_btn_bg_shape"
        android:bottom="6dp"
        android:left="6dp"
        android:right="6dp"
        android:top="6dp">
        <shape android:shape="rectangle" >
            <corners android:radius="40dp" />
            <solid android:color="#00caa8" />
        </shape>
    </item>

</layer-list>

create and save about code in drawable as wl_btn_bg.xml

Now you need to add this xml as view background

LayerDrawable layers = (LayerDrawable) context.getResources().getDrawable(R.drawable.wl_btn_bg);
view.setBackground(layers);

and if you want to change the color of selector then you need to add following lines in layers

LayerDrawable layers = (LayerDrawable) context.getResources().getDrawable(R.drawable.wl_btn_bg);
        GradientDrawable shape = (GradientDrawable) (layers.findDrawableByLayerId(R.id.wl_btn_bg_shape));
shape.setColor(Color.parseColor("#ff6655"));

Done....

Ashish Rajvanshi
  • 466
  • 4
  • 15