Android text alignment inside textview is wrong

10,612

To problem is in the TextView itself, the large size of the text is not calculated by the gravity attribute.

Try adding this attribute to your text view android:includeFontPadding="false"

Share:
10,612
Idob
Author by

Idob

Updated on June 05, 2022

Comments

  • Idob
    Idob almost 2 years

    I'm trying to align text inside textview to the center (both vertical and horizontal). Altought the vertical alignment working properlly in the Android Studio preview, it fails on the emulator.

    The design:

    enter image description here

    The android studio preview:

    enter image description here

    The emulator:

    enter image description here

    The code:

    <RelativeLayout
        android:id="@+id/teamsBox"
        android:layout_width="match_parent"
        android:layout_height="185dp">
    
        <LinearLayout
            android:layout_width="154dp"
            android:layout_height="wrap_content"
            android:background="@drawable/team_bg"
            android:padding="6dp"
            android:layout_alignParentLeft="true"
            android:orientation="vertical">
    
            <EditText
                android:id="@+id/teamOneName"
                android:layout_width="143dp"
                android:layout_height="42dp"
                android:text="HOME"
                android:textColor="@color/my_white"
                android:background="@color/my_transparent"
                android:textSize="23sp"
                android:singleLine="true"
                android:gravity="center"/>
    
            <TextView
                android:id="@+id/teamOneScore"
                android:layout_width="143dp"
                android:layout_height="132dp"
                android:text="3"
                android:textColor="@color/my_green"
                android:textSize="127sp"
                android:gravity="center"/>
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="154dp"
            android:layout_height="match_parent"
            android:background="@drawable/team_bg"
            android:padding="6dp"
            android:layout_alignParentRight="true"
            android:orientation="vertical">
    
            <EditText
                android:id="@+id/teamTwoName"
                android:layout_width="match_parent"
                android:layout_height="42dp"
                android:text="AWAY"
                android:textColor="@color/my_white"
                android:background="@color/my_transparent"
                android:textSize="23sp"
                android:singleLine="true"
                android:gravity="center"/>
    
            <TextView
                android:id="@+id/teamTwoScore"
                android:layout_width="fill_parent"
                android:layout_height="132dp"
                android:text="1"
                android:textColor="@color/my_green"
                android:textSize="127sp"
                android:gravity="center"
                android:layout_gravity="center"/>
    
        </LinearLayout>
    
    </RelativeLayout>
    

    Edit: I've changed to layout according to the answers, but the bug is still happening. This is my new layout:

    <RelativeLayout
        android:id="@+id/teamsBox"
        android:layout_width="match_parent"
        android:layout_height="185dp">
    
        <LinearLayout
            android:layout_width="154dp"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:background="@drawable/team_bg"
            android:orientation="vertical"
            android:padding="6dp" >
    
            <EditText
                android:id="@+id/teamOneName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="HOME"
                android:textColor="@color/my_white"
                android:background="@color/my_transparent"
                android:textSize="23sp"
                android:singleLine="true"
                android:padding="8dp"
                android:gravity="center"/>
    
            <TextView
                android:id="@+id/teamOneScore"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:text="3"
                android:textColor="@color/my_green"
                android:textSize="127sp"
                android:gravity="center_vertical|center_horizontal"/>
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="154dp"
            android:layout_height="match_parent"
            android:background="@drawable/team_bg"
            android:padding="6dp"
            android:layout_alignParentRight="true"
            android:orientation="vertical">
    
            <EditText
                android:id="@+id/teamTwoName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="AWAY"
                android:textColor="@color/my_white"
                android:background="@color/my_transparent"
                android:textSize="23sp"
                android:singleLine="true"
                android:padding="8dp"
                android:gravity="center"/>
    
            <TextView
                android:id="@+id/teamTwoScore"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:text="1"
                android:textColor="@color/my_green"
                android:textSize="127sp"
                android:gravity="center"
                android:layout_gravity="center"/>
    
        </LinearLayout>
    
    </RelativeLayout>
    
  • Idob
    Idob over 9 years
    Didn't solve the problem.. I put the updated layout in the question body, you can look at it
  • Idob
    Idob over 9 years
    Didn't solve the problem.. I put the updated layout in the question body, you can look at it
  • Neo
    Neo about 8 years
    Working like a charm! Thanks dear.