Android Tutorial – Example of Using CheckBox button





In this tutorial we will teach you about CheckBox button in Android Application Development. How they are used and how they can be implemented in Android Application.

CheckBox button – it is a two states button in Android i.e. it can be work in checked or unchecked state and it can perform corresponding tasks/operation according to its state.

 Must see : Android Tutorial – Sending Data Using Intent

Example code showing the use of CheckBox button

In this example code we will make an android application which will show toast message “I am checked” when checkbox button is checked and will show toast message “I am unchecked” when checkbox button is unchecked.

Excuse me, here are the best reads for you

Movie Hall For Couples : Ebony Lounge Review Movie Hall For Couples 5 Best Places To Visit With Girlfriend In Delhi Places To Visit With Gf Top Private, Secret Places For Couples In Delhi Secret Places | Couples Oysters Water Park Gurgaon Entry Ticket Price Oysters Water Park 5 Most Haunted Places In Delhi You Can Visit Most Haunted Places Best Nightout Places In Delhi/NCR Best Night Out Places Top 4 places To Hang out With Friends In Delhi Places To Hang Out

In this example we use two functions:

  1. setOnCheckedChangeListener(this): this function work is to check/listen the change in the state of the CheckBox button during operation.
  2. isChecked(): this function work is to state/check whether the checkbox is in checked state or in unchecked state.If the value of this function is true that means checkbox button is in checked state otherwise it is in unchecked state.

Don’t Miss : Android Tutorial: Hello World First Android Project

Now create a new project named as ‘CheckBox’, give the name ‘CheckActivity’ to your activity and give the name ‘activity_check’ to your layout.

Edit your activity_check.xml

This is the layout for our project which displays chechbox button and user interface on the screen

 

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

tools:context=“.CheckActivity” >

 

<CheckBox

android:id=“@+id/checkBox1”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_alignParentTop=“true”

android:layout_centerHorizontal=“true”

android:layout_marginTop=“50dp”

android:checked=“true”

 

android:text=“CheckBox” />

 

</RelativeLayout>

Edit your CheckActivity.java

This is the java class of our project where implementation of our task is done using two functions i.e. setOnCheckedChangeListener(this) and isChecked() on CheckBox button.

Useful : Android Tutorial – Using EditText, TextView and Button with Example

package com.youthgiri.checkbox;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.widget.CheckBox;

import android.widget.CompoundButton;

import android.widget.CompoundButton.OnCheckedChangeListener;

import android.widget.Toast;

 

public class CheckActivity extends Activity implements OnCheckedChangeListener {

CheckBox c;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_check);

c = (CheckBox)findViewById(R.id.checkBox1);

 

c.setOnCheckedChangeListener(this);

 

}

@Override

public void onCheckedChanged(CompoundButton arg0, boolean arg1) {

// TODO Auto-generated method stub

if(c.isChecked()){

Toast.makeText(this, “I am checked”, 0).show();

}

else{

Toast.makeText(this, “I am unchecked”, 0).show();

}

}

}

 Now the run application on emulator and it will be like this.

t4-1

 

t4-2

Hope you enjoyed this tutorial and gained information about Android Tutorial – Example of Using CheckBox button.  For more details stay connected with us. Also, you can put your queries in Comment Box, we will respond quickly. 

Suggestions BY Google





Leave a Reply

Your email address will not be published.


*