Showing posts with label CheckBox. Show all posts
Showing posts with label CheckBox. Show all posts

Sunday 2 November 2014

Android Tutorial 19 : Adding Check Boxes In Alert Dialog

We can also add check boxes in Alert Dialog and we can check multiple, it is used when we want mutiple option to be selected.

Handling the Click Events on CheckBoxes:

when a Checkbox is clocked means it is checked or unchecked obClick() method is called and the index of the selected item is passed, we can use this index to get the item which is checked




Create an Object of AlertDialog

AlertDialog dialog; 

//following code will be in your activity.java file 

final CharSequence[] items = {" Easy "," Medium "," Hard "," Very Hard "};
                // arraylist to keep the selected items
                final ArrayList seletedItems=new ArrayList();
              
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Select The Difficulty Level");
                builder.setMultiChoiceItems(items, null,
                        new DialogInterface.OnMultiChoiceClickListener() {

                 // indexSelected contains the index of item (of which checkbox checked)
                 @Override
                 public void onClick(DialogInterface dialog, int indexSelected,
                         boolean isChecked) {
                     if (isChecked) {
                         // If the user checked the item, add it to the selected items

                         // write your code when user checked the checkbox 
                         seletedItems.add(indexSelected);
                     } else if (seletedItems.contains(indexSelected)) {
                         // Else, if the item is already in the array, remove it 

                         // write your code when user Uchecked the checkbox 
                         seletedItems.remove(Integer.valueOf(indexSelected));
                     }
                 }
             })
              // Set the action buttons
             .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                 @Override
                 public void onClick(DialogInterface dialog, int id) {
                     //  Your code when user clicked on OK
                     //  You can write the code  to save the selected item here

                   
                 }
             })
             .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                 @Override
                 public void onClick(DialogInterface dialog, int id) {
                    //  Your code when user clicked on Cancel
                  
                 }
             });
      
                dialog = builder.create();//
AlertDialog dialog; create like this outside onClick
                dialog.show();
        }

Saturday 1 November 2014

Android Tutorial 8 : Android CheckBox Example

A checkbox is a specific type of two-states button that can be either checked or unchecked.
We will learn how to use checkbox in this tutorial.


Note: You should  know how to use Buttons and TextViews , if  you do not know then read this post  Using Buttons and TextViews in Android.

Create a new project.
Name your activity "CheckBoxActivity"


Editing main.xml file :


open main.xml file and  add TextViews, CheckBoxes and Buttons, it should like like below. (you can just copy the below code and paste in main.xml file)





main.xml


  <LinearLayout 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:orientation="vertical" >

   
    
    
     <TextView
         android:text="Choose Your Hobies"
         android:layout_marginTop="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

    
     <CheckBox
        android:id="@+id/chkReading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Reading" />

    <CheckBox
        android:id="@+id/chkSong"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Listening Songs"
        android:checked="true" />

    <CheckBox
        android:id="@+id/chkPLay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Playing" />
    
    <CheckBox
        android:id="@+id/chkSing"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Singing" />
    
    <CheckBox
        android:id="@+id/chkDance"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Dancing" />
    
    
     <TextView
         android:id="@+id/result"
         android:textSize="20dp"
         android:layout_marginTop="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

    <Button
        android:id="@+id/btnDisplay"
        android:layout_marginTop="20dp"
        android:gravity="center_horizontal"
        android:layout_gravity="center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="        OK        " />

</LinearLayout>


Thing To Note

 android:checked="true"         you can initially checked any check box using this tag. see  in fig.
 " Listening Song" is already checked.

Editing CheckBoxActivity  file


CheckBoxActivity.java



       public class CheckBoxActivity extends Activity
       {

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView textHobbies=(TextView)findViewById(R.id.result);
// getting refference of check boxes
final CheckBox reading=(CheckBox)findViewById(R.id.chkReading);
final CheckBox playing=(CheckBox)findViewById(R.id.chkReading);
final CheckBox listening=(CheckBox)findViewById(R.id.chkPLay);
final CheckBox singing=(CheckBox)findViewById(R.id.chkSing);
final CheckBox dancing=(CheckBox)findViewById(R.id.chkDance);
// get reffrence of Button
Button button=(Button)findViewById(R.id.btnDisplay);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String hobbies="";
if(reading.isChecked())
hobbies+="Reading";
if(playing.isChecked())
hobbies+="  Playing";
if(listening.isChecked())
hobbies+="  Listening Music";
if(singing.isChecked())
hobbies+="  Singing";
if(dancing.isChecked())
hobbies+="  Dancing";
  textHobbies.setText("Your Hobbies are : "+hobbies) ;
}
});
}

    
    }

Point to Note:

checkBoxObject.isChecked();
isChecked() method returns true if the checkBox is checked , and false otherwise.

Now run your project , do some activity(check or unceck checkboxes)
You will see the following.



Hope you learned and enjoyed
Comments and questions are welcome