Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

no gravity for scrollview. how to make content inside scrollview as center

I want the content inside the scrollView as center.

<ScrollView     android:id="@+id/scroller"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:paddingTop="12dp"     android:paddingBottom="20dp"     android:scrollbarStyle="outsideOverlay"     android:layout_gravity="center" >      <Button          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="check"          android:gravity="center_vertical|center_horizontal"/>  </ScrollView> 

Note: there is no android:gravity attribute for scrollvew.

any sol:-

like image 535
Padma Kumar Avatar asked Jan 24 '12 10:01

Padma Kumar


People also ask

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 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.

Can we use ScrollView in RelativeLayout in Android?

ScrollView is a ViewGroup that make the view hierarchy placed within it scrollable. You may only need to place one other ViewGroup inside it as a child. To add many Views to ScrollView, just directly add them to the child ViewGroup such as: RelativeLayout, LinearLayout….


2 Answers

I had the same issue and finally figured it out. This is for a vertical ScrollView.

Put your ScrollView inside a RelativeLayout and center it in the RelativeLayout. In order for this to work, your ScrollView should have

android:layout_height="wrap_content" 

This is how the final code should look like:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent">      <ScrollView         android:id="@+id/scrollView1"         android:layout_width="fill_parent"         android:layout_height="wrap_content"          android:layout_centerVertical="true" >          <LinearLayout             android:id="@+id/linearLayout1"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:orientation="vertical">              <Button                 android:id="@+id/button1"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:text="b1"/>             <Button                 android:id="@+id/button2"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:text="b2"/>             <Button                 android:id="@+id/button3"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:text="b3"/>         </LinearLayout>      </ScrollView>  </RelativeLayout> 
like image 70
hadi Avatar answered Oct 27 '22 10:10

hadi


How about this?

<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/scrollView1"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:paddingTop="12dp"     android:paddingBottom="20dp"     android:scrollbarStyle="outsideOverlay" >       <LinearLayout         android:id="@+id/linearLayout1"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:orientation="vertical"          android:layout_gravity="center"         android:gravity="center">          <Button             android:id="@+id/button1"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="check"              android:gravity="center_vertical|center_horizontal"/>     </LinearLayout>  </ScrollView> 
like image 43
Ghost Avatar answered Oct 27 '22 12:10

Ghost