Thursday, March 1, 2012

Android code for checkbox validation

This program is for checkbox validation in android





Step 1) Your main java file is here

package check.box;

import java.lang.reflect.Array;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class CheckboxActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
       final CheckBox chk=(CheckBox)findViewById(R.id.Chk);
       chk.setOnClickListener(new OnClickListener() {
                               
                                public void onClick(View v) {
                                                // TODO Auto-generated method stub
                               
                                                TextView t=(TextView)findViewById(R.id.txt);
                                               
                                                if (((CheckBox) v).isChecked()) {
                                                                t.setText("Checked");
                                                               
                                                }
                                                else
                                                {
                                                                t.setText("Not Checked");
                                                }
                                               
                                                                               
                                               
                                }
                });
      
      
      
    }
}


Step 2) Put below code in your main.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
   
    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/txt"
    />
   
    <CheckBox
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/Chk"
    android:text="Click me"
    android:checked="false"
   
    />
   
   
   
</LinearLayout>

No comments:

Post a Comment