Wednesday 26 August 2015

Multiple CheckBoxes in Android

activity_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"
   
tools:context=".MainActivity"
   
android:orientation="vertical"
   
android:id="@+id/hello">


    <
CheckBox
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="CheckBox1"
       
android:id="@+id/checkBox"
       
android:layout_gravity="center_horizontal"
       
android:checked="false"
       
android:layout_marginTop="15dp" />

    <
CheckBox
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="CheckBox2"
       
android:id="@+id/checkBox2"
       
android:layout_gravity="center_horizontal"
       
android:checked="false"
       
android:layout_marginTop="15dp" />

    <
CheckBox
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
        
android:text="CheckBox3"
       
android:id="@+id/checkBox3"
       
android:layout_gravity="center_horizontal"
       
android:checked="false"
       
android:layout_marginTop="15dp" />

    <
Button
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Show"
       
android:id="@+id/button"
       
android:layout_gravity="center_horizontal"
       
android:layout_marginTop="35dp" />
</
LinearLayout>


MainActivity.java
-----------------
package com.android.anil.multiplecheckboxes;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    CheckBox c1,c2,c3;
    Button b1;

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

        c1=(CheckBox)findViewById(R.id.checkBox);
        c2=(CheckBox)findViewById(R.id.checkBox2);
        c3=(CheckBox)findViewById(R.id.checkBox3);
        b1=(Button)findViewById(R.id.button);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(c1.isChecked()){
                    Toast.makeText(getApplicationContext(),"CheckBox1 is Checked",5000).show();
                }else{
                    Toast.makeText(getApplicationContext(),"CheckBox1 is UnChecked",5000).show();

                }

                if(c2.isChecked()){
                    Toast.makeText(getApplicationContext(),"CheckBox2 is Checked",5000).show();
                }else{
                    Toast.makeText(getApplicationContext(),"CheckBox2 is UnChecked",5000).show();

                }

                if(c3.isChecked()){
                    Toast.makeText(getApplicationContext(),"CheckBox3 is Checked",5000).show();
                }else{
                    Toast.makeText(getApplicationContext(),"CheckBox3 is UnChecked",5000).show();

                }
            }
        });
    }

}

Output is:

-------------













No comments:

Post a Comment

Encryption Decryption Example Android Using AES Alogirthm

activity_main.xml ------------------------- <?xml version="1.0" encoding="utf-8"?> <LinearLayou...