Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ScrollView cuts off the top and leaves space at the bottom

When I launch the emulator and enter the screen which uses this code it shows most of the text information but cuts off the top of the screen (cant scroll up) but leaves a bit of space at the bottom.

Here is the code;

    <ScrollView     xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent"      android:visibility="visible"     android:fillViewport="true"     android:id="@+id/backgroundImage" >   <LinearLayout     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:orientation="vertical"     android:layout_gravity="center"     android:padding="10dip" >       <ImageView         android:id="@+id/earthSymbolImageView"         android:layout_width="25dp"         android:layout_height="25dp"         android:src="@drawable/earthsymbol" />       <TextView         android:id="@+id/earth_content1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="@string/earth_title"         android:gravity="center"          android:textColor="#FFFFFF"         android:textSize="20sp" />      <TextView         android:id="@+id/earth_content2"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="@string/earth_text"          android:textColor="#FFFFFF" />      <Button         android:id="@+id/backButton"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="@string/back" />       </LinearLayout>     </ScrollView> 

Does anyone know why would this be happening?

like image 893
Maynn Avatar asked Apr 10 '13 10:04

Maynn


People also ask

What is the difference between ScrollView and horizontal ScrollView?

ScrollView and HorizontalScrollView has same attributes, the only difference is scrollView scroll the child items in vertical direction while horizontal scroll view scroll the child items in horizontal direction.

What is ScrollView layout?

In Android, a ScrollView is a view group that is used to make vertically scrollable views. A scroll view contains a single direct child only. 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.

Which scrolling is supported by ScrollView?

Android supports vertical scroll view as default scroll view.


2 Answers

This is being caused because of the layout_gravity in your LinearLayout. Since your LinearLayout is inside a ScrollView you are probably just trying to center horizontally (centering vertically inside a ScrollView doesn't make since). Specifying your LinearLayout to center horizontally like this should do the trick:

<LinearLayout     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:orientation="vertical"     android:layout_gravity="center_horizontal"     android:padding="10dip" > 
like image 164
DavidR Avatar answered Oct 04 '22 07:10

DavidR


I had a the same problem with aHorizontalScrollViewnested inside aScrollView.I had theHorizontalScrollViewgravity set tocenterwhich was causing the problem. Just removing it fixed the issue.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"         android:layout_width="match_parent"         android:layout_height="match_parent">  <HorizontalScrollView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center">      <LinearLayout             android:layout_width="wrap_content"             android:layout_height="wrap_content">          <TextView                 android:layout_width="1000dp"                 android:layout_height="1000dp"                 android:gravity="center"                 android:layout_gravity="center"                 android:text="New Text"/>     </LinearLayout> </HorizontalScrollView> 
like image 21
Trevin Avery Avatar answered Oct 04 '22 07:10

Trevin Avery