title: how to fetch multiple checkboxes value in android
body: <p>In Android My requirement is :-</p>
  
  <p>I have multiple checkboxes and one button..after selection when You click the button it will display as text  which boxes you have selected.</p>
  
  <p>So I have <strong>activity_test.xml</strong></p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
     android:id="@+id/check"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      android:background="#ffffff"
       &gt; 
  
     &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content" 
           android:id="@+id/lay1"&gt;
  
          &lt;CheckBox
              android:id="@+id/checkBox1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="apple"
              android:tag="apple"
              android:onClick="onCheckboxClicked" /&gt;
  
          &lt;CheckBox
              android:id="@+id/checkBox2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="banana" 
              android:tag="banana" 
              android:onClick="onCheckboxClicked"/&gt;
  
          &lt;CheckBox
              android:id="@+id/checkBox3"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="water_milon" 
              android:tag="water_milon"
              android:onClick="onCheckboxClicked"/&gt;
  
      &lt;/LinearLayout&gt;
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content" 
           android:id="@+id/lay2"&gt;
  
          &lt;CheckBox
              android:id="@+id/checkBox4"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="guava" 
              android:tag="guava" 
              android:onClick="onCheckboxClicked"/&gt;
  
          &lt;CheckBox
              android:id="@+id/checkBox5"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="panir" 
               android:tag="panir"
               android:onClick="onCheckboxClicked"/&gt;
  
          &lt;CheckBox
              android:id="@+id/checkBox6"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="chatni" 
               android:tag="chatni"
               android:onClick="onCheckboxClicked"/&gt;
  
      &lt;/LinearLayout&gt;
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content" 
           android:id="@+id/lay3"&gt;
  
          &lt;CheckBox
              android:id="@+id/checkBox7"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="college"
               android:tag="college"
               android:onClick="onCheckboxClicked" /&gt;
  
          &lt;CheckBox
              android:id="@+id/checkBox8"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="school" 
               android:tag="school"
               android:onClick="onCheckboxClicked"/&gt;
  
          &lt;CheckBox
              android:id="@+id/checkBox9"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="primary" 
               android:tag="primary"
               android:onClick="onCheckboxClicked"/&gt;
  
      &lt;/LinearLayout&gt;
  
      &lt;Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Button" 
         /&gt;
  
      &lt;TextView
          android:id="@+id/textView1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
         /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>and the java class file is:--</p>
  
  <pre><code>public class MainActivity_test extends Activity {
  
  
  
      CheckBox chk1,chk2,chk3,chk4,chk5,chk6,chk7,chk8,chk9;
      Button btn;
  
      TextView txt;
      ArrayList&lt;String&gt; list ;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_test);
  
  
          list = new ArrayList&lt;String&gt;();
  
  
          chk1=(CheckBox)findViewById(R.id.checkBox1);  
          chk2=(CheckBox)findViewById(R.id.checkBox2);  
          chk3=(CheckBox)findViewById(R.id.checkBox3);  
          chk4=(CheckBox)findViewById(R.id.checkBox4);  
          chk5=(CheckBox)findViewById(R.id.checkBox5);  
          chk6=(CheckBox)findViewById(R.id.checkBox6);  
          chk7=(CheckBox)findViewById(R.id.checkBox7);  
          chk8=(CheckBox)findViewById(R.id.checkBox8);  
          chk9=(CheckBox)findViewById(R.id.checkBox9);  
          txt = (TextView)findViewById(R.id.textView1);
          btn =(Button)findViewById(R.id.button1);  
          addListenerOnButton();
      }
  
      public void addListenerOnButton() {
  
  
          btn.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View arg0) {
  
                   for (String str : list) {
  
  
                       txt.setText("you have selected:--"+str);
  
                       }
  
  
              }
  
          });
  
      }
  
  
  
      public void onCheckboxClicked(View view) {
  
          boolean checked = ((CheckBox) view).isChecked();
  
          switch(view.getId()) {
          case R.id.checkBox1:
               list.add(chk1.getTag().toString());
  
  
          break;
          case R.id.checkBox2:
              list.add(chk2.getTag().toString());
  
          break;
  
          case R.id.checkBox3:
              list.add(chk3.getTag().toString());
  
              break;
          case R.id.checkBox4:
              list.add(chk4.getTag().toString());
  
              break;
          case R.id.checkBox5:
              list.add(chk5.getTag().toString());
  
              break;
          case R.id.checkBox6:
              list.add(chk6.getTag().toString());
  
              break;
          case R.id.checkBox7:
              list.add(chk7.getTag().toString());
  
              break;
          case R.id.checkBox8:
              list.add(chk8.getTag().toString());
  
              break;
          case R.id.checkBox9:
              list.add(chk9.getTag().toString());
  
              break;
  
          }
          }
  
  
  }
  </code></pre>
  
  <p>But here the problem is when I click the button it is displaying only the last selected item..Not the all selected items...where is the problem??</p>