-2

So I have a problem with centering a text inside a TextView. I want the text to be placed in the center of my Textview. Here is f my code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#EAEAEA"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:weightSum="3">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/Ciech"
        android:src="@drawable/ciech"
        android:layout_marginTop="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"/>
</LinearLayout>


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="@drawable/border"
    android:layout_margin="5dp">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView

            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="sampletext"
            android:layout_margin="16dp"/>
    </ScrollView>
</LinearLayout>


    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="16dp">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/globus"
                android:src="@drawable/grid_world"
                />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="sampletext"
                android:textColor="#000000"
                android:textStyle="bold"
                android:layout_alignBottom="@+id/globus"
                android:layout_toEndOf="@+id/globus"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/globus"
                android:gravity="center" />
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/koperta"
                android:src="@drawable/close_envelope"
                android:layout_below="@+id/globus"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="sampletext"
                android:textColor="#000000"
                android:textStyle="bold"
                android:layout_alignBottom="@+id/koperta"
                android:layout_alignTop="@+id/koperta"
                android:layout_toRightOf="@+id/koperta"
                android:gravity="center" />
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/torba"
                android:src="@drawable/portfolio_black_tool"
                android:layout_below="@+id/koperta"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="sampletext"
                android:textColor="#000000"
                android:textStyle="bold"
                android:layout_alignBottom="@+id/torba"
                android:layout_toEndOf="@+id/torba"
                android:layout_alignTop="@+id/torba"
                android:layout_toRightOf="@+id/torba"
                android:gravity="center" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/znaczek"
                android:src="@drawable/map_marker"
                android:layout_below="@+id/torba"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="sampletext"
                android:textColor="#000000"
                android:textStyle="bold"
                android:layout_alignBottom="@+id/znaczek"
                android:layout_toEndOf="@+id/znaczek"
                android:layout_alignTop="@+id/znaczek"
                android:layout_toRightOf="@+id/znaczek"
                android:gravity="center" />

        </RelativeLayout>
        </ScrollView>
    </LinearLayout>

</LinearLayout>

When I look at the preview in Android Studio it shows me: this And it's exactly what I want. However, when I run the emulator, then it suddenly changes and looks like: this

Can someone help me out with this one, I'm baffled

1 Answers1

2

Add: android:textAlignment="center" to the TextViews you want to be centered.

I don't know the real reason behind this. Look up below link for textAlignment vs gravity. Text/Layout Alignment in Android (textAlignment, gravity)

Community
  • 1
  • 1