Introduction
This blog explains how to develop a checkbox app in an Android Application, using Android Studio.
Requirements
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.
Step 2
Here, you can create Your Application name and choose where your project is stored on the location and click Next button.
Now, we can select the version of Android. It is Target Android Devices.
Step 3
Here, we can add the activity, more activity is available, and click Next button.
Now, we can write the activity name and click the Finish button.
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.
Now, we can see the Graphical User Interface design.
Step 5
Now, you will go to the resource folder and select the String.xml. Subsequently, you need to add the string value.
srings.xml code
- <resources>
- <string name="app_name">CheckBoxApp</string>
- <string name="Apple">APPLE</string>
- <string name="Banana">BANANA</string>
- <string name="BluBerry">BLUEBERRY</string>
- <string name="Grapes">GRAPES</string>
- <string name="Straw">STRAW</string>
- </resources>
Step 6
Here, you need to build on design and write the .XML code.
activity_main.xml code.
- <?xml version="1.0" encoding="utf-8"?>
- <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">
- <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" />
- <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">
- <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" />
- <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" />
- <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" />
- <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" />
- <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" />
- </LinearLayout>
- </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.
Now, we can see MainActivity.java code.
- package xyz.rvconstructions.www.checkboxapp;
-
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.Button;
-
- import android.widget.CheckBox;
- import android.widget.Toast;
-
- public class MainActivity extends AppCompatActivity implements View.OnClickListener {
- CheckBox Abble, Banana, Straw, Blueberry, Grapes;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- Abble = (CheckBox) findViewById(R.id.applechkb);
- Abble.setOnClickListener(this);
- Banana = (CheckBox) findViewById(R.id.bananachkb);
- Banana.setOnClickListener(this);
- Straw = (CheckBox) findViewById(R.id.strawchkb);
- Straw.setOnClickListener(this);
- Blueberry = (CheckBox) findViewById(R.id.blueberrychkb);
- Blueberry.setOnClickListener(this);
- Grapes = (CheckBox) findViewById(R.id.grapschkb);
- Grapes.setOnClickListener(this);
- }
- @Override
- public void onClick(View view) {
- switch (view.getId()) {
- case R.id.applechkb:
- if (Abble.isChecked())
- Toast.makeText(getApplicationContext(), "Your clicked abble", Toast.LENGTH_LONG).show();
- break;
- case R.id.bananachkb:
- if (Banana.isChecked())
- Toast.makeText(getApplicationContext(), "Your clicked banana", Toast.LENGTH_LONG).show();
- break;
- case R.id.strawchkb:
- if (Straw.isChecked())
- Toast.makeText(getApplicationContext(), "Your clicked straw", Toast.LENGTH_LONG).show();
- break;
- case R.id.blueberrychkb:
- if (Blueberry.isChecked())
- Toast.makeText(getApplicationContext(), "Your clicked blueberry", Toast.LENGTH_LONG).show();
- break;
- case R.id.grapschkb:
- if (Grapes.isChecked())
- Toast.makeText(getApplicationContext(), "Your clicked grapes", Toast.LENGTH_LONG).show();
- break;
- }
- }
- }
Step 8
Here, you will go to run it and select Run-> Run app option.
Here, you will choose Emulator or the devices and it is Nokia Nokia _X.
Step 9
Here, you can see the output.
Now, you will check Apple and afterwards, you will see the given output.
Now, you will check Banana and afterwards, you will see the given output.