Add setOnCheckedChangeListener on CheckBox in android

How to use checked change listener on CheckBox in android application programmatically.

In this tutorial we are implementing setOnCheckedChangeListener() method individually on radio button. So here is the complete step by step tutorial for Add setOnCheckedChangeListener on CheckBox  in android.

android-project-download-code-button

How to Add setOnCheckedChangeListener on CheckBox  in android.

Code for MainActivity.java file.

 package com.setoncheckedchangelistener_android_examples.com;

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

public class MainActivity extends Activity {

 CheckBox checkbox1,checkbox2;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 checkbox1 = (CheckBox)findViewById(R.id.checkBox1);
 checkbox2 = (CheckBox)findViewById(R.id.checkBox2);
 
 checkbox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

 @Override
 public void onCheckedChanged(CompoundButton buttonView,
 boolean isChecked) {
 // TODO Auto-generated method stub
 
 if(isChecked)
 {
 Toast.makeText(MainActivity.this, "Android Checked", Toast.LENGTH_LONG).show();
 }
 else
 {
 Toast.makeText(MainActivity.this, "Android Un-Checked", Toast.LENGTH_LONG).show();
 }
 
 }
 
 });
 
 checkbox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

 @Override
 public void onCheckedChanged(CompoundButton buttonView,
 boolean isChecked) {
 // TODO Auto-generated method stub
 
 if(isChecked)
 {
 Toast.makeText(MainActivity.this, "Blogger Checked", Toast.LENGTH_LONG).show();
 }
 else
 {
 Toast.makeText(MainActivity.this, "Blogger Un-Checked", Toast.LENGTH_LONG).show();
 }
 
 }
 
 });
 }

}

Code for activity_main.xml layout file.

 <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="com.setoncheckedchangelistener_android_examples.com.MainActivity" >

 <CheckBox
 android:id="@+id/checkBox1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Android" />

 <CheckBox
 android:id="@+id/checkBox2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignLeft="@+id/checkBox1"
 android:layout_below="@+id/checkBox1"
 android:text="Blogger" />

</RelativeLayout>

Screenshot:

Add setOnCheckedChangeListener on CheckBox in android

android-unchecked

Click here to download Add setOnCheckedChangeListener on CheckBox in android project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *