Search code examples
androidxmltextviewscrollview

Android XML Scrollview


I'm trying to put multiple TextViews below eachother in one ScrollView, but when I do this it makes my App crash. How can I put the same text twice underneath eachother?

  <ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="20dp"
    android:layout_marginBottom="20dp"
    android:layout_below="@id/linear">

    <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="@string/lorem_ipsum"/>

    <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="@string/lorem_ipsum"/>
  </ScrollView> 

Solution

  • As per android docmentaion :-

    http://developer.android.com/reference/android/widget/ScrollView.html

    A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects. A child that is often used is a LinearLayout in a vertical orientation, presenting a vertical array of top-level items that the user can scroll through.

    Scrollview always contains only one child layout. And Linear layout have orientation property to manage child layout either horizontal or vertical.

    Your code must be look like as following:

    <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="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/lorem_ipsum"/>
    
             <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/lorem_ipsum"/>
       </LinearLayout></ScrollView>
    

    I think the above code helps you.