Android – CheckBox Example – isChecked()

By -

Here we are going to have a demo of CheckBox’s isChecked() method. The isChecked() method is used to check whether the particular CheckBox is checked or unchecked.

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="Cycling"
		android:id="@+id/chkBoxCycling"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content">
	</CheckBox>

	<CheckBox
		android:text="Teaching"
		android:id="@+id/chkBoxTeaching"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content">
	</CheckBox>

	<CheckBox
		android:text="Blogging"
		android:id="@+id/chkBoxBlogging"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content">
	</CheckBox>

	<Button
		android:text="Get Hobby"
		android:id="@+id/button1"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:onClick="getHobbyClick">
	</Button>
</LinearLayout>

CheckBoxDemo.java

package com.technotalkative.checkboxdemo;

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

public class GetHobby extends Activity
{
	CheckBox chkBoxCycling;
	CheckBox chkBoxTeaching;
	CheckBox chkBoxBlogging;

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

		initialUISetup();
	}

	public void initialUISetup()
	{
		chkBoxCycling = (CheckBox) findViewById(R.id.chkBoxCycling);
		chkBoxTeaching = (CheckBox) findViewById(R.id.chkBoxTeaching);
		chkBoxBlogging = (CheckBox) findViewById(R.id.chkBoxBlogging);
	}

	public void getHobbyClick(View v)
	{
		String strMessage = "";

		if(chkBoxCycling.isChecked())
		{
			strMessage+="Cycling ";
		}

		if(chkBoxTeaching.isChecked())
		{
			strMessage+="Teaching ";
		}

		if(chkBoxBlogging.isChecked())
		{
			strMessage+="Blogging ";
		}

		showTextNotification(strMessage);
	}

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