Home > DeveloperSection > Beginner > Checkbox control in Android Application

Checkbox control in Android Application


Android Android 
Ratings:
1 Comment(s)
 5367  View(s)
Rate this:

Checkbox control in Android Application

In this article I am going to explain how to create a checkbox control in android application by using CheckBox widget. When the CheckBox is pressed, toast message will indicate the current state of the checkbox and the text of the checkbox will change.

·         Start a new project, named CheckBoxDemo.

·         Open res/layout/main.xml and add the checkbox as shown below:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent">

    <CheckBox android:id="@+id/checkbox"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="Check me"/>

</LinearLayout>

·         Open the activity file and add the following code as given below:

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.CheckBox;

import android.widget.Toast;

 

public class CheckBoxActivity extends Activity {

   

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        final CheckBox checkbox=(CheckBox)findViewById(R.id.checkbox);

        checkbox.setOnClickListener(new OnClickListener() {

              public void onClick(View v) {

                     if(((CheckBox)v).isChecked())

                     {

                           Toast.makeText(CheckBoxActivity.this, "Selected", 
                                                         Toast.
LENGTH_SHORT).show();

                           checkbox.setText("Unchecked me");

                     }

                     else

                     {

                           Toast.makeText(CheckBoxActivity.this, "Not Selected",
                                                          Toast.
LENGTH_SHORT).show();

                           checkbox.setText("Checked me");

                     }

                     }

              });

    }

}

This captures the CheckBox element from the layout, and then adds a View.OnClickListener. The View.OnClickListener must implement the onClick(View) callback method, which defines the action to be made when the checkbox is clicked. When clicked, isChecked() is called to check the new state of the check box. If it has been checked, then a Toast displays the message "Selected" and change the checkbox text to "Unchecked me”, otherwise it displays "Not selected" and change the checkbox text to "Checked me".

 

·         Run an application
Your output is something like this:

Checkbox control in Android Application

When you checked the checkbox, a toast display a message "Selected" and checkbox text will change into "Unchecked me".

Checkbox control in Android Application

Again, when you unchecked the checkbox, a toast display a message "Not Selected" and checkbox text will change into "Checked me".


store state of checkbox

By javed salat on   4 years ago
Sir How to store checked check-boxes and where to store please explain in detail...

Don't want to miss updates? Please click the below button!

Follow MindStick