XML attributes of CheckBox widget
XML Attributes | Description |
android:id | Used to uniquely identify a CheckBox |
android:checked | To set the default state of a CheckBox as checked or uncheched |
android:background | To set the background color of a CheckBox |
android:text | Used to store a text inside the CheckBox |
android:fontFamily | To set the font of the text of the CheckBox |
android:textSize | To set the CheckBox text size |
android:layout_width | To set the CheckBox width |
android:layout_height | To set the CheckBox height |
android:gravity | Used to adjust the CheckBox text alignment |
android:padding | Used to adjust the left, right, top and bottom padding of the CheckBox |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:id="@+id/checkBox_container" android:background="#168BC34A" android:layout_width="0dp" android:layout_height="wrap_content" android:orientation="vertical"> <CheckBox android:id="@+id/checkBox" android:layout_width="match_parent" android:layout_height="wrap_content" android:fontFamily="@font/roboto" android:text="@string/checkBox1_text" android:textSize="18sp" android:padding="7dp"/> <CheckBox android:id="@+id/checkBox4" android:layout_width="match_parent" android:layout_height="wrap_content" android:fontFamily="@font/roboto" android:text="@string/checkBox4_text" android:textSize="18sp" android:padding="7dp"/> <!-- Customized CheckBox --> <CheckBox android:id="@+id/pizza" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:background="@drawable/rbtn_bg" android:button="@android:color/transparent" android:elevation="10dp" android:gravity="center" android:text="Pizza -350₹" /> <CheckBox android:id="@+id/coffee" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:background="@drawable/rbtn_bg" android:button="@android:color/transparent" android:elevation="10dp" android:gravity="center" android:text="Coffee -100₹" /> </LinearLayout>
Make Customized CheckBox
android:button="@android:color/transparent"- removes tick box of checkbox and only text remains
android:background="@drawable/checked_state"- it is custom background design that can show different background based on state of radio button (focused or not)
- used it can be made in drawable resource file :
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true"> <shape> <solid android:color="@color/Purpal" /> <corners android:radius="12dp" /> </shape> </item> <item android:state_checked="false"> <shape> <solid android:color="@color/white" /> <corners android:radius="12dp" /> </shape> </item> </selector>
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="@color/Purpal" android:state_focused="false" /> <item android:color="@color/Purpal" android:state_focused="true" /> <item android:color="@color/Purpal" /> </selector>