Eidenai
Eidenai

Reputation: 441

Can Anyone Tell Me Why This Text Is Not Centering In Android Studio?

I'm attempting to align the text as you would expect to see if you were in a word pad and set your alignment to center, the middle of each line would be in the center of the screen and each margin to the left and right would be equidistant. However, using gravity does not appear to be working as the text is still starting from the left. Could someone shed light as to why? Here's the xml.

<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:paddingLeft="3dp"
    android:paddingRight="3dp"
    android:paddingTop="3dp"
    android:paddingBottom="3dp"
    android:orientation="vertical"
    android:background="#111111"
    tools:context="eqlogic.annswingsandthings.AboutUs">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/chalkBoard"
        android:src="@drawable/ann_chalkboard_img"
        android:layout_weight="0.5"/>
    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:id="@+id/scrollView"
        android:layout_gravity="center_horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:textColor="#999999"
            android:text="@string/AboutUsText"
            android:textSize="24sp"
            android:id="@+id/textView"
            android:layout_gravity="center_horizontal"
            android:singleLine="false" />
    </ScrollView>
</LinearLayout>

Upvotes: 0

Views: 56

Answers (4)

SriMaharshi Manchem
SriMaharshi Manchem

Reputation: 534

Set scrollview parameters Android:layout_width="match_parent" and Android:gravity="center".

Upvotes: 0

Hussein El Feky
Hussein El Feky

Reputation: 6707

Make your layout_width be fill_parent and use gravity instead of layout_gravity:

<TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="2dp"
        android:textColor="#999999"
        android:text="@string/AboutUsText"
        android:textSize="24sp"
        android:id="@+id/textView"
        android:gravity="center"
        android:singleLine="false" />

Upvotes: 0

cw fei
cw fei

Reputation: 1564

Set also android:gravity parameter in ScrollView to center.

For testing the effects of different layout parameters I recommend to use different background color for every element, so you can see how your layout changes with parameters like gravity, layout_gravity or others.

Upvotes: 1

Nir Alfasi
Nir Alfasi

Reputation: 53535

You are not setting the gravity, use:

android:gravity="center"

Full tag:

<TextView
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="2dp"
        android:textColor="#999999"
        android:text="@string/AboutUsText"
        android:textSize="24sp"
        android:id="@+id/textView"
        android:layout_gravity="center_horizontal"
        android:singleLine="false" />

Upvotes: 1

Related Questions