Saturday, September 8, 2012

Radio Button In Android

This example shows how you can work with radio buttons. We are using Tablet environment for the same.
Algorithm:
1.) Create a new project by File-> New -> Android Project name it RadioButtonDemo.
2.) You will see some default code into your strings.xml and android manifest file.
3.) Write following code into your mail.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical">

    <RadioGroup
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="vertical"
       android:checkedButton="@+id/lunch"
       android:id="@+id/menu">
        <RadioButton
           android:text="@string/radio_group_snack"
           android:id="@+id/snack" />
        <RadioButton
           android:text="@string/radio_group_1_breakfast"
           android:id="@+id/breakfast"
           />
        <RadioButton
           android:text="@string/radio_group_1_lunch"
           android:id="@id/lunch" />
        <RadioButton
           android:text="@string/radio_group_1_dinner"
           android:id="@+id/dinner" />
        <RadioButton
           android:text="@string/radio_group_1_all"
           android:id="@+id/all" />
        <TextView
           android:text="@string/radio_group_1_selection"
           android:id="@+id/choice" />
    </RadioGroup>
    <Button
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@string/radio_group_1_clear"
       android:id="@+id/clear" />
</LinearLayout>
4.) Write following code into your strings.xml
<resources>
    <string name="hello">Hello World, RadioButtonDemo!</string>
    <string name="app_name">RadioButtonDemo</string>
    <string name="radio_group_snack">Snack</string>
    <string name="radio_group_selection">"You have selected: "</string>
    <string name="radio_group_none">(none)</string>
    <string name="radio_group_1_breakfast">Breakfast</string>
    <string name="radio_group_1_lunch">Lunch</string>
    <string name="radio_group_1_dinner">Dinner</string>
    <string name="radio_group_1_all">All of them</string>
    <string name="radio_group_1_selection">You have selected: (none)</string>
    <string name="radio_group_1_clear">Clear</string>
    <string name="controls_1_radiobutton_1">RadioButton 1</string>
    <string name="controls_1_radiobutton_2">RadioButton 2</string>
</resources>
5.) Build and run your code and check the output given below in the doc.
Steps:
1.) Create a project named RadioButtonDemo and set the information as stated in the image.
Build Target: Android 3.0
Application Name: RadioButtonDemo
Package Name: com.org. RadioButtonDemo
Activity Name: RadioButtonDemo
Min SDK Version: 11
2.) Open RadioButtonDemo.java file and write following code there:
package com.org.RadioButtonDemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;
public class RadioButtonDemo extends Activity implementsRadioGroup.OnCheckedChangeListener,
        View.OnClickListener {
    private TextView mChoice;
    private RadioGroup mRadioGroup;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mRadioGroup = (RadioGroup) findViewById(R.id.menu);
        // test adding a radio button programmatically
        /*RadioButton newRadioButton = new RadioButton(this);
        newRadioButton.setText(R.string.radio_group_snack);
        newRadioButton.setId(R.id.snack);
        LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
                RadioGroup.LayoutParams.WRAP_CONTENT,
                RadioGroup.LayoutParams.WRAP_CONTENT);
        mRadioGroup.addView(newRadioButton, 0, layoutParams);*/
        // test listening to checked change events
        String selection = getString(R.string.radio_group_selection);
        mRadioGroup.setOnCheckedChangeListener(this);
        mChoice = (TextView) findViewById(R.id.choice);
        mChoice.setText(selection + mRadioGroup.getCheckedRadioButtonId());
        // test clearing the selection
        Button clearButton = (Button) findViewById(R.id.clear);
        clearButton.setOnClickListener(this);
    }
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        String selection = getString(R.string.radio_group_selection);
        String none = getString(R.string.radio_group_none);
        mChoice.setText(selection +
                (checkedId == View.NO_ID ? none : checkedId));
    }
    public void onClick(View v) {
        mRadioGroup.clearCheck();
    }
}
3.) Compile and build the project.
4.) Run on 3.0 simulator for the output.
Output

No comments:

Post a Comment