Search code examples
androidandroid-layouttextviewandroid-scrollview

Why TextView intersects with ScrollView?


Why my TextView intersects with ScrollView at the right side of Activity and how to fix it?

This is screenshot of my Activity.

Example

This is code XML of my Activity.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginTop="16dp"
    android:orientation="vertical">

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

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

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="@string/rules"
                android:textSize="@dimen/text_font_tall"
                android:textStyle="bold|italic" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="start"
                android:text="@string/rules_text"
                android:textSize="@dimen/text_font_medium" />

        </LinearLayout>
    </ScrollView>
</LinearLayout>

Solution

  • Use margin for the second LinearLayout the same way as you used for the first LinearLayout.

    android:layout_margin="16dp"
    

    BTW: as long as the parameters are the same for all of the margins (layout_marginTop, layout_marginBottom, layout_marginLeft and layout_marginRight) you can simply use only one attribute: layout_margin

    BTW2: Do you really need the parent LinearLayout? you can try it like that:

    <ScrollView>
        <LinearLayout>
            <TextView/>
            <TextView/>
        </LinearLayout>
    </ScrollView>