Thursday, 18 February 2016

Example for CheckBox in Android?

CheckBox:

               CheckBox is used to select multiple selectios among multiple options.





Step1> Create a new project with the name of CheckBoxTest and give a  Activity name as MainActivity.java

Step2> Create a xml file with the name of activity.xml

Step3> Write the below code into the activity.xml file


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 
     
     <LinearLayout android:layout_height="0dp"
         android:layout_weight="0.2"
         android:layout_width="match_parent">

         <TextView android:layout_height="match_parent"
             android:layout_width="match_parent"
             android:text="Select your favourite activity?"
             android:textColor="#FFFFFF"
             android:textSize="30sp"/>
       
     </LinearLayout>
 
 
     <LinearLayout android:layout_height="0dp"
         android:layout_weight="0.2"
         android:layout_width="match_parent">
       
         <CheckBox
             android:id="@+id/chkCricket"
              android:layout_height="match_parent"
             android:layout_width="match_parent" android:text="Cricket"
             android:textColor="#FFFFFF"
             android:textSize="25sp"/>
       
     </LinearLayout>

   
     <LinearLayout android:layout_height="0dp"
         android:layout_weight="0.2"
         android:layout_width="match_parent">
       
         <CheckBox
             android:id="@+id/chkReading"
              android:layout_height="match_parent"
             android:layout_width="match_parent" android:text="Reading"
             android:textColor="#FFFFFF"
             android:textSize="25sp"/>
       
     </LinearLayout>

   
       
     <LinearLayout android:layout_height="0dp"
         android:layout_weight="0.2"
         android:layout_width="match_parent">
       
         <CheckBox
             android:id="@+id/chkMusic"
              android:layout_height="match_parent"
             android:layout_width="match_parent"
             android:text="Music"
             android:textColor="#FFFFFF"
             android:textSize="25sp"/>
       
     </LinearLayout>
   
   
   
     <LinearLayout android:layout_height="0dp"
         android:layout_weight="0.2" android:gravity="center"
         android:layout_width="match_parent">
       
         <Button
             android:id="@+id/btnSubmit"
              android:layout_height="match_parent"
             android:layout_width="match_parent"
             android:text="Submit"
             android:textColor="#FFFFFF"
             android:textSize="25sp"/>
       
     </LinearLayout>


</LinearLayout>




Step4> Write the below code into the MainActivity.java file.



package com.example.checkboxtest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);

     
        final CheckBox chkCricket=(CheckBox)findViewById(R.id.chkCricket);
        final CheckBox chkReading=(CheckBox)findViewById(R.id.chkReading);
        final CheckBox chkMusic=(CheckBox)findViewById(R.id.chkMusic);
     
        Button btnSubmit=(Button)findViewById(R.id.btnSubmit);
     
        chkCricket.setOnCheckedChangeListener(new OnCheckedChangeListener()
        {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
                  if(isChecked)
                  {
                   Toast.makeText(getApplicationContext(), "if cond. Cricket :"+isChecked ,   2000).show();
                  }
                  else
                 {
                           Toast.makeText(getApplicationContext(), "else cond. Cricket:"+isChecked , 2000).show();
                  }
}
});

        chkReading.setOnCheckedChangeListener(new OnCheckedChangeListener()
        {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
             if(isChecked)
            {
                   Toast.makeText(getApplicationContext(), "if  condi. Reading:"+isChecked , 2000).show();
           }
          else
         {
              Toast.makeText(getApplicationContext(), "else condi. Reading:"+isChecked , 2000).show();
           }
}
});
        chkMusic.setOnCheckedChangeListener(new OnCheckedChangeListener()
        {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
                   if(isChecked)
                  {
    Toast.makeText(getApplicationContext(), "if  condi. Music:"+isChecked , 2000).show();
                   }
                  else
                  {
                             Toast.makeText(getApplicationContext(), "else condi. Music:"+isChecked , 2000).show();
                   }
}
});
     
        btnSubmit.setOnClickListener(new OnClickListener()
        {
@Override
public void onClick(View v)
{
Toast.makeText(getApplicationContext(), "Cricket:"+chkCricket.isChecked()+"\n Music:"+chkMusic.isChecked()+"\n Reading:"+chkReading.isChecked(), 2000).show();
}
});
 
        }

 }

Step 5> Define Activity in AndroidManifest.xml file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.checkboxtest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        >
        <activity
            android:name="com.example.checkboxtest.MainActivity"
            android:label="@string/app_name" >

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>



OutPut:

























No comments:

Post a Comment