Android Projects

Android Development Books

Friday

How to arrange 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 RadioButtonEx.
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, RadioButtonEx!</string>
    <string name="app_name">RadioButtonEx</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 RadioButtonEx and set the information as stated in the image.
Build Target: Android 3.0
Application Name: RadioButtonEx
Package Name: com.org. RadioButtonEx
Activity Name: RadioButtonEx
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 RadioButtonEx 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.

Share this post
  • Share to Facebook
  • Share to Twitter
  • Share to Google+
  • Share to Stumble Upon
  • Share to Evernote
  • Share to Blogger
  • Share to Email
  • Share to Yahoo Messenger
  • More...

0 comments

Thanks for your comment

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

Related Posts Plugin for WordPress, Blogger...
© Google Android Lovers
Designed by BlogThietKe Cooperated with Duy Pham
Released under Creative Commons 3.0 CC BY-NC 3.0