-1

Here is the Layout

<FrameLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/tab"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
           <LinearLayout
                android:id="@+id/panel1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
                <EditText
                    android:id="@+id/editText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="textMultiLine"
                    android:text="How much is 5 by 5?"
                    android:visibility="visible" />
                <RadioGroup
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
                    <RadioButton
                        android:id="@+id/radio_1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="10" />
                    <RadioButton
                        android:id="@+id/radio_2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="25" />
                    <RadioButton
                        android:id="@+id/radio_3"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="50" />
            </RadioGroup>
        </LinearLayout>
    </FrameLayout>

What I need is: the EditBox is on top, and all radio buttons will share the rest of space below the EditText. For this I use android:layout_weight="1"

When Android App Layout manager loads this Layout from XML then the space is used by radios as expected.

When I build it from code using:

rb.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                                LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));

It doesn't.. Radios are all nested upward, not spreaded inside the rest of space. Please, any idea why? And what am I doing wrong?

Thanks.

  • `what am I doing wrong?` **1** - Get rid of the useless FrameLayout. Nesting layouts is *bad for performances*. Keep your designs *as flat as possible*. **2** - The weighted size must be exactly **0 dp**, for the weights to work. – Phantômaxx Sep 03 '17 at 10:22
  • it's not useless. this code will be placed in another container - FrameLayout. Maybe it's bad but for me is the one of the necessary parts. The problem was - see my answer. –  Sep 03 '17 at 10:56
  • FrameLayout + LinearLayout... You could simply use a PercentRelativeLayout and have a **single** container. – Phantômaxx Sep 03 '17 at 12:17
  • in this Questiion I simplified the structure. Between FrameLayout and Linear there is ViewAnimator ). I create a number of Linears dynamically, load'em inside Animator and all that bouquet stays in Frame –  Sep 03 '17 at 14:27

2 Answers2

0
  1. Delete android:layout_weight of all radiobittons;
  2. Set android:layout_weight=1 to edittext and radiogroup
Arthur
  • 769
  • 7
  • 17
0

The answer is the next:

The LayoutWeight of RadioButton in RadioGroup will work correct if the correct structure for these parameters is used. In my case it is:

rb.setLayoutParams(new RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
                                        ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f));