Thursday, July 5, 2012

RadioButton in Android

Click Here to Download source code


package name   :  selva.radiobutton

project name    :  RadioButton

version             :  1.5  ( Supports 1.5 and above versions)




main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
   
   
   
     <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="84dp"
        android:text="RadioButton"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#00ff00" />

     <RadioButton
         android:id="@+id/radioButton1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="RadioButton 1" />

     <RadioButton
         android:id="@+id/radioButton2"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="RadioButton 2" />

     <RadioButton
         android:id="@+id/radioButton3"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="RadioButton 3" />

</LinearLayout>






RadioButtonActivity.java



package selva.radiobutton;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.Toast;

public class RadioButtonActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
       final RadioButton radio1=(RadioButton) findViewById(R.id.radioButton1);
       final RadioButton radio2=(RadioButton) findViewById(R.id.radioButton2);
       final RadioButton radio3=(RadioButton) findViewById(R.id.radioButton3);
       
       radio1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                if ( isChecked )
                {
                   radio2.setChecked(false);
                   radio3.setChecked(false);
                   Toast.makeText(getApplicationContext(), "RadioButton 1 is checked", Toast.LENGTH_SHORT).show();
                  
                }
                             
            }});
       
       
        radio2.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                if ( isChecked )
                {
                   radio1.setChecked(false);
                   radio3.setChecked(false);
                   Toast.makeText(getApplicationContext(), "RadioButton 2 is checked", Toast.LENGTH_SHORT).show();
                  
                }
          
            }});
       
       
       
        radio3.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                if ( isChecked )
                {
                   radio2.setChecked(false);
                   radio1.setChecked(false);
                   Toast.makeText(getApplicationContext(), "RadioButton 3 is checked", Toast.LENGTH_SHORT).show();
                  
                }




     
            }});
 
    }
}




OUTPUT:





































click RadioButton 1












































click RadioButton 2













































 Click Here to Download source code



No comments:

Post a Comment