Add setOnClickListener to CheckBox in android

How to implement on click listener click event on check box and retrieve checkbox clicked value.

In this tutorial we are adding setOnClickListener method on check box and displaying a toast message while user select or deselect the check box. So here is the complete step by step tutorial for Add setOnClickListener to CheckBox in android.

android-project-download-code-button

How to Add setOnClickListener to CheckBox in android.

Code for MainActivity.java file.

 package com.addsetonclicklistenercheckbox_android_examples.com;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends Activity {

 CheckBox one,two;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 one = (CheckBox)findViewById(R.id.checkBox1);
 two = (CheckBox)findViewById(R.id.checkBox2);
 
 one.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 if(one.isChecked())
 {
 Toast.makeText(MainActivity.this, "WordPress Checked", Toast.LENGTH_LONG).show();
 }
 else
 {
 Toast.makeText(MainActivity.this, "WordPress Un-Checked", Toast.LENGTH_LONG).show();
 }
 
 }
 
 });
 
 two.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 if(two.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.addsetonclicklistenercheckbox_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="WordPress" />

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

Screenshots:

Add setOnClickListener to CheckBox in android

wordpress-de-select

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

Leave a Reply

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