Skip to content Skip to sidebar Skip to footer

No Gravity For Scrollview. How To Make Content Inside Scrollview As Center

I want the content inside the scrollView as center.

Solution 1:

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"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"><ScrollViewandroid:id="@+id/scrollView1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_centerVertical="true" ><LinearLayoutandroid:id="@+id/linearLayout1"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="b1"/><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="b2"/><Buttonandroid:id="@+id/button3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="b3"/></LinearLayout></ScrollView></RelativeLayout>

Solution 2:

How about this?

<?xml version="1.0" encoding="utf-8"?><ScrollViewxmlns: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" ><LinearLayoutandroid:id="@+id/linearLayout1"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:layout_gravity="center"android:gravity="center"><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="check"android:gravity="center_vertical|center_horizontal"/></LinearLayout></ScrollView>

Solution 3:

I think a more elegant solution would be to use the ScrollView's android:fillViewport property. A ScrollView is a little different in how it treats it's content view (can only have one), even if you set match_parent (fill_parent) to the ScrollView it won't give that much spacing to it's content view, instead the default behavior is for the ScrollView to wrap the content regardless of what you specify for that view. What android:fillViewport does is tell the ScrollViewto stretch its content to fill the viewport (http://developer.android.com/reference/android/widget/ScrollView.html#attr_android:fillViewport). So in this case, your LinearLayout would be stretched to match the viewport and if the height goes behind the viewport then it will be scrollable which is exactly what you want!

The accepted answer won't work properly when the content extends beyond the ScrollView because it will still center the content view first causing it to cut off a portion of the view, and the ScrollView centered in another layout works but just doesn't feel right, besides I think it will also result in a lint error (useless parent or something along those lines).

Try something like this:

<ScrollViewandroid:id="@+id/scroller"android:layout_width="fill_parent"android:layout_height="fill_parent"android:paddingTop="12dp"android:paddingBottom="20dp"android:scrollbarStyle="outsideOverlay"android:fillViewport="true"><LinearLayoutandroid:id="@+id/content"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:gravity="center"><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="check" /></LinearLayout></ScrollView>

Just remember that the reason it is being centered here now is because of the android:gravity on the LinearLayout since the ScrollView will stretch the LinearLayout so keep that in mind depending on what you add to the layout.

Another good read on ScrollView although not about centering but about fillViewport is http://www.curious-creature.org/2010/08/15/scrollviews-handy-trick/

Solution 4:

Use this attribute in ScrollView

android:fillViewport="true"

Example

<?xml version="1.0" encoding="utf-8"?><ScrollViewxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:scrollbarStyle="outsideOverlay"android:fillViewport="true"
><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><!--Your Code goes here--></RelativeLayout></ScrollView>

Make sure to use Single Child in ScrollView just like in above example which is Relativelayout.

Solution 5:

For @Patrick_Boss - what stopped the content from cutting of in my application was to change the gravity and layout_gravity to center_horizontal for the LinearLayout.

It worked for me...but not sure if it will work for you.

Modification of @Ghost's answer -

<?xml version="1.0" encoding="utf-8"?><ScrollViewxmlns: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" ><LinearLayoutandroid:id="@+id/linearLayout1"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:layout_gravity="center_horizontal"android:gravity="center_horizontal"><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="check"android:gravity="center_vertical|center_horizontal"/></LinearLayout></ScrollView>

Post a Comment for "No Gravity For Scrollview. How To Make Content Inside Scrollview As Center"