-1

I am unable to position my radio group over a button. It goes back of the button in relative layout. What I want is a radio group over a button. I have added the output of the code. What I want is to bring the radio group over the 2 buttons. Can anyone help me with this UI issue?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
tools:context=".MainActivity">

<Button
    android:id="@+id/buttonBelow"
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:layout_centerInParent="true"
    android:background="#f00"
    android:text="Button Below" />

<Button
    android:id="@+id/buttonAbove"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_centerInParent="true"
    android:background="#ff0"
    android:text="Button Above" />


<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:background="#00f">

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="Male" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Female" />

</RadioGroup>


</RelativeLayout>
Amit Mishra
  • 128
  • 9

5 Answers5

0

Change your xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true">


<FrameLayout android:id="@+id/lin" android:layout_width="match_parent" android:layout_height="wrap_content"
             android:orientation="vertical" android:layout_centerInParent="true" android:layout_marginTop="24dp">



    <FrameLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center">

        <Button
                android:id="@+id/buttonBelow"
                android:layout_width="300dp"
                android:layout_gravity="center"
                android:layout_height="300dp"
                android:background="#f00"
                android:text="Button Below"/>

        <Button
                android:id="@+id/buttonAbove"
                android:layout_width="200dp"
                android:layout_height="200dp"
                android:layout_gravity="center"
                android:background="#ff0"
                android:text="Button Above"/>

    </FrameLayout>

    <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="#00f">

        <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="Male"/>

        <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Female"/>

    </RadioGroup>


</FrameLayout>

</RelativeLayout>
Piyush
  • 18,895
  • 5
  • 32
  • 63
  • good try @Piyush but the output is not desired output. I want the radio group over the button, meaning both buttons to go back of radio group and radio group coming over the 2 buttons. – Amit Mishra Dec 28 '18 at 07:22
  • @AmitMishra, instead of explaining it everytime, post the image of expected output and current output. – letsintegreat Dec 28 '18 at 07:25
0

From this answer, We need to just elevate RadioGroup by 1dp. No other changes.

It's because of Button has default android:elevation="1dp". so for RadioGroup, we need to set more elevation.

<Button
    android:id="@+id/buttonBelow"
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:layout_centerInParent="true"
    android:background="#f00"
    android:text="Button Below" />

<Button
    android:id="@+id/buttonAbove"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_centerInParent="true"
    android:background="#ff0"
    android:text="Button Above" />


<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:background="#00f"
    android:elevation="2dp">

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="Male" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Female" />

</RadioGroup>

Rumit Patel
  • 8,830
  • 18
  • 51
  • 70
0
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical">

        <Button
            android:id="@+id/buttonBelow"
            android:layout_width="300dp"
            android:layout_height="300dp"

            android:background="#f00"
            android:text="Button Below"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical">
        <Button
            android:id="@+id/buttonAbove"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:background="#ff0"
            android:text="Button Above"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical">
        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#00f">

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="Male"/>

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Female"/>

        </RadioGroup>
    </LinearLayout>

</RelativeLayout>
Mitesh Machhoya
  • 404
  • 2
  • 8
0
<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true">

    <Button
        android:id="@+id/buttonBelow"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_centerInParent="true"
        android:background="#f00"
        android:text="Button Below" />

    <Button
        android:id="@+id/buttonAbove"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_centerInParent="true"
        android:background="#ff0"
        android:text="Button Above" />
</RelativeLayout>

<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:background="#00f">

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="Male" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Female" />

</RadioGroup>
H.qy
  • 1
  • 1
0

Try this

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
tools:context=".MainActivity">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/buttonBelow"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_centerInParent="true"
        android:background="#f00"
        android:text="Button Below" />

    <Button
        android:id="@+id/buttonAbove"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_centerInParent="true"
        android:background="#ff0"
        android:text="Button Above" />
</RelativeLayout>
<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:background="#00f">

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="Male" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Female" />
</RadioGroup>

Android Geek
  • 8,956
  • 2
  • 21
  • 35