-1
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <LinearLayout
            android:id="@+id/public_line"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
        <TextView
            android:id="@+id/Public"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="6"
            android:text="Public"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="24dp"
            android:textSize="20dp"
            android:textColor="@android:color/black"/>
        <RadioButton
            android:id="@+id/public_button"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="16dp"
            android:layout_marginTop="24dp"
            />

        </LinearLayout>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginBottom="8dp"
            android:text="league is visible to the public,
                          that is, anyone can join your league"
            />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/Private"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="6"
                android:text="Private"
                android:layout_marginLeft="16dp"
                android:layout_marginTop="24dp"
                android:textSize="20dp"
                android:textColor="@android:color/black"/>
            <RadioButton
                android:id="@+id/private_button"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginRight="16dp"
                android:layout_marginTop="24dp"
                />

        </LinearLayout>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginBottom="8dp"
            android:text="Only people with invite code can join"
            />
    </RadioGroup>
    <TextView
        android:layout_alignParentBottom="true"
        android:background="@drawable/orange_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Done"
        android:padding="16dp"
        android:textSize="20dp"
        android:textColor="@android:color/black"
        android:layout_weight="1"
        android:gravity="center"/>
</RelativeLayout>

Here is my layout. It allows both the radio buttons to be selected. Can someone help me figure out what the problem is? From what I know the problem is that the radio buttons are not directly in the radio group, but how am I supposed to accommodate them in the radio group directly to get this same layout. Else is there an alternate way to solve the problem

Dr Mido
  • 2,414
  • 4
  • 32
  • 72

4 Answers4

2

Your radioButtons must be exaclly child of RadioGroup like this

<RadioGroup
        android:id="@+id/radiogroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />

 </RadioGroup>

But in your case is child of linearlayout and each radio button can selecteable

Radesh
  • 13,084
  • 4
  • 51
  • 64
0

The RadioButtons have to be direct childs of RadioGroup to have it automatically manage the single selection.
From your layout it seems you are trying to display text to the side of your RadioButtons. To do so you can simply set the attribute android:text on the RadioButton, no need to use additional TextViews.

gpunto
  • 2,573
  • 1
  • 14
  • 17
0

just simply try this one

<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/radio1">
    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/Public"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Public"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="24dp"
            android:textSize="20dp"
            android:textColor="@android:color/black"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"/>

        <RadioButton
            android:id="@+id/public_button"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="16dp"
            android:layout_marginTop="24dp"
            android:checked="true"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginBottom="8dp"
            android:text="league is visible to the public,
                      that is, anyone can join your league"
            app:layout_constraintTop_toBottomOf="@+id/Public"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:id="@+id/text1"
            />
    <TextView
            android:id="@+id/Private1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="6"
            android:text="Private"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="24dp"
            android:textSize="20dp"
            android:textColor="@android:color/black"
            app:layout_constraintTop_toBottomOf="@+id/text1"
            app:layout_constraintStart_toStartOf="parent"/>

        <RadioButton
            android:id="@+id/private_button2"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="16dp"
            android:layout_marginTop="24dp"
            android:checked="true"
            app:layout_constraintTop_toBottomOf="@+id/text1"
            app:layout_constraintEnd_toEndOf="parent"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginBottom="8dp"
            android:text="Only people with invite code can join"
            app:layout_constraintTop_toBottomOf="@+id/Private1"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"/>

    </android.support.constraint.ConstraintLayout>
</RadioGroup>
DEVSHK
  • 833
  • 11
  • 10
0

There are two ways to achieve your solutions

First is that RadioButtons set directly childs of RadioGroup like below

<RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <RadioButton
            android:id="@+id/public_button"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="16dp"
            android:layout_marginTop="24dp"
            />


         <RadioButton
                android:id="@+id/private_button"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginRight="16dp"
                android:layout_marginTop="24dp"
                />
</RadioGroup>

Second solution is if you don't set RadioButtons set directly childs of RadioGroup than you should add some code to your java file like below:

 publicButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    publicButton.setChecked(true);
                    privateButton.setChecked(false);
                }
            }
        });
privateButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(isChecked){
                            publicButton.setChecked(false);
                            privateButton.setChecked(true);
                    }
                }
            });

Where privateButton and publicButton are radio buttons.

I hope its work for you.

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