Android radio button group example tutorial

How to use radio button using radio group in android.

Radio button is simple round shape buttons basically used to select single choice from multiple given choices. There are a inbuilt condition set on radio buttons that application user can select only single selection at a time from given multiple values.So here is the complete step by step tutorial for Android radio button group example tutorial.

android-project-download-code-button

Android radio button group example tutorial.

Code for MainActivity.java file.

package com.android_examples.com.radiobutton;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
 // Inflate the menu; this adds items to the action bar if it is present.
 getMenuInflater().inflate(R.menu.main, menu);
 return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
 // Handle action bar item clicks here. The action bar will
 // automatically handle clicks on the Home/Up button, so long
 // as you specify a parent activity in AndroidManifest.xml.
 int id = item.getItemId();
 if (id == R.id.action_settings) {
 return true;
 }
 return super.onOptionsItemSelected(item);
 }
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.android_examples.com.radiobutton.MainActivity" >

 <RadioGroup
 android:id="@+id/radioGroup"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="43dp"
 android:orientation="vertical" >

 <RadioButton
 android:id="@+id/radioMale"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Male"
 android:typeface="serif" />

 <RadioButton
 android:id="@+id/radFemale"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Female"
 android:typeface="monospace" />
 </RadioGroup>

</RelativeLayout>

Screenshot:

Android radio button group example tutorial

Click Here To Download Android radio button group example tutorial Project.

Leave a Reply

Your email address will not be published. Required fields are marked *