CheckBox Tutorial With Example In Android Studio. In Android, CheckBox is a type of two state button either unchecked or checked in Android. Or you can say it is a type of on/off switch that can be toggled by the users. CompoundButton is the parent class of CheckBox class.
Lets write a simple example for checkBox, whenever we check the box it will display a Toast message. The code for MainActivity.java and acitivity.xml is shown below.
MainActivity.java
- package com.gcrt.checkboxhandling;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- Button b1;
- CheckBox ck1,ck2,ck3,ck4;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- b1 = (Button) findViewById(R.id.button);
- ck1 = (CheckBox) findViewById(R.id.checkBox);
- ck2 = (CheckBox) findViewById(R.id.checkBox2);
- ck3 = (CheckBox) findViewById(R.id.checkBox3);
- ck4 = (CheckBox) findViewById(R.id.checkBox4);
- }
- public void click(View view) {
- CheckBox checkBox = (CheckBox) view;
- boolean isSelected = checkBox.isChecked();
- switch (view.getId()) {
- case R.id.checkBox:
- if (isSelected == true) {
- Toast.makeText(MainActivity.this, "APPLE IS SELECTED", Toast.LENGTH_SHORT).show();
- }
- break;
- case R.id.checkBox2:
- if (isSelected == true) {
- Toast.makeText(MainActivity.this, "MOTO G IS SELECTED", Toast.LENGTH_SHORT).show();
- }
- break;
- case R.id.checkBox3:
- if (isSelected == true) {
- Toast.makeText(MainActivity.this, "REDME IS SELECTED", Toast.LENGTH_SHORT).show();
- }
- break;
- case R.id.checkBox4:
- if (isSelected == true) {
- Toast.makeText(MainActivity.this, "NOKIA IS SELCTED", Toast.LENGTH_SHORT).show();
- }
- break;
- }
- }
- public void clicked(View view) {
- if (ck1.isChecked()== false || ck4.isChecked() == false || ck2.isChecked() == false || ck3.isChecked() == false) {
- Toast.makeText(MainActivity.this, "Select at least one", Toast.LENGTH_SHORT).show();
- }
- }
- }
acitivity.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:orientation="vertical"
- android:background="#FF9"
- android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
- <CheckBox
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="APPLE"
- android:layout_marginTop="30dp"
- android:textSize="60px"
- android:onClick="click"
- android:id="@+id/checkBox" />
- <CheckBox
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="MOTO G"
- android:layout_marginTop="30dp"
- android:onClick="click"
- android:textSize="60px"
- android:id="@+id/checkBox2" />
- <CheckBox
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="REDMI"
- android:layout_marginTop="20dp"
- android:onClick="click"
- android:textSize="60px"
- android:id="@+id/checkBox3" />
- <CheckBox
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="NOKIA"
- android:layout_marginTop="20dp"
- android:onClick="click"
- android:textSize="60px"
- android:id="@+id/checkBox4" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="CHECK"
- android:onClick="clicked"
- android:id="@+id/button" />
- </LinearLayout>
For more detail checkout the video below. Do subscribe to our channel.
Hope this post is helpful,for more useful post like and share with your friends, we also have Youtube channel for more details check out the video below, do like share and subscribe to our channel it will boost our energy and help to improve our work, Have Nice Day.
/span>
Check Box Button Handling-Android Studio.
Reviewed by gcrt
on
March 20, 2018
Rating:

No comments: