Check-box and radio-button in Android

  • Check box and radio button both are the very import part of any application. These controls plays important role to select choices from the given list.
  • In the case of check box, we can select more than one item from the given options.
  • In the case of radio button, mostly it is used to select one choice from the given options such as select gender from the given options.
  • These both options is shown in the following diagram:
  • android

  • We can use these options by dragging and drop to the screen or activity.
  • Check box can have the following properties shown in the diagram:
  • android

  • Using these properties, we can change the text color of the checkbox.
  • We can define the text size using android:textSize attribute.
  • We can define the text style using android:textStyle attribute.
  • Radio button can have the following properties shown in the diagram:
  • android

  • Using these properties, we can change the text color of the checkbox.
  • We can define the text size using android:textSize attribute.
  • We can define the text style using android:textStyle attribute.
  • Below we will see the example.
  • android

    android

    android

    android

  • The xml file is shown below:
  • <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
    android:id="@+id/tvMessage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

    <CheckBox
    android:id="@+id/chk1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/tvMessage"
    android:layout_marginTop="64dp"
    android:text="CheckBox" />

    <RadioButton
    android:id="@+id/rdb1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/chk1"
    android:layout_below="@+id/chk1"
    android:layout_marginTop="40dp"
    android:text="RadioButton" />

    </RelativeLayout>
  • In this file, we have used the relative layout.
  • The java code is as follows:
  • package com.example.checkradiobutton;

    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.CheckBox;
    import android.widget.RadioButton;
    import android.widget.TextView;

    public class MainActivity extends Activity {
    // declare the CheckBox, RadioButton and TextView object
    TextView message;
    CheckBox check;
    RadioButton radio;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // register text view, CheckBox, RadioButton
    message = (TextView)findViewById(R.id.tvMessage);
    check = (CheckBox) findViewById(R.id.chk1);
    radio = (RadioButton) findViewById(R.id.rdb1);

    // when we check the check box
    check.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
    // TODO Auto-generated method stub
    if(check.isChecked())
    {
    message.setText("Check box checked");
    }
    else
    {
    message.setText("Check box is not checked");
    }

    }
    });

    // when radio button click
    radio.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
    // TODO Auto-generated method stub
    if(radio.isChecked())
    {
    message.setText("radio button is selected");
    }

    }
    });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }
    }
  • In this java file we have imported the following packages:
  • android

  • Now we have to declare the object of TextView, ChcekBox and RadioButton class as follows:
  • android

  • Now we have to register the controls which we have defined in the layout file as follows:
  • android

  • Now we have to set the action on radio button and check box.
  • We have provide the following functionality for the check box:
  • android

  • In this method, we have checked that the check box is checked or not.
  • When the app will launch then the default message “hello world” display on the screen.
  • Now inside the function, we are checking that the checkbox is checked or not.
  • When first time we check the checkbox then “Check box checked” message is shown on the screen and the default message will be replaced with this message.
  • No when we uncheck the checkbox then the message will be replaced with the “Check box is not checked”.
  • Here isChecked() method id used to check the checkbox is checked or not if checkbox is checked then it will return true and the if part will be executed otherwise else part will be executed.
  • Now here we have provided the following functionality to the radio button as follows:
  • android

  • In this, first we have checked that the radio button is selected or not in the if condition. If the radio button is selected then message that are displayed on the screen will replaced with the “radio button is selected”.
  • In this, we can select the radio button only once.
  • Output is shown above diagrams.