Android – CheckBox Example – OnCheckedChangeListener

By -

Here I am going to give you a simple demo of CheckBox and its OnCheckedChangeListener. In this tutorial, we will see the toast notification whenever a particular CheckBox is checked.

main.xml

<?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:text="Android"
    	android:id="@+id/chkBoxAndroid"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content">
	</CheckBox>

	<CheckBox
		android:text="iPhone"
		android:id="@+id/chkBoxIPhone"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content">
	</CheckBox>

	<CheckBox
		android:text="BlackBerry"
		android:id="@+id/chkBoxBlackBerry"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content">
	</CheckBox>
</LinearLayout>

CheckBoxCheckedDemo.java

package com.technotalkative.checkboxdemo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

public class CheckBoxCheckedDemo extends Activity
{
	CheckBox chkBoxAndroid;
	CheckBox chkBoxIPhone;
	CheckBox chkBoxBlackBerry;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		initialUISetup();
	}

	 public void initialUISetup() {
	    	chkBoxAndroid = (CheckBox) findViewById(R.id.chkBoxAndroid);
	    	chkBoxIPhone = (CheckBox) findViewById(R.id.chkBoxIPhone);
	    	chkBoxBlackBerry = (CheckBox) findViewById(R.id.chkBoxBlackBerry);

	    	chkBoxAndroid.setOnCheckedChangeListener(new myCheckBoxChnageClicker());
	    	chkBoxIPhone.setOnCheckedChangeListener(new myCheckBoxChnageClicker());
	    	chkBoxBlackBerry.setOnCheckedChangeListener(new myCheckBoxChnageClicker());
	 }

	 class myCheckBoxChnageClicker implements CheckBox.OnCheckedChangeListener
	 {

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			// TODO Auto-generated method stub

		 // Toast.makeText(CheckBoxCheckedDemo.this, &quot;Checked =&gt; &quot;+isChecked, Toast.LENGTH_SHORT).show();

		  if(isChecked) {
		        if(buttonView==chkBoxAndroid) {
				showTextNotification("Android");
			}

			if(buttonView==chkBoxIPhone) {
				showTextNotification("iPhone");
			}

			if(buttonView==chkBoxBlackBerry) {
				showTextNotification("BlackBerry");
			}
		  }
		}
	 }

	 public void showTextNotification(String msgToDisplay) {
	    	Toast.makeText(this, msgToDisplay, Toast.LENGTH_SHORT).show();
	 }
}

CEO & Co-Founder at SolGuruz® | Organiser @ GDG Ahmedabad | Top 0.1% over StackOverflow | 15+ years experienced Tech Consultant | Helping startups with Custom Software Development

Loading Facebook Comments ...
Loading Disqus Comments ...