Search code examples
androidandroid-vectordrawableandroid-radiobutton

AppCompatRadioButton using vector asset


I am having problems on 4.4 devices with using Vector Drawables. First of all i switched my RadioButton to AppCompatRadioButton so i could use vectors. Thing is i am not sure how i am supposed to use them as i have indicator in xml (indicator_selector) which is basically just on and off assets for click state:

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

    <item android:drawable="@drawable/on" android:state_checked="true"/>
    <item android:drawable="@drawable/of" android:state_checked="false"/>

</selector>

As this are vector drawables should i change them to

app:srcCompat=@drawable/on?

And when creating buttons in code should i use any other specific way for vector drawables or no? This is my current code:

AppCompatRadioButton radioBtn = new AppCompatRadioButton(activity);
radioBtn.setButtonDrawable(R.drawable.indicator_selector);

Solution

  • So basically i was not able to make it work with XML files, i just wrote code for states like

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

    and then set it up to AppCompatRadioButton and it works :)