Thursday, 7 February 2013

Working with CheckBox in Android

 





activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/TextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Developed by Shrikant"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <CheckBox
        android:id="@+id/check1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/TextView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="36dp"
        android:text="Checked / UnChecked &amp; Click Button" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/check1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="23dp"
        android:text="Click" />

    <CheckBox
        android:id="@+id/check2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/check1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="67dp"
        android:text="Checked and UnChecked" />

</RelativeLayout>


MainActivity.java
package com.shrikant.checkbox;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.view.View.OnClickListener;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class MainActivity extends Activity {
CheckBox check1,check2;
Button clickbtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        check1=(CheckBox)findViewById(R.id.check1);
        check2=(CheckBox)findViewById(R.id.check2);
        clickbtn=(Button)findViewById(R.id.button1);
        clickbtn.setOnClickListener(new OnClickListener() {
           
            @Override
            public void onClick(View arg0) {
                if(check1.isChecked())
                {
                    Toast.makeText(getApplicationContext(),"Check Box Checked", Toast.LENGTH_LONG).show();
                }
                else
                {
                    Toast.makeText(getApplicationContext(),"Check Box UnChecked", Toast.LENGTH_LONG).show();
                }
            }
        });
        check2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
           
            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                if(arg0.isChecked())
                {
                check2.setText("Checked");   
                }
                else
                {
                    check2.setText("Unchecked");
                }
            }
        });
    }

   }
 



No comments:

Post a Comment

Note: only a member of this blog may post a comment.