How to make image as a radio button in android

This question has been answered before Below is from @Benito-Bertoli

RadioButton - how to use a custom drawable?

Give your radiobutton a custom style:

<style name="MyRadioButtonStyle" parent="@android:style/Widget.CompoundButton.RadioButton">
    <item name="android:button">@drawable/custom_btn_radio</item>
</style>

custom_btn_radio.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_checked="true" android:state_window_focused="false"
      android:drawable="@drawable/btn_radio_on" />
   <item android:state_checked="false" android:state_window_focused="false"
      android:drawable="@drawable/btn_radio_off" />

   <item android:state_checked="true" android:state_pressed="true"
      android:drawable="@drawable/btn_radio_on_pressed" />
   <item android:state_checked="false" android:state_pressed="true"
      android:drawable="@drawable/btn_radio_off_pressed" />

   <item android:state_checked="true" android:state_focused="true"
      android:drawable="@drawable/btn_radio_on_selected" />
   <item android:state_checked="false" android:state_focused="true"
      android:drawable="@drawable/btn_radio_off_selected" />

   <item android:state_checked="false" android:drawable="@drawable/btn_radio_off" />
   <item android:state_checked="true" android:drawable="@drawable/btn_radio_on" />
</selector>

Replace the drawables with your own.


Try like this

  <RadioGroup
    android:layout_width="wrap_content"
    android:orientation="horizontal"
    android:layout_height="wrap_content">

    <RadioButton
        android:button="@null"
        android:background="@mipmap/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <RadioButton
        android:button="@null"
        android:background="@mipmap/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <RadioButton
        android:button="@null"
        android:background="@mipmap/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RadioGroup>

output:(margin and pading by yourself)

enter image description here

Tags:

Java

Android