Thursday, March 12, 2020

Custom checkbox on Android with drawble

Custom checkbox on Android with drawble

Suppose there are two image resources that draw check and uncheck states: and with the names drawable / checkbox_empty and drawable / checkbox_check, we can create a selector as follows:
drawable/checkbox_custome.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false"
        android:drawable="@drawable/checkbox_empty" />
    <item android:state_checked="true"
        android:drawable="@drawable/checkbox_check" />
</selector>
layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="example.javatpoint.com.customcheckbox.MainActivity">
<CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New CheckBox"
        android:id="@+id/checkBox"
android:button="@drawable/checkbox_custome"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="46dp" />
</RelativeLayout>
Custom checkbox on Android with drawble

No comments:

Post a Comment