Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android combine Drawable and Shape into a single Drawable (programatically)

I'm setting android:drawable of a RadioButton programatically like this:

Drawable unchecked = getResources().getDrawable(R.drawable.ic_room_car);

Drawable d= getResources().getDrawable(R.drawable.ic_room_car);

Shape circle = ?????;
??? checked = combine circle and d


SateListDrawable states= new StateListDrawable();
states.addState(new int[] {android.R.attr.state_checked}, checked);
states.addState(new int[] {}, unchecked);

((RadioButton)findViewById(R.id.icon_1)).setButtonDrawable(states);

Basically it has a custom icon that I'm reading from resources when it's unchecked, and when checked I would like to draw a circle behind the icon.

The unchecked behavior is set in the second addState line, and it's working. My problem is how to draw a circle programatically and then somehow combine it with the drawable I'm reading from resources.

like image 653
rbasniak Avatar asked Dec 30 '15 13:12

rbasniak


1 Answers

It's a layer-list.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@mipmap/ic_launcher" android:left="10dp" android:right="10dp" android:top="10dp" android:bottom="10dp">
    </item>
    <item>
       <shape android:shape="oval">
            <solid android:color="@android:color/transparent"></solid>
            <stroke android:color="#4b14b1" android:width="1dp"></stroke>
            <size android:height="50dp" android:width="50dp"></size>
        </shape>
    </item>
</layer-list>
like image 88
tiny sunlight Avatar answered Nov 17 '22 21:11

tiny sunlight