How to center in ScrollView and LinearLayout
Asked Answered
O

4

5

Please have a look at the following code

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DisplayResult" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >

        <ImageView
            android:id="@+id/zodiac1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/aries"
            />

        <TextView
            android:id="@+id/orTxt"
            android:text="Or"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            />

        <ImageView 
            android:id="@+id/zodiac2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/gemini"
            />

    </LinearLayout>




    </ScrollView>

I need to center everything inside the LinearLayout. I must use scrollview because when the app is 100% done, there will be lot of information which cannot be viewed by a single glance, without scrolling. How can make these center? Please help!

UPDATE

use any layout inside the scroll, no prob. But I need those elements to be in the same order (one after another) and centered.

Osher answered 27/12, 2012 at 19:41 Comment(1)
P
16

you need to set gravity as centered for linear layout

< LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="horizontal" >
Pickel answered 27/12, 2012 at 19:47 Comment(2)
is there a way to do that programmaticaly?Eda
find you linearlayout onCreate view then ln.setGravity(Gravity.CENTER);Pickel
R
3

This one worked for me:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">

<ScrollView 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
>



 <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center"
Rogelioroger answered 6/8, 2013 at 9:54 Comment(0)
C
0

Try using layout_gravity on your LinearLayout to center_horizontal:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_height="wrap_content"
    android:orientation="horizontal"/>
Cima answered 27/12, 2012 at 19:45 Comment(0)
Z
0
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DisplayResult"
    scrollbars="horizontal">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_horizontal">

        <ImageView
            android:id="@+id/zodiac1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/aries"
            />

        <TextView
            android:id="@+id/orTxt"
            android:text="Or"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            />

        <ImageView 
            android:id="@+id/zodiac2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/gemini"
            />

    </LinearLayout>




    </ScrollView>

#If you want to Linear layout in a center you need to write android:gravity="center_horizontal" in your linear layout.

Zodiac answered 17/6, 2020 at 18:8 Comment(2)
in what ways your answer differs with the one already marked as an answer? Could you explain?Noonberg
Also, it's recommended to wite a little explanation about your solution.Noonberg

© 2022 - 2024 — McMap. All rights reserved.