Search code examples
androidandroid-layoutandroid-scrollview

Android text inside LinearLayout under ScrollView horizontally not wrapping correctly?


Basically my layout has header, footer and content. I'm trying to put text inside the content view. My test text Lorem ipsum... inside LinearLayout under ScrollView were not fully displayed. Seems like the wrapping was not correct. Currently it shows

<------ screen ----------> off screen

Lorem ipsum dolor sit amet, ne usu nullam maluisset

aliquando, vis choro soluta vituperata te. Usu dico

utinam labores ad.

By right it should be like this

<------ screen ----------> off screen

Lorem ipsum dolor sit

amet, ne usu nullam

maluisset aliquando, vis

choro soluta vituperata

te. Usu dico utinam

labores ad.

Here's my xml layout

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

    <!-- Header aligned to top -->

    <RelativeLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" >

        <LinearLayout
          android:layout_weight="1"
            android:id="@+id/header"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" 
            android:gravity="right" >

            <Button
            android:id="@+id/buttonTest"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Test" />

        </LinearLayout>


    </RelativeLayout>

    <!-- Footer aligned to bottom -->

    <RelativeLayout
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="center" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="Footer"
            android:textColor="#000"
            android:textSize="20sp" />
    </RelativeLayout>

    <!-- Content below header and above footer -->

    <RelativeLayout
        android:id="@+id/content"
        android:background="#FFFFFF"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@id/footer"
        android:layout_below="@id/header"
        android:gravity="center" >

        <LinearLayout
            android:background="#FFFFFF"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_centerVertical="true"
            android:weightSum="4" >

            <ListView
                android:layout_weight="3"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:choiceMode="singleChoice" >
            </ListView>

            <View
             android:id="@+id/divider"
             android:layout_width="1dp"
             android:layout_height="fill_parent"
             android:background="#000" />

             <ScrollView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                <LinearLayout
                android:layout_weight="1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >

                    <TextView
                        android:layout_marginLeft="10dp"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="left"
                        android:singleLine="false"
                        android:textColor="#000"
                        android:text="Lorem ipsum dolor sit amet, ne usu nullam maluisset aliquando, vis choro soluta vituperata te. Usu dico utinam labores ad. Prompta scaevola iudicabit eu vim, ex nec sint nemore. Duo legimus nusquam an, ei facer inermis concludaturque ius. In sed nihil impedit senserit." />

                </LinearLayout>
            </ScrollView>

        </LinearLayout>

    </RelativeLayout>

</RelativeLayout>

Any idea how to fix this?


Solution

  • Move your layout weight to ScrollView instead for LinearLayout, the part should be like below

     <ScrollView
                 android:layout_weight="1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
    
                <LinearLayout
    
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >