Noorul
Noorul

Reputation: 3444

How to use vector drawable(svg) as button on checkbox and radio button in android

I am using vector drawable(svg) files for Checkbox and radio button with custom design. I can't able to set the button on those component.

 <CheckBox android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:button="@drawable/checkbox_button_bg"
           android:checked="true"
           android:padding="@dimen/min_padding"
           android:layout_margin="@dimen/min_margin"
           android:text="Do you need recurring" />

 <RadioButton android:id="@+id/rdbtnNever"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:padding="@dimen/most_min_padding"
              android:button="@drawable/radio_button_bg"
              android:text="Never"/>

drawable/radio_button_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:drawable="@drawable/radio_btn_select"
    android:state_checked="true"
    android:state_pressed="true" />
<item
    android:drawable="@drawable/radio_btn_select"
    android:state_pressed="true" />
<item
    android:drawable="@drawable/radio_btn_select"
    android:state_checked="true" />
<item
    android:drawable="@drawable/radio_btn_unselect" />
</selector>

here radio_btn_select and radio_btn_unselect is vector drawable

And i have done for checkbox as like below.

checkbox_button_bg.xml

 <?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android" >
  <item
    android:drawable="@drawable/checkbox_check"
    android:state_checked="true"
    android:state_pressed="true" />
<item
    android:drawable="@drawable/checkbox_check"
    android:state_pressed="true" />
<item
    android:drawable="@drawable/checkbox_check"
    android:state_checked="true" />
</selector>

Here checkbox_check is vector drawable when I run the program, xml inflating exception raised, and which goes off when I remove the android:button line in both component. so, my doubt is, how to use the vector drawable's in these components?

UPDATE

I have added following code in app and vector drawable completely working fine for Imageview src adding. with the help of app:srcCompat="@drawable/mic"

 defaultConfig {
    minSdkVersion 15
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    vectorDrawables.useSupportLibrary = true
}

Upvotes: 2

Views: 3521

Answers (1)

Peterdk
Peterdk

Reputation: 16005

These days there is a property app:buttonCompat on the CompatCheckbox which works the same as app:srcCompat. It solved my issue with no vector button appearing as checkbox button on Android 4.x.

Upvotes: 2

Related Questions