0

Android Radio Button and Radio Group Example Code

main.xml
[code language=”css”]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:textSize="20dp" android:textStyle="bold"
android:textColor="#fff" android:layout_width="wrap_content"
android:layout_gravity="center_horizontal" android:id="@+id/textView1"
android:layout_height="wrap_content" android:layout_marginTop="10dp"
android:layout_marginBottom="10dp" android:text="How was today’s tutorial ?"></TextView>
<RadioGroup android:layout_width="wrap_content"
android:layout_gravity="center_horizontal" android:layout_height="wrap_content"
android:layout_weight="1" android:id="@+id/rgOpinion">
<RadioButton android:layout_width="wrap_content"
android:id="@+id/radio0" android:layout_height="wrap_content"
android:checked="true" android:text="Excellent"></RadioButton>
<RadioButton android:id="@+id/radioButton1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Good"></RadioButton>
<RadioButton android:layout_width="wrap_content"
android:id="@+id/radio1" android:layout_height="wrap_content"
android:text="Average"></RadioButton>
<RadioButton android:layout_width="wrap_content"
android:id="@+id/radio2" android:layout_height="wrap_content"
android:text="Poor"></RadioButton>
</RadioGroup>
<Button android:text="Submit Poll" android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/btnSubmit"></Button>
</LinearLayout>
[/code]

MainActivity.java

[code language=”css”]

package com.code.ofsandy;

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.RadioGroup;
import android.widget.Toast;

public class MainActivity extends Activity implements
OnClickListener {

// Widget GUI
RadioGroup rgOpinion;
Button btnSubmit;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// Init Widget GUI
rgOpinion = (RadioGroup) findViewById(R.id.rgOpinion);
btnSubmit = (Button) findViewById(R.id.btnSubmit);

// Attached Click listener to Button
btnSubmit.setOnClickListener(this);
}

public void onClick(View v) {
// TODO Auto-generated method stub

if (v == btnSubmit) {

// Get Selected Radio Button and display output
RadioButton selectRadio = (RadioButton) findViewById(rgOpinion
.getCheckedRadioButtonId());
String opinion = selectRadio.getText().toString();

Toast.makeText(this, "Your Opinion is : " + opinion,
Toast.LENGTH_LONG).show();

}

}
}
[/code]

Sandeep Kumar

Sandeep kumar is a web developer and a professinal blogger. He works for latest technologies and write articles what he learn. In ICodeHub he share web languages tutorials and other programming tips and tricks. if you need some help or have some suggestion then mail him at sandeep92pal@gmail.com without any hesitation. He believes in learning and sharing.