Tuesday 15 January 2013

CheckBox in Android

ScreenShots:



activity_main.xml:

<RelativeLayout 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" >

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="87dp"
        android:layout_marginTop="54dp"
        android:text="CheckBox" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Show" />

</RelativeLayout>

MainActivity.java:
package com.ram.checkboxdemo;

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.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;

public class MainActivity extends Activity {
CheckBox check;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        check = (CheckBox)findViewById(R.id.checkBox1);
       
       /* check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if(arg0.isChecked()){
check.setText("Checked");
}else{
check.setText("UnChecked");
}
}
});*/
       
   
        Button showButton = (Button)findViewById(R.id.button1);
       
        showButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(check.isChecked()){
Toast.makeText(getApplicationContext(), "Checked", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(), "UnChecked", Toast.LENGTH_LONG).show();
}
}
});
    }

   
   
}


1 comment: