Tuesday, March 16, 2021

Android CheckBox

Android CheckBox Example

android checkbox

Android CheckBox is a type of two state button either checked or unchecked.

There can be a lot of usage of checkboxes. For example, it can be used to know the hobby of the user, activate/deactivate the specific action etc.

Android CheckBox class is the subclass of CompoundButton class.

Android CheckBox class

The android.widget.CheckBox class provides the facility of creating the CheckBoxes.

Methods of CheckBox class

There are many inherited methods of View, TextView, and Button classes in the CheckBox class. Some of them are as follows:

MethodDescription
public boolean isChecked()Returns true if it is checked otherwise false.
public void setChecked(boolean status)Changes the state of the CheckBox.


Android CheckBox Example

activity_main.xml

Drag the three checkboxes and one button for the layout. Now the activity_main.xml file will look like this:

File: activity_main.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="example.javatpoint.com.checkbox.MainActivity">  
  8.   
  9.   
  10.     <CheckBox  
  11.         android:id="@+id/checkBox"  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:layout_marginLeft="144dp"  
  15.         android:layout_marginTop="68dp"  
  16.         android:text="Pizza"  
  17.         app:layout_constraintStart_toStartOf="parent"  
  18.         app:layout_constraintTop_toTopOf="parent" />  
  19.   
  20.     <CheckBox  
  21.         android:id="@+id/checkBox2"  
  22.         android:layout_width="wrap_content"  
  23.         android:layout_height="wrap_content"  
  24.         android:layout_marginLeft="144dp"  
  25.         android:layout_marginTop="28dp"  
  26.         android:text="Coffee"  
  27.         app:layout_constraintStart_toStartOf="parent"  
  28.         app:layout_constraintTop_toBottomOf="@+id/checkBox" />  
  29.   
  30.     <CheckBox  
  31.         android:id="@+id/checkBox3"  
  32.         android:layout_width="wrap_content"  
  33.         android:layout_height="wrap_content"  
  34.         android:layout_marginLeft="144dp"  
  35.         android:layout_marginTop="28dp"  
  36.         android:text="Burger"  
  37.         app:layout_constraintStart_toStartOf="parent"  
  38.         app:layout_constraintTop_toBottomOf="@+id/checkBox2" />  
  39.   
  40.     <Button  
  41.         android:id="@+id/button"  
  42.         android:layout_width="wrap_content"  
  43.         android:layout_height="wrap_content"  
  44.         android:layout_marginLeft="144dp"  
  45.         android:layout_marginTop="184dp"  
  46.         android:text="Order"  
  47.         app:layout_constraintStart_toStartOf="parent"  
  48.         app:layout_constraintTop_toBottomOf="@+id/checkBox3" />  
  49.   
  50. </android.support.constraint.ConstraintLayout>  

Activity class

Let's write the code to check which toggle button is ON/OFF.

File: MainActivity.java
  1. package example.javatpoint.com.checkbox;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. import android.widget.CheckBox;  
  8. import android.widget.Toast;  
  9.   
  10. public class MainActivity extends AppCompatActivity {  
  11.     CheckBox pizza,coffe,burger;  
  12.     Button buttonOrder;  
  13.     @Override  
  14.     protected void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.activity_main);  
  17.         addListenerOnButtonClick();  
  18.     }  
  19.     public void addListenerOnButtonClick(){  
  20.         //Getting instance of CheckBoxes and Button from the activty_main.xml file  
  21.         pizza=(CheckBox)findViewById(R.id.checkBox);  
  22.         coffe=(CheckBox)findViewById(R.id.checkBox2);  
  23.         burger=(CheckBox)findViewById(R.id.checkBox3);  
  24.         buttonOrder=(Button)findViewById(R.id.button);  
  25.   
  26.         //Applying the Listener on the Button click  
  27.         buttonOrder.setOnClickListener(new View.OnClickListener(){  
  28.   
  29.             @Override  
  30.             public void onClick(View view) {  
  31.                 int totalamount=0;  
  32.                 StringBuilder result=new StringBuilder();  
  33.                 result.append("Selected Items:");  
  34.                 if(pizza.isChecked()){  
  35.                     result.append("\nPizza 100Rs");  
  36.                     totalamount+=100;  
  37.                 }  
  38.                 if(coffe.isChecked()){  
  39.                     result.append("\nCoffe 50Rs");  
  40.                     totalamount+=50;  
  41.                 }  
  42.                 if(burger.isChecked()){  
  43.                     result.append("\nBurger 120Rs");  
  44.                     totalamount+=120;  
  45.                 }  
  46.                 result.append("\nTotal: "+totalamount+"Rs");  
  47.                 //Displaying the message on the toast  
  48.                 Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_LONG).show();  
  49.             }  
  50.   
  51.         });  
  52.     }  
  53. }  

Output:

android checkbox example output 1 android checkbox example output 2

No comments:

Post a Comment

Inapp update

  Inapp update https://desk.zoho.com/portal/vegabirdtech/en/kb/articles/how-to-use-burp-suite-with-android-mobile