Skip to content

Instantly share code, notes, and snippets.

@WSAyan
Last active September 22, 2021 05:33
Show Gist options
  • Save WSAyan/c64489a4dbcf1701d81b93af85e8d2e7 to your computer and use it in GitHub Desktop.
Save WSAyan/c64489a4dbcf1701d81b93af85e8d2e7 to your computer and use it in GitHub Desktop.
Android custom radio button example
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1dp"
android:color="@color/black" />
<corners android:radius="2dp" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/blue" />
<corners android:radius="2dp" />
</shape>
<resources>
<color name="white">#FFFFFF</color>
<color name="black">#000000</color>
<color name="blue">#35B5E5</color>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioGroup
android:id="@+id/checkRadio"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:id="@+id/check1RB"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_marginEnd="10dp"
android:background="@drawable/radio_button_selector"
android:button="@null"
android:gravity="center"
android:text="CHECK 1"
android:textColor="@drawable/radio_button_selector" />
<RadioButton
android:id="@+id/check2RB"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_marginEnd="10dp"
android:background="@drawable/radio_button_selector"
android:button="@null"
android:gravity="center"
android:text="CHECK 2"
android:textColor="@drawable/radio_button_selector" />
<RadioButton
android:id="@+id/check3RB"
android:layout_width="100dp"
android:layout_height="40dp"
android:background="@drawable/radio_button_selector"
android:button="@null"
android:gravity="center"
android:text="CHECK 3"
android:textColor="@drawable/radio_button_selector" />
</RadioGroup>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/white" android:drawable="@drawable/bg_on" android:state_checked="true" android:state_window_focused="false" />
<item android:color="@color/black" android:drawable="@drawable/bg_off" android:state_checked="false" android:state_window_focused="false" />
<item android:color="@color/white" android:drawable="@drawable/bg_on" android:state_checked="true" android:state_pressed="true" />
<item android:color="@color/black" android:drawable="@drawable/bg_off" android:state_checked="false" android:state_pressed="true" />
<item android:color="@color/white" android:drawable="@drawable/bg_on" android:state_checked="true" android:state_focused="true" />
<item android:color="@color/black" android:drawable="@drawable/bg_off" android:state_checked="false" android:state_focused="true" />
<item android:color="@color/black" android:drawable="@drawable/bg_on" android:state_checked="false" />
<item android:color="@color/white" android:drawable="@drawable/bg_off" android:state_checked="true" />
</selector>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment