Tuesday, 20 January 2015

RadioButton Example1

RadioButton Example in android




strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">RadioButton2</string>
    <string name="action_settings">Settings</string>
    <string name="gender">Select Gender:</string>
    <string name="radiobutton_male">Male</string>
    <string name="radiobutton_female">Female</string>
       
   
</resources>


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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/activity_textView_gender"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="47dp"
        android:layout_marginTop="84dp"
        android:text="@string/gender"
        android:textSize="20sp" />

    <RadioGroup
        android:id="@+id/activity_radioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/activity_textView_gender"
        android:layout_centerHorizontal="true" >

        <RadioButton
            android:id="@+id/activity_radiobutton_male"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radiobutton_male" />

        <RadioButton
            android:id="@+id/activity_radiobutton_female"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radiobutton_female" />
    </RadioGroup>

</RelativeLayout>


MainActivity.java


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

public class MainActivity extends Activity {


       RadioGroup gender;
       RadioButton male,female;
       String item;
       Button button;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
             
              gender=(RadioGroup) findViewById(R.id.activity_radioGroup);
      

              gender.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                    
                     @Override
                     public void onCheckedChanged(RadioGroup group, int checkedId) {
                          
                           RadioButton button = (RadioButton)findViewById(checkedId);
                          
                           String string= button.getText().toString();    
                           Toast.makeText(getApplicationContext(), string+" is                                      Selected", Toast.LENGTH_LONG).show();
                    
                     }

               });
        
           }

      }





No comments:

Post a Comment