• We just launched and are currently in beta. Join us as we build and grow the community.

Android - Simple Checkbox Selection

mickel2000

SEO Keyword Architect
M Rep
0
0
0
Rep
0
M Vouches
0
0
0
Vouches
0
Posts
65
Likes
115
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
Operating System

Android

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. <

    TextView
  8. android:

    layout_width=

    "wrap_content"
  9. android:

    layout_height=

    "wrap_content"
  10. android:

    layout_centerInParent=

    "true"
  11. android:

    layout_alignParentTop=

    "true"
  12. android:

    layout_marginTop=

    "50dp"
  13. android:

    fontFamily=

    "serif"
  14. android:

    textSize=

    "20sp"
  15. android:

    id=

    "@+id/tv_title"
  16. android:

    gravity=

    "center"
  17. android:

    text=

    "What is your favorite Programming Language?"

    />

  18. <

    RelativeLayout
  19. android:

    layout_width=

    "match_parent"
  20. android:

    layout_height=

    "wrap_content"
  21. android:

    layout_centerInParent=

    "true"
  22. android:

    layout_below=

    "@+id/tv_title"
  23. android:

    layout_marginTop=

    "20dp"
  24. android:

    id=

    "@+id/rl_content"

    >


  25. <

    CheckBox
  26. android:

    id=

    "@+id/cb_c"
  27. android:

    layout_width=

    "wrap_content"
  28. android:

    layout_height=

    "wrap_content"
  29. android:

    text=

    "C"
  30. android:

    layout_marginLeft=

    "20dp"
  31. android:

    textSize=

    "30sp"

    />

  32. <

    CheckBox
  33. android:

    id=

    "@+id/cb_cplus"
  34. android:

    layout_toRightOf=

    "@+id/cb_c"
  35. android:

    layout_width=

    "wrap_content"
  36. android:

    layout_height=

    "wrap_content"
  37. android:

    text=

    "C++"
  38. android:

    layout_marginLeft=

    "70dp"
  39. android:

    textSize=

    "30sp"

    />

  40. <

    CheckBox
  41. android:

    id=

    "@+id/cb_vbnet"
  42. android:

    layout_toRightOf=

    "@+id/cb_cplus"
  43. android:

    layout_width=

    "wrap_content"
  44. android:

    layout_height=

    "wrap_content"
  45. android:

    text=

    "VB"
  46. android:

    layout_marginLeft=

    "50dp"
  47. android:

    textSize=

    "30sp"

    />

  48. <

    CheckBox
  49. android:

    id=

    "@+id/cb_csharp"
  50. android:

    layout_width=

    "wrap_content"
  51. android:

    layout_height=

    "wrap_content"
  52. android:

    text=

    "C#"
  53. android:

    layout_below=

    "@+id/cb_c"
  54. android:

    layout_alignLeft=

    "@+id/cb_c"
  55. android:

    textSize=

    "30sp"

    />

  56. <

    CheckBox
  57. android:

    id=

    "@+id/cb_java"
  58. android:

    layout_width=

    "wrap_content"
  59. android:

    layout_height=

    "wrap_content"
  60. android:

    text=

    "JAVA"
  61. android:

    layout_below=

    "@+id/cb_cplus"
  62. android:

    layout_alignLeft=

    "@+id/cb_cplus"
  63. android:

    textSize=

    "30sp"

    />

  64. <

    CheckBox
  65. android:

    id=

    "@+id/cb_php"
  66. android:

    layout_width=

    "wrap_content"
  67. android:

    layout_height=

    "wrap_content"
  68. android:

    text=

    "PHP"
  69. android:

    layout_below=

    "@+id/cb_vbnet"
  70. android:

    layout_alignLeft=

    "@+id/cb_vbnet"
  71. android:

    textSize=

    "30sp"

    />

  72. <

    CheckBox
  73. android:

    id=

    "@+id/cb_python"
  74. android:

    layout_width=

    "wrap_content"
  75. android:

    layout_height=

    "wrap_content"
  76. android:

    text=

    "PYTHON"
  77. android:

    layout_below=

    "@+id/cb_csharp"
  78. android:

    layout_alignLeft=

    "@+id/cb_csharp"
  79. android:

    textSize=

    "20sp"

    />

  80. <

    CheckBox
  81. android:

    id=

    "@+id/cb_ruby"
  82. android:

    layout_width=

    "wrap_content"
  83. android:

    layout_height=

    "wrap_content"
  84. android:

    text=

    "RUBY"
  85. android:

    layout_below=

    "@+id/cb_java"
  86. android:

    layout_alignLeft=

    "@+id/cb_java"
  87. android:

    textSize=

    "30sp"

    />

  88. <

    CheckBox
  89. android:

    id=

    "@+id/cb_perl"
  90. android:

    layout_width=

    "wrap_content"
  91. android:

    layout_height=

    "wrap_content"
  92. android:

    text=

    "PERL"
  93. android:

    layout_below=

    "@+id/cb_php"
  94. android:

    layout_alignLeft=

    "@+id/cb_php"
  95. android:

    textSize=

    "30sp"

    />

  96. </

    RelativeLayout>


  97. <

    TextView
  98. android:

    layout_height=

    "wrap_content"
  99. android:

    layout_width=

    "wrap_content"
  100. android:

    id=

    "@+id/tv_display"
  101. android:

    layout_centerInParent=

    "true"
  102. android:

    hint=

    "RESULT"
  103. android:

    layout_below=

    "@+id/rl_content"
  104. android:

    textSize=

    "30sp"
  105. android:

    layout_marginTop=

    "50dp"
  106. android:

    gravity=

    "center"
  107. />

  108. <

    Button


  109. android:

    id=

    "@+id/btn_confirm"
  110. android:

    layout_marginTop=

    "50dp"
  111. android:

    ems=

    "12"
  112. android:

    layout_below=

    "@+id/tv_display"
  113. android:

    layout_height=

    "wrap_content"
  114. android:

    layout_width=

    "wrap_content"
  115. android:

    layout_centerInParent=

    "true"
  116. android:

    text=

    "Confirm"

    />


  117. </

    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. <

    application
  5. android:

    allowBackup=

    "true"
  6. android:

    icon=

    "@mipmap/ic_launcher"
  7. android:

    label=

    "@string/app_name"
  8. android:

    roundIcon=

    "@mipmap/ic_launcher_round"
  9. android:

    supportsRtl=

    "true"
  10. android:

    theme=

    "@Style/AppTheme"

    >
  11. <

    activity android:

    name=

    ".MainActivity"
  12. android:

    configChanges=

    "orientation"
  13. android:

    screenOrientation=

    "portrait"

    >
  14. <

    intent-

    filter>
  15. <

    action android:

    name=

    "android.intent.action.MAIN"

    />

  16. <

    category android:

    name=

    "android.intent.category.LAUNCHER"

    />
  17. </

    intent-

    filter>
  18. </

    activity>
  19. </

    application>

  20. </

    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. btn_confirm.setOnClickListener

    (

    new

    View

    .OnClickListener

    (

    )

    {
  3. @Override
  4. public

    void

    onClick(

    View

    v)

    {
  5. ShowResult(

    )

    ;
  6. }
  7. }

    )

    ;

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. StringBuilder result =

    new

    StringBuilder(

    )

    ;

  3. cb_c =

    (

    CheckBox)

    findViewById(

    R.id

    .cb_c

    )

    ;
  4. cb_cplus =

    (

    CheckBox)

    findViewById(

    R.id

    .cb_cplus

    )

    ;
  5. cb_vbnet =

    (

    CheckBox)

    findViewById(

    R.id

    .cb_vbnet

    )

    ;
  6. cb_csharp =

    (

    CheckBox)

    findViewById(

    R.id

    .cb_csharp

    )

    ;
  7. cb_java =

    (

    CheckBox)

    findViewById(

    R.id

    .cb_java

    )

    ;
  8. cb_php =

    (

    CheckBox)

    findViewById(

    R.id

    .cb_php

    )

    ;
  9. cb_python =

    (

    CheckBox)

    findViewById(

    R.id

    .cb_python

    )

    ;
  10. cb_ruby =

    (

    CheckBox)

    findViewById(

    R.id

    .cb_perl

    )

    ;
  11. tv_display =

    (

    TextView)

    findViewById(

    R.id

    .tv_display

    )

    ;

  12. if

    (

    !

    tv_display.getText

    (

    )

    .toString

    (

    )

    .equals

    (

    ""

    )

    )

    {
  13. tv_display.setText

    (

    ""

    )

    ;
  14. }

  15. if

    (

    cb_c.isChecked

    (

    )

    )

    {
  16. result.append

    (

    cb_c.getText

    (

    )

    .toString

    (

    )

    +

    ", "

    )

    ;
  17. }

  18. if

    (

    cb_cplus.isChecked

    (

    )

    )

    {
  19. result.append

    (

    cb_cplus.getText

    (

    )

    .toString

    (

    )

    +

    ", "

    )

    ;
  20. }

  21. if

    (

    cb_vbnet.isChecked

    (

    )

    )

    {
  22. result.append

    (

    cb_vbnet.getText

    (

    )

    .toString

    (

    )

    +

    ", "

    )

    ;
  23. }

  24. if

    (

    cb_csharp.isChecked

    (

    )

    )

    {
  25. result.append

    (

    cb_csharp.getText

    (

    )

    .toString

    (

    )

    +

    ", "

    )

    ;
  26. }

  27. if

    (

    cb_java.isChecked

    (

    )

    )

    {
  28. result.append

    (

    cb_java.getText

    (

    )

    .toString

    (

    )

    +

    ", "

    )

    ;
  29. }

  30. if

    (

    cb_php.isChecked

    (

    )

    )

    {
  31. result.append

    (

    cb_php.getText

    (

    )

    .toString

    (

    )

    +

    ", "

    )

    ;
  32. }

  33. if

    (

    cb_python.isChecked

    (

    )

    )

    {
  34. result.append

    (

    cb_python.getText

    (

    )

    .toString

    (

    )

    +

    ", "

    )

    ;
  35. }

  36. if

    (

    cb_ruby.isChecked

    (

    )

    )

    {
  37. result.append

    (

    cb_ruby.getText

    (

    )

    .toString

    (

    )

    +

    ", "

    )

    ;
  38. }


  39. if

    (

    result.length

    (

    )

    >

    0

    )

    {
  40. result.setLength

    (

    Math

    .max

    (

    result.length

    (

    )

    -

    2

    , 0

    )

    )

    ;
  41. tv_display.setText

    (

    "I Like: "

    +

    result)

    ;
  42. }

    else

    {
  43. tv_display.setText

    (

    ""

    )

    ;
  44. }
  45. }

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.


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

334,719

334,727

Top