Search code examples
androidandroid-layouttextviewandroid-relativelayoutandroid-studio-2.0

Android not centering TextView's text vertically on device/emulator (but fine though in Android Studio's designer view)


I have this setup:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:id="@+id/postHeader">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="CP"
            android:id="@+id/initialsView"
            android:layout_alignTop="@+id/avatarView"
            android:layout_alignLeft="@+id/avatarView"
            android:layout_alignBottom="@+id/avatarView"
            android:layout_alignRight="@+id/avatarView"
            android:background="@drawable/avatar_background"
            android:textColor="@color/white"
            android:gravity="center"
            android:textStyle="bold"
            android:textSize="16sp" />

        <com.makeramen.roundedimageview.RoundedImageView
            app:riv_corner_radius="20dp"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/avatarView"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="14dp"
            android:layout_marginLeft="20dp"
            android:layout_marginBottom="6dp"
            app:riv_border_color="@color/lightGray"
            app:riv_border_width="0.2dp" />

    ...
/>

The TextView aligns correctly to the image view. Everything is fine in Android Studio:

enter image description here

However, when I run on actual device (Android 4.4.4) or emulator (Android 6.0) I get the text ("CP" in the screenshot) stuck all the way to the top, just like I haven't set a vertical gravity. I've tried setting textAlignment, width and height to match_parent, layout_centerInParent to both, gravity to center_vertical explicity, includeFontPadding to false, but no avail. It still doesn't center vertically (horizontal is fine though).

What am I doing wrong? Why is it displayed correctly on Android Studio anyway?


Solution

  • Okay I've figured out the problem after a bit investigation. Seems that it has been broken: https://code.google.com/p/android/issues/detail?id=63673

    Implementing the solution at the link solved my problem:

      private static final int AT_MOST_UNSPECIFIED = MeasureSpec.makeMeasureSpec((1<<30)-1, MeasureSpec.AT_MOST);
    
      @Override
      protected void onMeasure(int widthSpec, int heightSpec)
      {
        // RelativeLayout has bugs when measured in UNSPECIFIED height mode that were supposedly
        // fixed in API 18. However the 'fix' does breaks gravity == center_vertical in TextView. 
        // https://code.google.com/p/android/issues/detail?id=63673
        if (MeasureSpec.getMode(heightSpec) == MeasureSpec.UNSPECIFIED) heightSpec = AT_MOST_UNSPECIFIED;
        super.onMeasure(widthSpec, heightSpec);
      }