Wednesday 24 October 2012

Radio Button Example in Android

A radio button is a two-states button that can be either checked or unchecked. When the radio button is unchecked, the user can press or click it to check it. However, contrary to a CheckBox, a radio button cannot be unchecked by the user once checked.

Radio buttons are normally used together in a RadioGroup. When several radio buttons live inside a radio group, checking one radio button unchecks all the others.

This example shows the how to create a Radio button in Xml File...and when user choose one option from Radio Buttons. Choose button text will appear in Text View.....

UI layout(radiobutton.xml) create this xml in res/layout 

<?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/textshow"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <RadioGroup
        android:id="@+id/rbgroup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/button1"
            android:checked="false"
            android:text="Android " />

        <RadioButton
            android:id="@+id/button2"
            android:checked="false"
            android:text="iphone" />
    </RadioGroup>

    <Button
        android:id="@+id/select"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Confrom selection shows in Text " >
    </Button>

</LinearLayout>


Android Activity Class(RBexample.java)

package com.androidsurya.radiobutton;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;

public class RBexample extends Activity {

/** Called when the activity is first created. */

RadioButton rbone;
RadioButton rbtwo;
TextView textView;
Button button;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.radiobutton);
rbone = (RadioButton) findViewById(R.id.button1);
rbtwo = (RadioButton) findViewById(R.id.button2);
button = (Button) findViewById(R.id.select);
textView = (TextView) findViewById(R.id.textshow);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
if (rbone.isChecked() == true) {
textView.setText("Andriod");
}
if (rbtwo.isChecked() == true) {
textView.setText("iphone");
}
}
});
}
}

Register Android Activity in Android Manifest file

  <activity
            android:name="com.androidsurya.radiobutton.RBexample"
            android:label="@string/app_name" >

Output Screenshot






















For More information about Radio Group: Android Developers site

No comments:

Post a Comment

Android SQLite Database Viewer or Debuging with Stetho

Every Android Developer uses SQLite Database to store data into the Android Application data. But to view the data in SQLite have a lot of...