Beginner
    AJAX
    Android
    ASP.Net
    C#
    Cloud Computing
    Crystal Report
    Database
    Drupal
    HTML5
    JavaScript
    Joomla
    JQuery
    MVC
    php
    Reporting
    SharePoint
    SQL Server
    VB.Net
    Windows Phone
    WordPress
    WPF
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> Android >> Checkbox control in Android Application




Checkbox control in Android Application - MindStick

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".



 Author Information
Name: Rohit Kesharwani
Member Since: 8/8/2011
Article Submitted Date: 10/22/2011
Location:     India
Report Abuse Form
Reason:    
 
Total Online Users: 5561
Advertisement
mindstick
Advertise with Us
  
Dudelabs
Copyright © 2009 - 2013MindStick. All Rights Reserved.