Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrollview doesn't scroll to the margin at the bottom

Tags:

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.

like image 256
Mehmet Katircioglu Avatar asked Jun 15 '13 14:06

Mehmet Katircioglu


People also ask

What is nested scroll view?

NestedScrollView is just like ScrollView , but it supports acting as both a nested scrolling parent and child on both new and old versions of Android. Nested scrolling is enabled by default.

How do I scroll horizontally in ScrollView?

In android, You can scroll the elements or views in both vertical and horizontal directions. To scroll in Vertical we simply use ScrollView as we shown in the previous code of this article and to scroll in horizontal direction we need to use HorizontalScrollview.

What is the difference between ScrollView and horizontal ScrollView?

In order to place multiple views in the scroll view, one needs to make a view group(like LinearLayout) as a direct child and then we can define many views inside it. A ScrollView supports Vertical scrolling only, so in order to create a horizontally scrollable view, HorizontalScrollView is used.

What is the difference between ScrollView and ListView?

ScrollView is used to put different or same child views or layouts and the all can be scrolled. ListView is used to put same child view or layout as multiple items. All these items are also scrollable. Simply ScrollView is for both homogeneous and heterogeneous collection.


1 Answers

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> 
like image 51
Mehmet Katircioglu Avatar answered Oct 01 '22 00:10

Mehmet Katircioglu