How to use Checkbox in Android?

In this Article we create how to use Checkbox in android.
In Android, CheckBox is a type of two state button either unchecked or checked.
Checkboxes allow the user to select one or more options from a set. In android each checkbox is managed separately and you must register a click listener for each one. A special type of button that has two states: checked or unchecked. In android, there is a lot of usage of check box.

//Simple Checkbox Example in android

Step 1: Firstly, open your activity_main.xml file and add following code ( Open res -> layout -> activity_main.xml and add following code:)
// activity_main.xml

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="@dimen/activity_vertical_margin">

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="What would you like to take in snakes?"
android:textAppearance="?android:attr/textAppearanceLarge" />

<CheckBox
android:id="@+id/milk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/milk" />

<CheckBox
android:id="@+id/burger"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/burger" />

<CheckBox
android:id="@+id/pizza"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pizza" />

</LinearLayout>

Step 2: Let’s  open .Java file and and add following code (Open src -> package -> MainActivity.java and add following code: )
Code//

package com.example.thakur.checkbox;

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 {
 private CheckBox pizza, burger, milk;

 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 pizza = (CheckBox) findViewById(R.id.pizza);
 burger = (CheckBox) findViewById(R.id.burger);
 milk = (CheckBox) findViewById(R.id.milk);
 milk.setOnClickListener(checkboxClickListener);
 burger.setOnClickListener(checkboxClickListener);
 pizza.setOnClickListener(checkboxClickListener);

 /* Set blogging by default */
 pizza.setChecked(true);

 }

 View.OnClickListener checkboxClickListener = new View.OnClickListener() {
 @Override
 public void onClick(View view) {
 boolean checked = ((CheckBox) view).isChecked();
 if (checked) {
 String text = null;
 switch (view.getId()){
 case R.id.burger:
 text = "Yes, I love burger";
 break;
 case R.id.milk:
 text = "never";
 break;
 case R.id.pizza:
 text="wow, I like pizza";
 break;
 }
 Toast.makeText(MainActivity.this, text, Toast.LENGTH_LONG).show();
 }
 }
 };
}

Step 3: Open AndroidManifest.xml and add following code :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.example.thakur.checkbox">

 <application
 android:allowBackup="true"
 android:icon="@mipmap/ic_launcher"
 android:label="@string/app_name"
 android:supportsRtl="true"
 android:theme="@style/AppTheme">
 <activity android:name=".MainActivity">
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />

 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
 </activity>
 </application>

</manifest>

Step 4 : Our output will be like this :

1

2

3

Thank You..

Tags: , ,