Adding Check Box Functionality In An Android App Using Android Studio

Introduction

In this article, I will show you how to create an Android app using Android Studio that uses Checkbox. Android Checkbox is a type of two state button - either checked or unchecked. There can be a lot of uses for checkboxes. For example, it can be used to learn the hobby of the user or to activate/deactivate the specific action etc.

Requirements

Steps to be followed -

Follow these steps to create the aforementioned app. I have included the source code in the attachment.

Step 1

Open Android Studio and start a new Android Studio Project.

Android

Step 2

You can choose your application name and choose where your project is stored. If you wish to use C++ for coding the project, mark the "Include C++ support", and click "Next" button.

Android

Step 3

Now, select the version of Android and select the target Android devices. We need to choose the SDK level which plays an important role in running the application.

Android

Step 4

Now, add the activity and click "Next" button.

Android

Step 5

Add Activity name and click "Finish".

Android

Step 6

Go to activity_main.xml. This XML file contains the designing code for your Android app.

Android

The XML code is given below.

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.     <TextView  
  7.   
  8.         android:id="@+id/textView1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:text=" Pizza Hut"  
  12.         android:textSize="44dp"  
  13.         android:layout_alignParentTop="true"  
  14.         android:layout_alignParentStart="true"  
  15.         android:layout_marginTop="13dp" />  
  16.   
  17.     <CheckBox  
  18.         android:id="@+id/checkBox1"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:text="Pizza"  
  22.         android:layout_below="@+id/textView1"  
  23.         android:layout_alignStart="@+id/checkBox2"  
  24.         android:layout_marginTop="95dp" />  
  25.   
  26.     <CheckBox  
  27.         android:id="@+id/checkBox2"  
  28.         android:layout_width="wrap_content"  
  29.         android:layout_height="wrap_content"  
  30.         android:text="Coffe"  
  31.         android:layout_marginTop="62dp"  
  32.         android:layout_below="@+id/checkBox1"  
  33.         android:layout_alignStart="@+id/checkBox3" />  
  34.   
  35.     <CheckBox  
  36.         android:id="@+id/checkBox3"  
  37.         android:layout_width="wrap_content"  
  38.         android:layout_height="wrap_content"  
  39.         android:text="Burger"  
  40.         android:layout_below="@+id/checkBox2"  
  41.         android:layout_alignEnd="@+id/button1"  
  42.         android:layout_marginTop="43dp" />  
  43.   
  44.     <Button  
  45.         android:id="@+id/button1"  
  46.         android:layout_width="wrap_content"  
  47.         android:layout_height="wrap_content"  
  48.         android:text="Order"  
  49.         android:layout_marginBottom="61dp"  
  50.         android:layout_alignParentBottom="true"  
  51.         android:layout_centerHorizontal="true" />  
  52.   
  53. </RelativeLayout>  

Step 6

Go to Main Activity.java. This Java program is the backend language for Android apps.

Android

The java code is given below.

  1. package abu.checkbox;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.*;  
  9.   
  10. public class MainActivity extends Activity {  
  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.        pizza=(CheckBox)findViewById(R.id.checkBox1);  
  21.         coffe=(CheckBox)findViewById(R.id.checkBox2);  
  22.         burger=(CheckBox)findViewById(R.id.checkBox3);  
  23.         buttonOrder=(Button)findViewById(R.id.button1);  
  24.         buttonOrder.setOnClickListener(new OnClickListener(){  
  25.   
  26.             @Override  
  27.             public void onClick(View view) {  
  28.                 int totalamount=0;  
  29.                 StringBuilder result=new StringBuilder();  
  30.                 result.append("Selected Items:");  
  31.                 if(pizza.isChecked()){  
  32.                     result.append("\nPizza 100Rs");  
  33.                     totalamount+=100;  
  34.                 }  
  35.                 if(coffe.isChecked()){  
  36.                     result.append("\nCoffe 50Rs");  
  37.                     totalamount+=50;  
  38.                 }  
  39.                 if(burger.isChecked()){  
  40.                     result.append("\nBurger 120Rs");  
  41.                     totalamount+=120;  
  42.                 }  
  43.                 result.append("\nTotal: "+totalamount+"Rs");  
  44.            
  45.                 Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_LONG).show();  
  46.             }  
  47.   
  48.         });  
  49.     }  
  50.     @Override  
  51.     public boolean onCreateOptionsMenu(Menu menu) {.  
  52.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  53.         return true;  
  54.     }  
  55.   
  56. }   

Step 8

Now, go to menu bar and click "Make Project" or press ctrl+f9 to debug the error.

Android

Step 9

Then, click "Run" button or press shift+f10 To run the project. And choose the virtual machine and click OK.

Android

Conclusion

We have successfully created Checkbox Android application using Android Studio.

Android

Android

Up Next
    Ebook Download
    View all
    Learn
    View all