Store multiple checkbox selections at one time.
Checkbox can select or check multiple values at single time this feature called as Access Multiple selection on button click. So here is the step by step tutorial for How to get multiple checked checkbox value in android.
How to get multiple checked checkbox value in android.
Code for MainActivity.java file.
package com.android_examples.com.getmultiplecheckbox; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.Switch; import android.widget.Toast; public class MainActivity extends Activity { CheckBox ONE,TWO,THREE; Button MultiData; Integer First,Second,Third; Integer Total; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ONE = (CheckBox)findViewById(R.id.checkBox1); TWO = (CheckBox)findViewById(R.id.checkBox2); THREE = (CheckBox)findViewById(R.id.checkBox3); MultiData = (Button)findViewById(R.id.button1); MultiData.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(ONE.isChecked()) { First = 1; Total = First; } if(TWO.isChecked()) { Second = 2; Total = Second; } if(THREE.isChecked()) { Third = 4; Total = Third; } if(ONE.isChecked() && TWO.isChecked()) { Total = 3; } if(ONE.isChecked() && THREE.isChecked()) { Total = 5; } if(THREE.isChecked() && TWO.isChecked()) { Total = 6; } if(THREE.isChecked() && ONE.isChecked() && TWO.isChecked()) { Total = 7; } switch(Total) { case 1: Toast.makeText(MainActivity.this, "12th Pass Selected", Toast.LENGTH_SHORT).show(); break; case 2: Toast.makeText(MainActivity.this, "Graduation Selected", Toast.LENGTH_SHORT).show(); break; case 4: Toast.makeText(MainActivity.this, "Post Graduation Selected", Toast.LENGTH_SHORT).show(); break; case 3: Toast.makeText(MainActivity.this, "12th Pass & Graduation Selected", Toast.LENGTH_SHORT).show(); break; case 5: Toast.makeText(MainActivity.this, "12th Pass & Post Graduation Selected", Toast.LENGTH_SHORT).show(); break; case 6: Toast.makeText(MainActivity.this, "Graduation & Post Graduation Selected", Toast.LENGTH_SHORT).show(); break; case 7: Toast.makeText(MainActivity.this, "12th Pass & Graduation & Post Graduation Selected", Toast.LENGTH_SHORT).show(); break; } } }); } }
Code for activity_main.xml layout file.
<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="com.android_examples.com.getmultiplecheckbox.MainActivity" > <CheckBox android:id="@+id/checkBox2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/checkBox1" android:layout_centerHorizontal="true" android:layout_marginTop="20dp" android:text="Graduation" /> <CheckBox android:id="@+id/checkBox3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/checkBox2" android:layout_below="@+id/checkBox2" android:layout_marginTop="21dp" android:text="Post Graduation" /> <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/checkBox2" android:layout_alignParentTop="true" android:layout_marginTop="66dp" android:text="12th Pass" /> <Button android:id="@+id/button1" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/checkBox3" android:layout_centerHorizontal="true" android:layout_marginTop="37dp" android:text="Select Multiple or Single CheckBox value" /> </RelativeLayout>
Screenshot: