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 the "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 the "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. android:id="@+id/textView1"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:text=" Pizza Hut"
  11. android:textSize="44dp"
  12. android:layout_alignParentTop="true"
  13. android:layout_alignParentStart="true"
  14. android:layout_marginTop="13dp" />
  15. <CheckBox
  16. android:id="@+id/checkBox1"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:text="Pizza"
  20. android:layout_below="@+id/textView1"
  21. android:layout_alignStart="@+id/checkBox2"
  22. android:layout_marginTop="95dp" />
  23. <CheckBox
  24. android:id="@+id/checkBox2"
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:text="Coffe"
  28. android:layout_marginTop="62dp"
  29. android:layout_below="@+id/checkBox1"
  30. android:layout_alignStart="@+id/checkBox3" />
  31. <CheckBox
  32. android:id="@+id/checkBox3"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:text="Burger"
  36. android:layout_below="@+id/checkBox2"
  37. android:layout_alignEnd="@+id/button1"
  38. android:layout_marginTop="43dp" />
  39. <Button
  40. android:id="@+id/button1"
  41. android:layout_width="wrap_content"
  42. android:layout_height="wrap_content"
  43. android:text="Order"
  44. android:layout_marginBottom="61dp"
  45. android:layout_alignParentBottom="true"
  46. android:layout_centerHorizontal="true" />
  47. </RelativeLayout>
Step 7
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. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.view.Menu;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.*;
  8. public class MainActivity extends Activity {
  9. CheckBox pizza,coffe,burger;
  10. Button buttonOrder;
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_main);
  15. addListenerOnButtonClick();
  16. }
  17. public void addListenerOnButtonClick(){
  18. pizza=(CheckBox)findViewById(R.id.checkBox1);
  19. coffe=(CheckBox)findViewById(R.id.checkBox2);
  20. burger=(CheckBox)findViewById(R.id.checkBox3);
  21. buttonOrder=(Button)findViewById(R.id.button1);
  22. buttonOrder.setOnClickListener(new OnClickListener(){
  23. @Override
  24. public void onClick(View view) {
  25. int totalamount=0;
  26. StringBuilder result=new StringBuilder();
  27. result.append("Selected Items:");
  28. if(pizza.isChecked()){
  29. result.append("\nPizza 100Rs");
  30. totalamount+=100;
  31. }
  32. if(coffe.isChecked()){
  33. result.append("\nCoffe 50Rs");
  34. totalamount+=50;
  35. }
  36. if(burger.isChecked()){
  37. result.append("\nBurger 120Rs");
  38. totalamount+=120;
  39. }
  40. result.append("\nTotal: "+totalamount+"Rs");
  41. Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_LONG).show();
  42. }
  43. });
  44. }
  45. @Override
  46. public boolean onCreateOptionsMenu(Menu menu) {.
  47. getMenuInflater().inflate(R.menu.activity_main, menu);
  48. return true;
  49. }
  50. }
Step 8
Now, go to the menu bar and click "Make Project" or press ctrl+f9 to debug the error.
Android
Step 9
Then, click the "Run" button or press shift+f10 To run the project. And choose the virtual machine and click OK.
Android

Conclusion

We have successfully created a Checkbox Android application using Android Studio.
Android
Android