Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center content in scroll view

I want to center my LinearLayout within ScrollView. When LinearLayout's height is small it's centered alright (see image #1) but when LinearLayout's height is bigger than the screen's height then it behaves strange. I cannot see the top of LinearLayout (see image #2) and at the bottom of ScrollView there's huge padding. I don't know what's happening here. When there are lots of content in LinearLayout the whole screen should look like in image #3.

image #1
image #2
image #3

Here's my layout file:

            <?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"                 android:background="#cccfff" >                  <LinearLayout                     android:layout_width="match_parent"                     android:layout_height="wrap_content"                     android:layout_gravity="center_vertical"                     android:layout_margin="28dp"                     android:background="#ffffff"                     android:orientation="vertical"                     android:paddingBottom="40dp"                     android:paddingLeft="20dp"                     android:paddingRight="20dp"                     android:paddingTop="40dp" >                      <ImageView                         android:layout_width="wrap_content"                         android:layout_height="wrap_content"                         android:layout_marginBottom="16dp"                         android:src="@drawable/ic_launcher" />                      <TextView                         android:id="@+id/tip_title"                         android:layout_width="wrap_content"                         android:layout_height="wrap_content"                         android:layout_marginBottom="12dp"                         android:text="Title"                         android:textColor="@color/orange_text"                         android:textSize="@dimen/font_size_medium" />                      <TextView                         android:id="@+id/tip_description"                         android:layout_width="wrap_content"                         android:layout_height="wrap_content"                         android:text="Description description..."                         android:textSize="@dimen/font_size_small" />                 </LinearLayout>              </ScrollView> 
like image 565
Egis Avatar asked Oct 23 '13 11:10

Egis


People also ask

How do I center something in scroll view?

To do vertical centering when using ScrollView with React Native, we can set the contentContainerStyle prop to an object with some flexbox styles. to expand the ScrollView to fit the View . And we set justifyContent to 'center' to center align the ScrollView .

What is fillViewport in ScrollView?

fillViewport allows scrollView to extend it's height equals to the full height of device screen's height in the cases when the child of scroll view has less height.

What is scroll view 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.

How do I use scrollTo in ScrollView React Native?

To scroll to top of the ScrollView with React Native, we assign a ref to the ScrollView and call scrollTo on the ref's value. to create a ref with useRef and set that as the value of the ref prop of the ScrollView . Then we add a Button that calls ref. current.


2 Answers

You can use android:fillViewport="true" to center the content. Something like this:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:fillViewport="true">         <!-- content --> </ScrollView> 
like image 106
yaircarreno Avatar answered Sep 18 '22 23:09

yaircarreno


You should use an external horizontal LinearLayout. It's working for me in the same situation.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:gravity="center"         android:orientation="horizontal" >     <ScrollView             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:orientation="vertical" >         <LinearLayout                 android:orientation="vertical"                 android:layout_width="match_parent"                 android:layout_height="wrap_content"                 android:gravity="center_horizontal" >             <TextView                     android:text="@string/your_text"                     android:layout_width="wrap_content"                     android:layout_height="wrap_content" />         </LinearLayout>     </ScrollView> </LinearLayout> 
like image 33
Vladimir Petrakovich Avatar answered Sep 20 '22 23:09

Vladimir Petrakovich