-1

I am doing an exercise from "Advanced Android Development Course". This code doesn't display RadioButtons:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="@color/my_fragment_color"
   android:orientation="horizontal"
   tools:context=".SimpleFragment">

<TextView
    android:id="@+id/fragment_header"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
    android:padding="10dp"
    android:text="@string/question_article"/>

<RadioGroup
    android:id="@+id/radio_group"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <RadioButton
        android:id="@+id/radio_button_yes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="8dp"
        android:text="@string/yes"/>

    <RadioButton
        android:id="@+id/radio_button_no"
        android:layout_marginRight="8dp"
        android:text="@string/no"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RadioGroup>

RadioButtons appear when I remove TextView. How TextView hid those buttons? I tried using also marginEnd or weight atributes, but this wasn't a solution.

Pochmurnik
  • 780
  • 6
  • 18
  • 35

5 Answers5

4

Your TextView hight is match_parent that's why your RadioButton is not visible

Try to set android:layout_weight="1" in your TextView it will work

Try this

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/my_fragment_color"
        android:orientation="horizontal"
        tools:context=".SimpleFragment">

        <TextView
            android:id="@+id/fragment_header"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
            android:padding="10dp"
            android:text="@string/question_article"/>

        <RadioGroup
            android:id="@+id/radio_group"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/radio_button_yes"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="8dp"
                android:text="@string/yes"/>

            <RadioButton
                android:id="@+id/radio_button_no"
                android:layout_marginRight="8dp"
                android:text="@string/no"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </RadioGroup>
</LinearLayout>
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
2

Try this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">


    <TextView
        android:id="@+id/fragment_header"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:padding="10dp"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
        android:text="@string/question_article"/>

    <RadioGroup
        android:id="@+id/radio_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/radio_button_yes"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="8dp"
            android:text="@string/yes"/>

        <RadioButton
            android:id="@+id/radio_button_no"
            android:layout_marginRight="8dp"
            android:text="@string/no"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RadioGroup>




</LinearLayout>
Ratilal Chopda
  • 4,162
  • 4
  • 18
  • 31
2

Use wrap content for your TextView height and width as shown below.

 <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/gray_btn_bg_color"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/fragment_header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
        android:padding="10dp"
        android:text="question_article"/>
    <RadioGroup
        android:id="@+id/radio_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/radio_button_yes"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="yes"/>
        <RadioButton
            android:id="@+id/radio_button_no"
            android:text="no"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RadioGroup>
</LinearLayout>

You can use the weight attribute to allocate a portion of the layout’s space to a view, depending on its weight, and for better responsive UI try to use ConstraintLayout. To learn more about ConstraintLayout click Here

Sniffer
  • 1,495
  • 2
  • 14
  • 30
2

Your LinearLayout is orientation="horizontal"yet your first TextView takes full width with layout_width="match_parent" leaving no space for any other view.

Review your layout. I'm not sure if you wanted the TextView vertically above RadioButton.

shkschneider
  • 17,833
  • 13
  • 59
  • 112
1

This property assignment for textView is incorrect

android:layout_width="match_parent"

Assign wrap_content instead and it will work.

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
kundan kamal
  • 674
  • 7
  • 16