Search code examples
androidradio-buttonradio-groupandroid-compatibility

Arranging radio buttons just below the text and perfectly in center for all android devices


I want to Arrange radio butons just below the text but I am not able to do it. I have used android:layout_marginLeft="20dp" but it works for only particular device not for all.

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/txt1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="#ff0000"
        android:gravity="center"
        android:text="red" />

    <TextView
        android:id="@+id/txt2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="#00ff00"
        android:gravity="center"
        android:text="green" />

    <TextView
        android:id="@+id/txt3"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="#0000ff"
        android:gravity="center"
        android:text="blue" />
</LinearLayout>

<RadioGroup
    android:id="@+id/rg_rgrp"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="3" >

    <RadioButton
        android:id="@+id/rbtn_red"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:gravity="center" />

    <RadioButton
        android:id="@+id/rbtn_green"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center" />

    <RadioButton
        android:id="@+id/rbtn_blue"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center" />
</RadioGroup>

I have used Gravity and layout_gravity both but it works properly for text not for radio buttons.

I want screen like this compatible for many devices


Solution

  • Your RadioGroup should look like this. No code required.

    <RadioGroup
        android:id="@+id/rg_rgrp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <View
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="1" />
        <RadioButton
            android:id="@+id/rbtn_red"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="" />
        <View
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="2" />
        <RadioButton
            android:id="@+id/rbtn_green"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="" />
        <View
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="2" />
        <RadioButton
            android:id="@+id/rbtn_blue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="" />
        <View
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="1" />
    </RadioGroup>