Search code examples
androidandroid-layoutscrollviewandroid-imageviewandroid-scrollview

ScrollView goes on top of another layout Android


In the following code I've got two main containers, a RelativeLayout and a ScrollView. I need the RelativeLayout to be on top fixed and below that I need the ScrollView with Images to be scrollable. I've got the following two questions:

  1. Though my ScrollView is scrollable but it goes on top of the RelativeLayout. !important
  2. The view of my images present within the vertical LinearLayout are thumbnail sized. If I have 15-20 images, they take a lot of vertical space. How do I have the images to fit the maximum in a row based on the screen size and then go to the next row?

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
        </LinearLayout>
    </RelativeLayout>
    
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
            <ImageView>
            </ImageView>
    
            <ImageView>
            </ImageView>
    
            <ImageView>
            </ImageView>
    
            <ImageView>
            </ImageView>
    
        </LinearLayout>
    </ScrollView>
    

Solution

  • Follow these in simple way:

    1. You need to use a fixed height for the parent, i.e., <RelativeLayout>.
    2. Set the height of the ScrollView to match_parent.
    3. Make sure you put everything inside a LinearLayout.

    Finally you would end up with this:

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