Android - Simple Checkbox Selection

Submitted by: 
Operating System: 
Visitors have accessed this post 541 times.

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="<a href="http://schemas.android.com/apk/res/android"
  3. " rel="nofollow">http://schemas.android.com/apk/res/android"
  4. </a> xmlns:tools="<a href="http://schemas.android.com/tools"
  5. " rel="nofollow">http://schemas.android.com/tools"
  6. </a> android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. tools:context="com.razormist.simplecheckboxselection.MainActivity">
  9.  
  10. <TextView
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_centerInParent="true"
  14. android:layout_alignParentTop="true"
  15. android:layout_marginTop="50dp"
  16. android:fontFamily="serif"
  17. android:textSize="20sp"
  18. android:id="@+id/tv_title"
  19. android:gravity="center"
  20. android:text="What is your favorite Programming Language?" />
  21.  
  22. <RelativeLayout
  23. android:layout_width="match_parent"
  24. android:layout_height="wrap_content"
  25. android:layout_centerInParent="true"
  26. android:layout_below="@+id/tv_title"
  27. android:layout_marginTop="20dp"
  28. android:id="@+id/rl_content">
  29.  
  30.  
  31. <CheckBox
  32. android:id="@+id/cb_c"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:text="C"
  36. android:layout_marginLeft="20dp"
  37. android:textSize="30sp"/>
  38.  
  39. <CheckBox
  40. android:id="@+id/cb_cplus"
  41. android:layout_toRightOf="@+id/cb_c"
  42. android:layout_width="wrap_content"
  43. android:layout_height="wrap_content"
  44. android:text="C++"
  45. android:layout_marginLeft="70dp"
  46. android:textSize="30sp"/>
  47.  
  48. <CheckBox
  49. android:id="@+id/cb_vbnet"
  50. android:layout_toRightOf="@+id/cb_cplus"
  51. android:layout_width="wrap_content"
  52. android:layout_height="wrap_content"
  53. android:text="VB"
  54. android:layout_marginLeft="50dp"
  55. android:textSize="30sp"/>
  56.  
  57. <CheckBox
  58. android:id="@+id/cb_csharp"
  59. android:layout_width="wrap_content"
  60. android:layout_height="wrap_content"
  61. android:text="C#"
  62. android:layout_below="@+id/cb_c"
  63. android:layout_alignLeft="@+id/cb_c"
  64. android:textSize="30sp"/>
  65.  
  66. <CheckBox
  67. android:id="@+id/cb_java"
  68. android:layout_width="wrap_content"
  69. android:layout_height="wrap_content"
  70. android:text="JAVA"
  71. android:layout_below="@+id/cb_cplus"
  72. android:layout_alignLeft="@+id/cb_cplus"
  73. android:textSize="30sp"/>
  74.  
  75. <CheckBox
  76. android:id="@+id/cb_php"
  77. android:layout_width="wrap_content"
  78. android:layout_height="wrap_content"
  79. android:text="PHP"
  80. android:layout_below="@+id/cb_vbnet"
  81. android:layout_alignLeft="@+id/cb_vbnet"
  82. android:textSize="30sp"/>
  83.  
  84. <CheckBox
  85. android:id="@+id/cb_python"
  86. android:layout_width="wrap_content"
  87. android:layout_height="wrap_content"
  88. android:text="PYTHON"
  89. android:layout_below="@+id/cb_csharp"
  90. android:layout_alignLeft="@+id/cb_csharp"
  91. android:textSize="20sp"/>
  92.  
  93. <CheckBox
  94. android:id="@+id/cb_ruby"
  95. android:layout_width="wrap_content"
  96. android:layout_height="wrap_content"
  97. android:text="RUBY"
  98. android:layout_below="@+id/cb_java"
  99. android:layout_alignLeft="@+id/cb_java"
  100. android:textSize="30sp"/>
  101.  
  102. <CheckBox
  103. android:id="@+id/cb_perl"
  104. android:layout_width="wrap_content"
  105. android:layout_height="wrap_content"
  106. android:text="PERL"
  107. android:layout_below="@+id/cb_php"
  108. android:layout_alignLeft="@+id/cb_php"
  109. android:textSize="30sp"/>
  110.  
  111. </RelativeLayout>
  112.  
  113.  
  114. <TextView
  115. android:layout_height="wrap_content"
  116. android:layout_width="wrap_content"
  117. android:id="@+id/tv_display"
  118. android:layout_centerInParent="true"
  119. android:hint="RESULT"
  120. android:layout_below="@+id/rl_content"
  121. android:textSize="30sp"
  122. android:layout_marginTop="50dp"
  123. android:gravity="center"
  124. />
  125.  
  126. android:id="@+id/btn_confirm"
  127. android:layout_marginTop="50dp"
  128. android:ems="12"
  129. android:layout_below="@+id/tv_display"
  130. android:layout_height="wrap_content"
  131. android:layout_width="wrap_content"
  132. android:layout_centerInParent="true"
  133. android:text="Confirm"/>
  134.  
  135.  
  136. </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="<a href="http://schemas.android.com/apk/res/android"
  3. " rel="nofollow">http://schemas.android.com/apk/res/android"
  4. </a> package="com.razormist.simplecheckboxselection">
  5.  
  6. <application
  7. android:allowBackup="true"
  8. android:icon="@mipmap/ic_launcher"
  9. android:label="@string/app_name"
  10. android:roundIcon="@mipmap/ic_launcher_round"
  11. android:supportsRtl="true"
  12. android:theme="@style/AppTheme">
  13. <activity android:name=".MainActivity"
  14. android:configChanges="orientation"
  15. android:screenOrientation="portrait">
  16. <intent-filter>
  17. <action android:name="android.intent.action.MAIN" />
  18.  
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. </application>
  23.  
  24. </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!!!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.