Hari Krishnan
Hari Krishnan

Reputation: 6302

How could i align radio buttons inside radiogroup android?

I need to align the RadioButtons inside a RadioGroup just like a RelativeLayout. I have read that the RadioGroup is inherited from LinearLayout and possibly i can't align contents like a RelativeLayout inside it. The actual thing that i am trying to implement is two rows inside the RadioGroup, First row contains two RadioButtons and in the second row, i have to add another button at the start of it. How could i do that?

Upvotes: 0

Views: 1424

Answers (3)

Meow Cat 2012
Meow Cat 2012

Reputation: 998

By default RadioGroup only recognize RadioButtons added as direct child and act like a LinearLayout, but we could tweak the behavior if we write our own RadioGroups.

Now there're quite many projects so we don't have to reinvent: https://github.com/search?q=radiogroup. This is a link to search result and will not be invalid unless Github dies.

I prefer this solution it simply detect RadioButtons as indirect children.

Upvotes: 0

tk1505
tk1505

Reputation: 312

All you need to do is set orientation in radio group to horizontal to align them horizontally check out below code.

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="@dimen/activity_horizontal_margin">


    <RadioGroup
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="match_parent">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New RadioButton"
            android:id="@+id/radioButton" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New RadioButton"
            android:id="@+id/radioButton2" />
    </RadioGroup>
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New RadioButton"
        android:layout_gravity="start"
        android:id="@+id/radioButton3" />

</LinearLayout>

Upvotes: 2

Naveen
Naveen

Reputation: 830

Can you try this layout and let me know if that works for you. I can modify that if required.

<?xml version="1.0" encoding="utf-8"?>
<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:padding="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="5dp">

        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Red"/>

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

    </LinearLayout>
</RadioGroup>

</LinearLayout>

Layout screenshot

Upvotes: 0

Related Questions