Native Mobile App Development With Java

Android CheckBox

15-Sep-2024

Implement CheckBox functionality in Android Studio Project Java

simple example of using a CheckBox in an Android app. In this example, a CheckBox Checked data show in TextView:


Here is the tutorial for Kotlin



There Are Some Common Attributes for CheckBox


id
This is unique identity.
layout_widthmatch_parent -> Text area contains fullscreen
wrap_content-> Text area contains only text size
layout_widthmatch_parent -> Text area contains fullscreen
wrap_content-> Text area contains only text size
textSize
Change The Text Size
textColor
Change Text Color
textStyle
bold, italic, normal -> Style Change
background
Set background Color



Step 1 :

File open res/layout/activity_main.xml file −



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_margin="20dp"
android:orientation="vertical">


<CheckBox
android:id="@+id/checkBox1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Mango" />

<CheckBox
android:id="@+id/checkBox2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Banana" />

<CheckBox
android:id="@+id/checkBox3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Orange" />

<Button
android:id="@+id/buttonSubmit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit" />

<TextView
android:id="@+id/textResult"
android:layout_width="match_parent"
android:gravity="center"
android:textSize="20dp"
android:layout_marginTop="20dp"
android:layout_height="wrap_content"
android:text="Show Result" />
</LinearLayout>



</RelativeLayout>


Step 2 :

File open com.microappvalley.newtestproject/MainActivity.java file −



package com.microappvalley.newtestproject;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
Button buttonSubmit;
CheckBox checkBox1;
CheckBox checkBox2;
CheckBox checkBox3;
TextView textResult;
ImageButton imageButton;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonSubmit=(Button) findViewById(R.id.buttonSubmit);
checkBox1=(CheckBox) findViewById(R.id.checkBox1);
checkBox2=(CheckBox) findViewById(R.id.checkBox2);
checkBox3=(CheckBox) findViewById(R.id.checkBox3);
checkBox3=(CheckBox) findViewById(R.id.checkBox3);
textResult=(TextView) findViewById(R.id.textResult);


buttonSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String checkdData="";
if (checkBox1.isChecked())
{
checkdData=checkdData + checkBox1.getText().toString()+"\n";
}


if (checkBox2.isChecked())
{
checkdData=checkdData + checkBox2.getText().toString()+"\n";
}


if (checkBox3.isChecked())
{
checkdData=checkdData + checkBox2.getText().toString()+"\n";
}

textResult.setText(checkdData);
Toast.makeText(MainActivity.this, checkdData, Toast.LENGTH_SHORT).show();
}
});
}
}



Open Device Manager, run the emulator, and  then run the application. Next, check the working output and check the output  you declared in your code.


Output:




Comments