RadioButton View and CheckBox View in Android

When you are creating a form to accept users inputs in your android app then you have to list down a few options for the user to choose from, checkboxes and radio boxes can be used.
So the functional difference between radio box and check box is that we can use radio box when out of multiple options you need to select only one if multiple selection is allowed then check box can be used.

CheckBox View in Android

Checkbox is used when we have to show multiple options to the user and the user can choose as many options as they want, by tapping on them and we can set its default check status as true or false and other properties are the same as TextView.


<CheckBox
    android:id="@+id/myCheckbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Salesforcedrillers"
    android:checked="true"
    android:textColor="@android:color/black"
    android:layout_marginLeft="30dp"/>

  • android:checked=”true”
    This attribute checks that is, places the tick mark in the checkbox are by default.
    This covers how we can display a CheckBox view on the app screen and how it works is something that we will learn very soon.
  • Output Screen

RadioButton View in Android

RadioButton can be used when you have to allow selection of one option among the list of multiple options and It is used under its parent view – RadioGroup so that we can select value out of all the listed radio buttons.


<RadioGroup
    android:id="@+id/rg_gender"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="20dp">

    <RadioButton
        android:id="@+id/rb_male"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Male"
        android:textColor="@android:color/black"/>

    <RadioButton
        android:id="@+id/rb_female"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:text="Female"/>

</RadioGroup>

Also RadioGroup is a subclass of LinearLayout and arranges the radio button vertically because it has vertical orientation by default and we can change that by adding the attribute android:orientation=”horizontal” to your RadioGroup in the layout XML file.

Output Screen:

Subscribe Now