Android - Simple Checkbox Selection

Operating System
In this tutorial we will try to create a Simple Checkbox Selection using Android. Android is a widely-adopted open-source project. Android is an open source so that developer find it easy to develop and expand new features. Android is available to any devices such as TV, phones, watches etc. So now let's do the coding..... Getting Started: First you will have to download & install the Android Development IDE (Android Studio or Eclipse). Android Studio is an open source development feel free to develop your things. Here's the link for the Android Studio https://developer.android.com/studio/index.html.

Layout Design

We will now create the design for the application, first locate the activity_main.xml and click text to view the script. Then copy and paste the code below.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:tools="http://schemas.android.com/tools"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent"
  6.     tools:context="com.razormist.simplecheckboxselection.MainActivity">
  7.  
  8.     <TextView
  9.         android:layout_width="wrap_content"
  10.         android:layout_height="wrap_content"
  11.         android:layout_centerInParent="true"
  12.         android:layout_alignParentTop="true"
  13.         android:layout_marginTop="50dp"
  14.         android:fontFamily="serif"
  15.         android:textSize="20sp"
  16.         android:id="@+id/tv_title"
  17.         android:gravity="center"
  18.         android:text="What is your favorite Programming Language?" />
  19.  
  20.     <RelativeLayout
  21.         android:layout_width="match_parent"
  22.         android:layout_height="wrap_content"
  23.         android:layout_centerInParent="true"
  24.         android:layout_below="@+id/tv_title"
  25.         android:layout_marginTop="20dp"
  26.         android:id="@+id/rl_content">
  27.  
  28.  
  29.         <CheckBox
  30.             android:id="@+id/cb_c"
  31.             android:layout_width="wrap_content"
  32.             android:layout_height="wrap_content"
  33.             android:text="C"
  34.             android:layout_marginLeft="20dp"
  35.             android:textSize="30sp"/>
  36.  
  37.         <CheckBox
  38.             android:id="@+id/cb_cplus"
  39.             android:layout_toRightOf="@+id/cb_c"
  40.             android:layout_width="wrap_content"
  41.             android:layout_height="wrap_content"
  42.             android:text="C++"
  43.             android:layout_marginLeft="70dp"
  44.             android:textSize="30sp"/>
  45.  
  46.         <CheckBox
  47.             android:id="@+id/cb_vbnet"
  48.             android:layout_toRightOf="@+id/cb_cplus"
  49.             android:layout_width="wrap_content"
  50.             android:layout_height="wrap_content"
  51.             android:text="VB"
  52.             android:layout_marginLeft="50dp"
  53.             android:textSize="30sp"/>
  54.  
  55.         <CheckBox
  56.             android:id="@+id/cb_csharp"
  57.             android:layout_width="wrap_content"
  58.             android:layout_height="wrap_content"
  59.             android:text="C#"
  60.             android:layout_below="@+id/cb_c"
  61.             android:layout_alignLeft="@+id/cb_c"
  62.             android:textSize="30sp"/>
  63.  
  64.         <CheckBox
  65.             android:id="@+id/cb_java"
  66.             android:layout_width="wrap_content"
  67.             android:layout_height="wrap_content"
  68.             android:text="JAVA"
  69.             android:layout_below="@+id/cb_cplus"
  70.             android:layout_alignLeft="@+id/cb_cplus"
  71.             android:textSize="30sp"/>
  72.  
  73.         <CheckBox
  74.             android:id="@+id/cb_php"
  75.             android:layout_width="wrap_content"
  76.             android:layout_height="wrap_content"
  77.             android:text="PHP"
  78.             android:layout_below="@+id/cb_vbnet"
  79.             android:layout_alignLeft="@+id/cb_vbnet"
  80.             android:textSize="30sp"/>
  81.  
  82.         <CheckBox
  83.             android:id="@+id/cb_python"
  84.             android:layout_width="wrap_content"
  85.             android:layout_height="wrap_content"
  86.             android:text="PYTHON"
  87.             android:layout_below="@+id/cb_csharp"
  88.             android:layout_alignLeft="@+id/cb_csharp"
  89.             android:textSize="20sp"/>
  90.  
  91.         <CheckBox
  92.             android:id="@+id/cb_ruby"
  93.             android:layout_width="wrap_content"
  94.             android:layout_height="wrap_content"
  95.             android:text="RUBY"
  96.             android:layout_below="@+id/cb_java"
  97.             android:layout_alignLeft="@+id/cb_java"
  98.             android:textSize="30sp"/>
  99.  
  100.         <CheckBox
  101.             android:id="@+id/cb_perl"
  102.             android:layout_width="wrap_content"
  103.             android:layout_height="wrap_content"
  104.             android:text="PERL"
  105.             android:layout_below="@+id/cb_php"
  106.             android:layout_alignLeft="@+id/cb_php"
  107.             android:textSize="30sp"/>
  108.  
  109.     </RelativeLayout>
  110.  
  111.  
  112.     <TextView
  113.         android:layout_height="wrap_content"
  114.         android:layout_width="wrap_content"
  115.         android:id="@+id/tv_display"
  116.         android:layout_centerInParent="true"
  117.         android:hint="RESULT"
  118.         android:layout_below="@+id/rl_content"
  119.         android:textSize="30sp"
  120.         android:layout_marginTop="50dp"
  121.         android:gravity="center"
  122.         />
  123.  
  124.     <Button
  125.         android:id="@+id/btn_confirm"
  126.         android:layout_marginTop="50dp"
  127.         android:ems="12"
  128.         android:layout_below="@+id/tv_display"
  129.         android:layout_height="wrap_content"
  130.         android:layout_width="wrap_content"
  131.         android:layout_centerInParent="true"
  132.         android:text="Confirm"/>
  133.  
  134.  
  135. </RelativeLayout>

Android Manifest File

The Android Manifest file provides essential information about your app to the Android system in which the system must required before running the code.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.     package="com.razormist.simplecheckboxselection">
  4.  
  5.     <application
  6.         android:allowBackup="true"
  7.         android:icon="@mipmap/ic_launcher"
  8.         android:label="@string/app_name"
  9.         android:roundIcon="@mipmap/ic_launcher_round"
  10.         android:supportsRtl="true"
  11.         android:theme="@style/AppTheme">
  12.         <activity android:name=".MainActivity"
  13.             android:configChanges="orientation"
  14.             android:screenOrientation="portrait">
  15.             <intent-filter>
  16.                 <action android:name="android.intent.action.MAIN" />
  17.  
  18.                 <category android:name="android.intent.category.LAUNCHER" />
  19.             </intent-filter>
  20.         </activity>
  21.     </application>
  22.  
  23. </manifest>

The Main Function

This code contains the main function of the application. This code will display the value of the checkbox that the user checked. To start with first locate your MainActivity java file and open it, then write these some important variables inside the MainActivity class.
  1. private CheckBox cb_c, cb_cplus, cb_vbnet, cb_csharp, cb_java, cb_php, cb_python, cb_ruby, cb_pel;
  2.     private TextView tv_display;
  3.     private Button btn_confirm;
Then after that write these block of codes inside onCreate method, this will trigger the methods when the button is click.
  1. btn_confirm = (Button) findViewById(R.id.btn_confirm);
  2.  
  3.         btn_confirm.setOnClickListener(new View.OnClickListener() {
  4.             @Override
  5.             public void onClick(View v) {
  6.                 ShowResult();
  7.             }
  8.         });
Next is to create the function that store the checkbox value then will display it at the same time. Write this method called ShowResult() inside the class.
  1.  public void ShowResult() {
  2.  
  3.         StringBuilder result = new StringBuilder();
  4.  
  5.         cb_c = (CheckBox) findViewById(R.id.cb_c);
  6.         cb_cplus = (CheckBox) findViewById(R.id.cb_cplus);
  7.         cb_vbnet = (CheckBox) findViewById(R.id.cb_vbnet);
  8.         cb_csharp = (CheckBox) findViewById(R.id.cb_csharp);
  9.         cb_java = (CheckBox) findViewById(R.id.cb_java);
  10.         cb_php = (CheckBox) findViewById(R.id.cb_php);
  11.         cb_python = (CheckBox) findViewById(R.id.cb_python);
  12.         cb_ruby = (CheckBox) findViewById(R.id.cb_perl);
  13.         tv_display = (TextView) findViewById(R.id.tv_display);
  14.  
  15.         if (!tv_display.getText().toString().equals("")) {
  16.             tv_display.setText("");
  17.         }
  18.  
  19.         if (cb_c.isChecked()) {
  20.             result.append(cb_c.getText().toString() + ", ");
  21.         }
  22.  
  23.         if (cb_cplus.isChecked()) {
  24.             result.append(cb_cplus.getText().toString() + ", ");
  25.         }
  26.  
  27.         if (cb_vbnet.isChecked()) {
  28.             result.append(cb_vbnet.getText().toString() + ", ");
  29.         }
  30.  
  31.         if (cb_csharp.isChecked()) {
  32.             result.append(cb_csharp.getText().toString() + ", ");
  33.         }
  34.  
  35.         if (cb_java.isChecked()) {
  36.             result.append(cb_java.getText().toString() + ", ");
  37.         }
  38.  
  39.         if (cb_php.isChecked()) {
  40.             result.append(cb_php.getText().toString() + ", ");
  41.         }
  42.  
  43.         if (cb_python.isChecked()) {
  44.             result.append(cb_python.getText().toString() + ", ");
  45.         }
  46.  
  47.         if (cb_ruby.isChecked()) {
  48.             result.append(cb_ruby.getText().toString() + ", ");
  49.         }
  50.  
  51.  
  52.         if (result.length() > 0) {
  53.             result.setLength(Math.max(result.length() - 2, 0));
  54.             tv_display.setText("I Like: " + result);
  55.         }else {
  56.             tv_display.setText("");
  57.         }
  58.     }
Try to run the app and see if it worked. There you have it we create a Simple Checkbox Selection using Android. I hope that this tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Tags

Add new comment