How to make Radio button horizontal in android

How to create RadioGroup with radio button horizontal layout in android at left to right side by side.

In this tutorial we are creating multiple radio buttons using XML layout file and setting up that buttons on left to right formation or we can call that as Side by Side. So here is the complete step by step tutorial for How to make Radio button horizontal in android.

android-project-download-code-button

How to make Radio button horizontal in android.

Code for MainActivity.java file.

package com.radiobuttonhorizontal_android_examples.com;
import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

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

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.radiobuttonhorizontal_android_examples.com.MainActivity" >

 
 
 <RadioGroup
 android:id="@+id/radiogroup"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:orientation="horizontal"
 android:layout_centerVertical="true"
 android:layout_centerHorizontal="true" >
 <RadioButton
 android:id="@+id/radioButton1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Android" />

 <RadioButton
 android:id="@+id/radioButton2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="PHP" />

 <RadioButton
 android:id="@+id/radioButton3"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Blogger" />
</RadioGroup>

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_above="@+id/radiogroup"
 android:layout_centerHorizontal="true"
 android:layout_marginBottom="25dp"
 android:text="Horizontal Radio Buttons"
 android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Screenshot:

How to make Radio button horizontal in android

Click here to download How to make Radio button horizontal in android project with source code.

Leave a Reply

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