Create A CheckBox App In An Android Application Using Android Studio

Introduction

This blog explains how to develop a checkbox app in an Android Application, using Android Studio.

Requirements

  • Android Studio 2.1.3

If you want create a checkbox app, it should follow the steps given below.

Step 1

Now, open Android Studio and you can choose the File, followed by New.  Afterwards, choose New Project.

Android

Step 2

Here, you can create Your Application name and choose where your project is stored on the location and click Next button.

Android

Now, we can select the version of Android. It is Target Android Devices.

Android

Step 3

Here, we can add the activity, more activity is available, and click Next button.

Android

Now, we can write the activity name and click the Finish button.

Android

Step 4

Now, open your project and you will go to the activity_main.xml and afterwards, you will build the design. It should choose toolbox and if you want some other option (CheckBox, TextView), use the drag and drop method.

Android

Now, we can see the Graphical User Interface design.

Android

Step 5

Now, you will go to the resource folder and select the String.xml. Subsequently, you need to add the string value.

Android

srings.xml code

  1. <resources>  
  2.     <string name="app_name">CheckBoxApp</string>  
  3.     <string name="Apple">APPLE</string>  
  4.     <string name="Banana">BANANA</string>  
  5.     <string name="BluBerry">BLUEBERRY</string>  
  6.     <string name="Grapes">GRAPES</string>  
  7.     <string name="Straw">STRAW</string>  
  8. </resources>  
Step 6

Here, you need to build on design and write the .XML code.

activity_main.xml code.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout 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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="xyz.rvconstructions.www.checkboxapp.MainActivity">  
  3.     <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Select Your favorite fruits: " android:textColor="#f00" android:textSize="20sp" android:textStyle="bold" />  
  4.     <LinearLayout android:id="@+id/linearLayout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:background="#e0e0e0" android:orientation="vertical">  
  5.         <CheckBox android:id="@+id/applechkb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:checked="false" android:padding="20dp" android:text="@string/Apple" android:textColor="#44f" android:textSize="20sp" android:textStyle="bold|italic" />  
  6.         <CheckBox android:id="@+id/bananachkb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:checked="false" android:padding="20dp" android:text="@string/Banana" android:textColor="#f44" android:textSize="20sp" android:textStyle="bold|italic" />  
  7.         <CheckBox android:id="@+id/strawchkb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:checked="false" android:padding="20dp" android:text="@string/BluBerry" android:textColor="#444" android:textSize="20sp" android:textStyle="bold|italic" />  
  8.         <CheckBox android:id="@+id/blueberrychkb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:checked="false" android:padding="20dp" android:text="@string/Grapes" android:textColor="#888" android:textSize="20sp" android:textStyle="bold|italic" />  
  9.         <CheckBox android:id="@+id/grapschkb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:checked="false" android:padding="20dp" android:text="@string/Straw" android:textColor="#101010" android:textSize="20sp" android:textStyle="bold|italic" />  
  10.     </LinearLayout>  
  11. </RelativeLayout>  
Step 7

Now, you will go to MainActivity.java page and build Java code.

First of all, you will declare a file, which is an extension file.

Android

Now, we can see MainActivity.java code.
  1. package xyz.rvconstructions.www.checkboxapp;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.graphics.Color;  
  6. import android.os.Bundle;  
  7. import android.view.Menu;  
  8. import android.view.MenuItem;  
  9. import android.view.View;  
  10. import android.widget.Button;  
  11.   
  12. import android.widget.CheckBox;  
  13. import android.widget.Toast;  
  14.   
  15. public class MainActivity extends AppCompatActivity implements View.OnClickListener {  
  16.     CheckBox Abble, Banana, Straw, Blueberry, Grapes;  
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.activity_main);  
  21.   
  22.         Abble = (CheckBox) findViewById(R.id.applechkb);  
  23.         Abble.setOnClickListener(this);  
  24.         Banana = (CheckBox) findViewById(R.id.bananachkb);  
  25.         Banana.setOnClickListener(this);  
  26.         Straw = (CheckBox) findViewById(R.id.strawchkb);  
  27.         Straw.setOnClickListener(this);  
  28.         Blueberry = (CheckBox) findViewById(R.id.blueberrychkb);  
  29.         Blueberry.setOnClickListener(this);  
  30.         Grapes = (CheckBox) findViewById(R.id.grapschkb);  
  31.         Grapes.setOnClickListener(this);  
  32.     }  
  33.     @Override  
  34.     public void onClick(View view) {  
  35.         switch (view.getId()) {  
  36.             case R.id.applechkb:  
  37.                 if (Abble.isChecked())  
  38.                     Toast.makeText(getApplicationContext(), "Your clicked abble", Toast.LENGTH_LONG).show();  
  39.                 break;  
  40.             case R.id.bananachkb:  
  41.                 if (Banana.isChecked())  
  42.                     Toast.makeText(getApplicationContext(), "Your clicked banana", Toast.LENGTH_LONG).show();  
  43.                 break;  
  44.             case R.id.strawchkb:  
  45.                 if (Straw.isChecked())  
  46.                     Toast.makeText(getApplicationContext(), "Your clicked straw", Toast.LENGTH_LONG).show();  
  47.                 break;  
  48.             case R.id.blueberrychkb:  
  49.                 if (Blueberry.isChecked())  
  50.                     Toast.makeText(getApplicationContext(), "Your clicked blueberry", Toast.LENGTH_LONG).show();  
  51.                 break;  
  52.             case R.id.grapschkb:  
  53.                 if (Grapes.isChecked())  
  54.                     Toast.makeText(getApplicationContext(), "Your clicked grapes", Toast.LENGTH_LONG).show();  
  55.                 break;  
  56.         }  
  57.     }  
  58. }  
Step 8

Here, you will go to run it and select Run-> Run app option.

Android

Here, you will choose Emulator or the devices and it is Nokia Nokia _X.

Android

Step 9

Here, you can see the output.

Android

Now, you will check Apple and afterwards, you will see the given output.

Android

Now, you will check Banana and afterwards, you will see the given output.

Android

 

Ebook Download
View all
Learn
View all