21

I have a simple layout as follows :

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#D23456" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:background="#FFFFFF" > <ImageView android:layout_width="match_parent" android:layout_height="800dp" android:src="@drawable/ic_launcher" /> </LinearLayout> </ScrollView> 

The background of the scrollview is pink and linear layout inside has the android icon image with a height of 800dp (that doesnt fit to the screen) . What I'm expecting to see is that imageview floats in a background of pink with a margin of 10dp in every sides (top,bottom,left,right).But when I scroll to the bottom, the scrollview doesn't scroll to the margin, so the bottom of the scroll is the imageview not the pink margin.

How can I prevent this? This makes the user think the page hasn't ended yet and makes him want to scroll more.

3 Answers 3

53

I later found out that ,a similar situation has already been answered in the following thread https://stackoverflow.com/a/16885601/1474471 by @olefevre.

Adding an extra LinearLayout that surrounds the current LinearLayout with a padding and removing the inner LinearLayout's layout-margin solved the problem:

<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#D23456" android:padding="10dp" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FFFFFF" > <ImageView android:layout_width="match_parent" android:layout_height="800dp" android:src="@drawable/ic_launcher" /> </LinearLayout> </LinearLayout> </ScrollView> 
Sign up to request clarification or add additional context in comments.

3 Comments

Just an FYI, it doesn't have to be a LinearLayout. I am using a RelativeLayout.
this is the solution...it worked like a charm..! @Mehmet Katircioglu
As @mattblang has mentioned, for performance it's better to use a FrameLayout instead of the heavier Linear and Relative layouts.
19

The solution posted by @Mehmet Katircioglu works well, but you can solve the problem simply changing the android:layout_margin to android:padding, without none extra view. Like this:

<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#D23456" android:padding="10dp" > <!-- Your content (ImageView, buttons...) --> <LinearLayout/> 

Comments

1

use android:fillViewport="true" on the ScrollView may do it.

example in this thread.

1 Comment

I'm aware of this property, but doesn't work in this case (try yourself). we might be missing something.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.