How to get checkbox checked value in android

How to Select CheckBox selected values.

There are lots of ways to select checkbox selected values. checkbox has basically its two different type of events one is checked other is unchecked. You can select checkbox values and show a toast message on screen with checkbox selection. So here is the complete step by step tutorial for How to get checkbox checked value in android.

android-project-download-code-button

How to get checkbox checked value in android.

Code for MainActivity.java file.

 package com.android_examples.com.getcheckboxvalue;

import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

 CheckBox First,Second,Third;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 First = (CheckBox)findViewById(R.id.checkBox1);
 Second = (CheckBox)findViewById(R.id.checkBox2);
 Third = (CheckBox)findViewById(R.id.checkBox3);
 
 //First CheckBox 
 First.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 if(First.isChecked())
 {
 Toast.makeText(MainActivity.this,"First checkbox checked", Toast.LENGTH_SHORT).show();
 }
 else
 {
 Toast.makeText(MainActivity.this,"First checkbox Unchecked", Toast.LENGTH_SHORT).show();
 }
 
 }
 });
 
 //Second CheckBox
 Second.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 if(Second.isChecked())
 {
 Toast.makeText(MainActivity.this,"Second checkbox checked", Toast.LENGTH_SHORT).show();
 }
 else
 {
 Toast.makeText(MainActivity.this,"Second checkbox Unchecked", Toast.LENGTH_SHORT).show();
 }
 
 }
 });
 
 //Third CheckBox

 Third.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 if(Third.isChecked())
 {
 Toast.makeText(MainActivity.this,"Third checkbox checked", Toast.LENGTH_SHORT).show();
 }
 else
 {
 Toast.makeText(MainActivity.this,"Third checkbox Unchecked", Toast.LENGTH_SHORT).show();
 }
 
 }
 });
 
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
 // Inflate the menu; this adds items to the action bar if it is present.
 getMenuInflater().inflate(R.menu.main, menu);
 return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
 // Handle action bar item clicks here. The action bar will
 // automatically handle clicks on the Home/Up button, so long
 // as you specify a parent activity in AndroidManifest.xml.
 int id = item.getItemId();
 if (id == R.id.action_settings) {
 return true;
 }
 return super.onOptionsItemSelected(item);
 }
}

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.android_examples.com.getcheckboxvalue.MainActivity" >

 <CheckBox
 android:id="@+id/checkBox2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/checkBox1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="15dp"
 android:text="CheckBox Second" />

 <CheckBox
 android:id="@+id/checkBox1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignLeft="@+id/checkBox2"
 android:layout_alignParentTop="true"
 android:layout_marginTop="72dp"
 android:text="CheckBox First" />

 <CheckBox
 android:id="@+id/checkBox3"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignLeft="@+id/checkBox2"
 android:layout_below="@+id/checkBox2"
 android:layout_marginTop="18dp"
 android:text="CheckBox Third" />

</RelativeLayout>

Screenshot:

How to get checkbox checked value in android

checkbox unchecked

Click Here To Download checkbox checked value in android project.

Leave a Reply

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