1

I have an ImageView, a TextView and another 'ImageView', grouped in a vertical LinearLayout.
The TextView should contain long text, so I wrapped it in a ScrollView. The first ImageView should be 100dp height, and the third 100dp height. The middle TextView should fill the entire available space, and if the text is too long the a scroll bar should appear.
The entire layout is part of a parent layout, so this layout has only part of the screen available. To demonstrate it i set the LinearLayout height to be 265dp.
The problem is that the the whole text appears, the scroll bar doesn't appear, and the third bottom ImageView height is squashed.
I'm following this post, and this is a screenshot.

enter image description here

My code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="265dp"
android:background="#BDBDBD"
android:orientation="vertical">


<ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:background="#FFFF00" />


<ScrollView
    android:id="@+id/SCROLLER_ID"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical"
    android:fillViewport="true">

    <TextView
        android:id="@+id/TEXT_STATUS_ID"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.0"
        android:text="bla\nbla\nbla\nbla\nbla\nbla\nbla"/>

</ScrollView>


<ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:background="#FF00FF" />

</LinearLayout>
Community
  • 1
  • 1
Presen
  • 1,629
  • 4
  • 25
  • 44

3 Answers3

0

Try this layout :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#BDBDBD"
android:orientation="vertical">

<ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:background="#FFFF00" />

<ScrollView
    android:id="@+id/SCROLLER_ID"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

    <TextView
        android:id="@+id/TEXT_STATUS_ID"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="bla\nbla\nbla\nbla\nbla\nbla\nbla"/>

</ScrollView>

<ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:background="#FF00FF" />

</LinearLayout>
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="265dp"
    android:background="#BDBDBD" >

    <ImageView
        android:id="@+id/imgtop"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentTop="true"
        android:background="#FFFF00" />

    <ImageView
        android:id="@+id/imgbottom"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentBottom="true"
        android:background="#FF00FF" />

    <ScrollView
        android:id="@+id/SCROLLER_ID"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/imgbottom"
        android:layout_below="@id/imgtop"
        android:fillViewport="true"
        android:scrollbars="vertical" >

        <TextView
            android:id="@+id/TEXT_STATUS_ID"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="bla\nbla\nbla\nbla\nbla\nbla\nbla"
            android:textColor="@color/black" />
    </ScrollView>

</RelativeLayout>
Prateek Yadav
  • 752
  • 5
  • 8
0
<ScrollView
android:id="@+id/SCROLLER_ID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fillViewport="true">

  <TextView
    android:id="@+id/TEXT_STATUS_ID"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1.0"
    android:text="bla\nbla\nbla\nbla\nbla\nbla\nbla"/>

</ScrollView>
  1. the weight that you assigned to the TextView does not have any meaning: It's inside a ScrollView (which extends FrameLayout, not LinearLayout) and even if it did have a meaning it's not what you are looking for - It's just say that it should take most of the space inside the ScrollView
  2. You gave the ScrollView height of wrap_content - in your case it means that it will take the full height of the TextView - thus if the TextView height is bigger than the height of the screen - It will take all the height of the screen - thus your last TextView will not have enough space.

You can solve it using 3 (or more) methods

  1. Assign fixed height to the ScrollView
  2. Assign weight to the ScrollView (and maybe also to the rest of the Views)
  3. Use the New PercentRelativeLayout. just assign height percent for the ScrollView to capture.
royB
  • 11,685
  • 14
  • 48
  • 77