Android - Simple Checkbox Selection
Submitted by razormist on Monday, January 22, 2018 - 17:08.
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.
Then after that write these block of codes inside onCreate method, this will trigger the methods when the button is click.
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!!!
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.- <?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"
- tools:context="com.razormist.simplecheckboxselection.MainActivity">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:layout_alignParentTop="true"
- android:layout_marginTop="50dp"
- android:fontFamily="serif"
- android:textSize="20sp"
- android:id="@+id/tv_title"
- android:gravity="center"
- android:text="What is your favorite Programming Language?" />
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:layout_below="@+id/tv_title"
- android:layout_marginTop="20dp"
- android:id="@+id/rl_content">
- <CheckBox
- android:id="@+id/cb_c"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="C"
- android:layout_marginLeft="20dp"
- android:textSize="30sp"/>
- <CheckBox
- android:id="@+id/cb_cplus"
- android:layout_toRightOf="@+id/cb_c"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="C++"
- android:layout_marginLeft="70dp"
- android:textSize="30sp"/>
- <CheckBox
- android:id="@+id/cb_vbnet"
- android:layout_toRightOf="@+id/cb_cplus"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="VB"
- android:layout_marginLeft="50dp"
- android:textSize="30sp"/>
- <CheckBox
- android:id="@+id/cb_csharp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="C#"
- android:layout_below="@+id/cb_c"
- android:layout_alignLeft="@+id/cb_c"
- android:textSize="30sp"/>
- <CheckBox
- android:id="@+id/cb_java"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="JAVA"
- android:layout_below="@+id/cb_cplus"
- android:layout_alignLeft="@+id/cb_cplus"
- android:textSize="30sp"/>
- <CheckBox
- android:id="@+id/cb_php"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="PHP"
- android:layout_below="@+id/cb_vbnet"
- android:layout_alignLeft="@+id/cb_vbnet"
- android:textSize="30sp"/>
- <CheckBox
- android:id="@+id/cb_python"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="PYTHON"
- android:layout_below="@+id/cb_csharp"
- android:layout_alignLeft="@+id/cb_csharp"
- android:textSize="20sp"/>
- <CheckBox
- android:id="@+id/cb_ruby"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="RUBY"
- android:layout_below="@+id/cb_java"
- android:layout_alignLeft="@+id/cb_java"
- android:textSize="30sp"/>
- <CheckBox
- android:id="@+id/cb_perl"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="PERL"
- android:layout_below="@+id/cb_php"
- android:layout_alignLeft="@+id/cb_php"
- android:textSize="30sp"/>
- </RelativeLayout>
- <TextView
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:id="@+id/tv_display"
- android:layout_centerInParent="true"
- android:hint="RESULT"
- android:layout_below="@+id/rl_content"
- android:textSize="30sp"
- android:layout_marginTop="50dp"
- android:gravity="center"
- />
- android:id="@+id/btn_confirm"
- android:layout_marginTop="50dp"
- android:ems="12"
- android:layout_below="@+id/tv_display"
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:layout_centerInParent="true"
- android:text="Confirm"/>
- </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.- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.razormist.simplecheckboxselection">
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <activity android:name=".MainActivity"
- android:configChanges="orientation"
- android:screenOrientation="portrait">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </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.- private CheckBox cb_c, cb_cplus, cb_vbnet, cb_csharp, cb_java, cb_php, cb_python, cb_ruby, cb_pel;
- private TextView tv_display;
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.
- public void ShowResult() {
- StringBuilder result = new StringBuilder();
- cb_c = (CheckBox) findViewById(R.id.cb_c);
- cb_cplus = (CheckBox) findViewById(R.id.cb_cplus);
- cb_vbnet = (CheckBox) findViewById(R.id.cb_vbnet);
- cb_csharp = (CheckBox) findViewById(R.id.cb_csharp);
- cb_java = (CheckBox) findViewById(R.id.cb_java);
- cb_php = (CheckBox) findViewById(R.id.cb_php);
- cb_python = (CheckBox) findViewById(R.id.cb_python);
- cb_ruby = (CheckBox) findViewById(R.id.cb_perl);
- tv_display = (TextView) findViewById(R.id.tv_display);
- if (!tv_display.getText().toString().equals("")) {
- tv_display.setText("");
- }
- if (cb_c.isChecked()) {
- result.append(cb_c.getText().toString() + ", ");
- }
- if (cb_cplus.isChecked()) {
- result.append(cb_cplus.getText().toString() + ", ");
- }
- if (cb_vbnet.isChecked()) {
- result.append(cb_vbnet.getText().toString() + ", ");
- }
- if (cb_csharp.isChecked()) {
- result.append(cb_csharp.getText().toString() + ", ");
- }
- if (cb_java.isChecked()) {
- result.append(cb_java.getText().toString() + ", ");
- }
- if (cb_php.isChecked()) {
- result.append(cb_php.getText().toString() + ", ");
- }
- if (cb_python.isChecked()) {
- result.append(cb_python.getText().toString() + ", ");
- }
- if (cb_ruby.isChecked()) {
- result.append(cb_ruby.getText().toString() + ", ");
- }
- if (result.length() > 0) {
- tv_display.setText("I Like: " + result);
- }else {
- tv_display.setText("");
- }
- }
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.
Add new comment
- 357 views