Why won't my first TextView shrink to accommodate a second one?

322 views Asked by At

When trying to render the following RelativeLayout on smaller screen sizes. In the graphical layout editor, the second TextView is completely hidden, but the first one is fully expanded showing the complete text contents. I understand that TextViews are supposed to be scrollable.

Is there any way to use RelativeLayout and give each an equal portion of the screen, or should I just use a LinearLayout and use layout_weight? From the guidance in the Android documentation, I was under the impression I should avoid using LinearLayout in favor of RelativeLayout.

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:fillViewport="true"
        android:gravity="center"
        android:text="Lorem ipsum dolor sit amet, onsectetur adipiscing elit. Curabitur molestie tempor ante non interdum. Nullam sit amet diam ligula. Suspendisse ultricies commodo felis in rutrum. Lorem ipsum dolor sit amet, onsectetur adipiscing elit. Curabitur molestie tempor ante non interdum. Nullam sit amet diam ligula. Suspendisse ultricies commodo felis in rutrum.Lorem ipsum dolor sit amet, onsectetur adipiscing elit. Curabitur molestie tempor ante non interdum. Nullam sit amet diam ligula. Suspendisse ultricies commodo felis in rutrum."
        android:scrollbars="vertical"
        android:maxlines="100"
        android:textSize="22dp" >
    </TextView>

    <TextView
        android:id="@+id/tv2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv1"
        android:fillViewport="true"
        android:gravity="center"
        android:scrollbars="vertical"
        android:maxlines="100"
        android:text="Lorem ipsum dolor sit amet, onsectetur adipiscing elit. Curabitur molestie tempor ante non interdum. Nullam sit amet diam ligula. Suspendisse ultricies commodo felis in rutrum. Lorem ipsum dolor sit amet, onsectetur adipiscing elit. Curabitur molestie tempor ante non interdum. Nullam sit amet diam ligula. Suspendisse ultricies commodo felis in rutrum.Lorem ipsum dolor sit amet, onsectetur adipiscing elit. Curabitur molestie tempor ante non interdum. Nullam sit amet diam ligula. Suspendisse ultricies commodo felis in rutrum."
        android:textSize="22dp" />
</RelativeLayout>

2

There are 2 answers

0
Eddy K On BEST ANSWER

Since you used "wrap_content" in the first TextView, it fills all the space available then there is no space left for the second TextView. if you wish to have them both, then you can put them inside a LinearLayout with equal weights or you can specify a certain size in the "layout_height" field. P.S. If you want them to be equal on all devices then LinearLayout is your best bet. Also you can have the LinearLayout inside the RelativeLayout in case you want to add any other widgets relative to them.

0
Gophermofur On

The supported way, if you could call it that, would be to use LinearLayout and layout weights. Someone in another answer had a neat idea. Create an invisible View, center it and position the two text views on both side of that view.

That said, it would always be a 50/50 split and doesn't give you as much flexibility (Shrink/Grow) as layout weights, but it depends on what you need.