Wednesday, 26 August 2015

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

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

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

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



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 cb1;

    Button b1;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        cb1=(CheckBox)findViewById(R.id.checkBox);

        b1=(Button)findViewById(R.id.button);

        b1.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                if(cb1.isChecked()){

         Toast.makeText(getApplicationContext(),"CheckBox is Checked",5000).show();

                    

                }else{

       Toast.makeText(getApplicationContext(),"CheckBox is UnChecked",5000).show();



                }

            }

        });

        

    }



}



Output is :
----------







No comments:

Post a Comment