Radio Button Example Android

Radio buttons are used when you have multiple choices but you have to select only one.

In  Android,  Radio buttons are grouped together by  android.widget.RadioGroup class.If radio buttons are inside Radio group so when one radio button is selected then all others becomes unselected.

Now,I am going to make radio group "Gender " with two radio buttons "Male" &  "Female".

activity_main.xml



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txtGender"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:text="GENDER"
        android:textColor="#0198E1"
        android:textSize="16sp"
        android:textStyle="bold" >
    </TextView>

    <View
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="2dp"
        android:background="#0198E1" />

    <LinearLayout
        android:id="@+id/linearlayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <RadioGroup
            android:id="@+id/rgGender"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/rdbMale"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:checked="true"
                android:text="Male"
                android:textColor="#0198E1"
                android:textSize="16sp"
                android:textStyle="bold" />

            <RadioButton
                android:id="@+id/rdbFemale"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:text="Female"
                android:textColor="#0198E1"
                android:textSize="16sp"
                android:textStyle="bold" />
        </RadioGroup>
    </LinearLayout>

    <TextView
        android:id="@+id/txtSelGender"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:text="Selected Gender"
        android:textColor="#0198E1"
        android:textSize="16sp"
        android:textStyle="bold" >
    </TextView>

    <View
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="2dp"
        android:background="#0198E1" />

    <TextView
        android:id="@+id/txtSelGenderValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:textColor="#0198E1"
        android:textSize="16sp"
        android:textStyle="bold" >
    </TextView>

    <Button
        android:id="@+id/btnSubmit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="submit" />

</LinearLayout>


layout preview





















MainActivity,java




package com.arjun.radiobuttondemo;



import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity implements OnCheckedChangeListener {

Button btnSubmit;
RadioButton rdbMale, rdbFemale;
RadioGroup rgGender;
TextView txtSelecteGender;
String gender ="Male";
Context ctx;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ctx = this;

init();

rgGender.setOnCheckedChangeListener(this);

btnSubmit.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
txtSelecteGender.setText(gender);
}
});



}

public void init() {
btnSubmit =(Button)findViewById(R.id.btnSubmit);
rgGender = (RadioGroup) findViewById(R.id.rgGender);
rdbMale = (RadioButton) findViewById(R.id.rdbMale);
rdbFemale = (RadioButton) findViewById(R.id.rdbMale);
txtSelecteGender =(TextView) findViewById(R.id.txtSelGenderValue);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

@Override
public void onCheckedChanged(RadioGroup group, int position) {
// TODO Auto-generated method stub
switch (position) {
case R.id.rdbMale:
gender = "Male";
System.out.println("Male");
break;
case R.id.rdbFemale:
gender = "Female";
System.out.println("FeMale");
break;

default:
break;
}
}

}


after clicking submit button it will show selected gender




Comments

Post a Comment

Popular Posts