Home > Uncategorized > How to implement Radio Button in Android

How to implement Radio Button in Android

Here is the step by step procedure to create radio button in android : –
In Android by using RadioGroup only we can use RadioButton. We can add any number of RadioButtons to a single RadioGroup. we can add any number of RadioGroup to a single Layout.

1. main.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:orientation=”vertical” android:layout_width=”fill_parent”
android:layout_height=”fill_parent”>
<TextView android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:textStyle=”bold” android:text=”Radio Button Example of Online Examinaiton Center from SSTech” />
<RadioGroup android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:orientation=”vertical”
android:id=”@+id/group_radio”>
<RadioButton android:checked=”true”
android:id=”@+id/local_support” android:text=”Local Support”/>
<RadioButton android:checked=”false”
android:id=”@+id/web_support” android:text=”Web Support”/>
</RadioGroup>
<TextView android:id=”@+id/TextView01″ android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”Selected is : “></TextView>

<Button android:layout_width=”wrap_content”
android:layout_height=”wrap_content” android:text=”OK” android:id=”@+id/ok_button”/>
</LinearLayout>

2. MainActivity.java

package com.sstech.onle

public class OLECHOme extends Activity implements Button.OnClickListener
{
private RadioButton localSupport;
private RadioButton webSupport;
private Button okButton;
private TextView t1;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
localSupport=(RadioButton)findViewById(R.id.local_support);
webSupport=(RadioButton)findViewById(R.id.web_support);
okButton=(Button)findViewById(R.id.ok_button);
okButton.setOnClickListener(this);
t1=(TextView)findViewById(R.id.TextView01);
}

@Override
public void onClick(View v) {
if(v == b1)
{
if(localSupport.isChecked() == true)
t1.setText(“Selected is : “+rb1.getText());
if(webSupport.isChecked() == true)
t1.setText(“Selected is : “+rb2.getText());
}
}
}

Advertisements
Categories: Uncategorized
  1. May 24, 2011 at 8:09 am

    Is it possible to set text to bold when it is selected?

    Thanks!

  2. May 24, 2011 at 8:41 am

    yes,

  3. Naeem Shah
    July 12, 2011 at 7:43 am

    thanx its helpful

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: