ScrollView adds a lot of space at the bottom

In my main activity there are several buttons that fit together on the big screen of the tablet, but not on the screen with a smaller phone. I added ScrollView so the user can scroll down to other buttons if screen size is required:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="@drawable/bg" android:gravity="center_horizontal"> <TextView android:id="@+id/trackSelectorText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal"/> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center_horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="foo" /> <!-- More buttons --> </LinearLayout> </ScrollView> </LinearLayout> 

This view works, however, I have a large empty space below the last button (i.e., ScrollView may scroll too far). Both on real tablets and on an emulator with a phone configuration. If I center inner LinearLayout (instead of center_horizontal ), the space is evenly distributed between the top and bottom, which I do not want either one or the other. How to fix this layout?

I hope this image becomes clearer, imagine that ScrollView scrolls completely down and shows the last buttons. The empty space that I mean is the one below the button 6 in the right image:

enter image description here

+6
source share
4 answers

Actually, this was not really related to the layout (unless I was missing something). I tried to comment on the background attribute and it unexpectedly worked. The background image ( @drawable/bg ) was just too large and caused scrolling. I converted the background to a 9-patch image, so it could be scaled automatically, and voilà was extra space. However, if this were feasible without a 9 patch, I would still be interested to know if this can be done in an XML file.

+10
source

Try adding this attribute to your ScrollView element. android: fillViewport = "true"

 <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:fillViewport="true" > ... </ScrollVIew> 

Link: ScrollView Ref

+8
source

I checked with my code. Work fine:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@drawable/bg" android:gravity="center_horizontal"> <TextView android:id="@+id/trackSelectorText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal"/> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center_horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="foo" /> <!-- More buttons --> </LinearLayout> </ScrollView> </LinearLayout> 

May help you ....... !!

+1
source

you can use this attribute in scrollview to avoid extra padding at the bottom of the viewport

 android:overScrollMode="never" 
+1
source

Source: https://habr.com/ru/post/953321/


All Articles