How to show checklist or checkbox items from java android application — Java Android
This is how you can display an Android Check Box Itemsin
Main Activity in Java Android Studio:
This example demonstrates how to use the checkbox in android studio. Also you are going to make if else conditions practice.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
How to use Checklist or Checkbox items in android?
Step 2 — “Add the following code to res/layout/activity_main.xml.“
I worked in simple relative layout method for only give basic example.
*In the below code, we have declared 3 checkBox (checkBox1 — Messi, checkBox2 — Ronaldo, checkBox3 — Neymar) and TextView (textView.setText) and Button (Give On Click method inside design parts see the below photo and give submitButton id)
LAYOUT FILE CODE IN 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"
tools:context="com.yourname.yourandroidstuido.MainActivity"
android:padding="16dp"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:layout_marginBottom="50dp"
android:textColor="@color/colorPrimary"
android:textSize="25sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="Which footballer do you like?"
android:textColor="@color/colorAccent"
android:textStyle="bold" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Messi" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ronaldo" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Neymar" />
<Button
android:id="@+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:onClick="onClick"/>
<TextView
android:id="@+id/textViewJavaLearn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="300dp"
android:gravity="center"
android:text="5S JAVA AND KOTLIN LEARNING" />
</LinearLayout>
MainActivity.java” java file − Add the following code to src/MainActivity.java
JAVA CODE IN MAIN ACTIVITY:
package com.yourname.yourpackagename;import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;public class MainActivity extends AppCompatActivity {
//give id and implement method
CheckBox checkBox1, checkBox2, checkBox3;
Button submitButton;
TextView textView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
checkBox3 = (CheckBox) findViewById(R.id.checkBox3);
//meet id inside create method
submitButton = (Button) findViewById(R.id.submitButton);
textView = (TextView) findViewById(R.id.textView);
}
public void onClick(View view){
if (checkBox1.isChecked() == true && checkBox2.isChecked() == true && checkBox3.isChecked() == true){
textView.setText("You like all footballer ⚽ !");
}
else if (checkBox1.isChecked() == true && checkBox2.isChecked() == true){
textView.setText("You like Messi and Ronaldo !");
}
else if (checkBox1.isChecked() == true && checkBox3.isChecked() == true){
textView.setText("You like Messi and Neymar !");
}
else if (checkBox2.isChecked() == true && checkBox3.isChecked() == true){
textView.setText("You like Ronaldo and Neymar !");
}
else if(checkBox1.isChecked() == true){
textView.setText("You like Messi !");
}
else if (checkBox2.isChecked() == true){
textView.setText("You like Ronaldo !");
}
else if (checkBox3.isChecked() == true){
textView.setText("You like Neymar !");
}
else{
textView.setText("You don't like these footballer? ZLATAN IBRAHIMOVIC ONLY LOVE ❤\uD83D\uDD25");
}
}
}
Review Application
Fourth Step Results and How to Work Rating Bar !
In this lesson, you learn how to CheckBox items in java.
If you have any question write comments I will reply.
Thanks for reading my blog.
Have a nice day.
If you need Google AdMob Alternatives you can check out Start Ad Platfrom.
Regards
5S JAVA AND KOTLIN LEARNING