Skip to content Skip to sidebar Skip to footer

Can´t Center Textview´s Text Vertically

[Solved] I had to add android:fillViewport='true' to the ScrollView, that fixed the problem with the text not centering vertically. I know this has been answered many times before

Solution 1:

To achieve the result you want, it will be enough to remove

android:gravity="center_vertical"
android:layout_alignBottom="@+id/icon"
android:layout_alignTop="@+id/icon"

and add

android:layout_centerVertical="true"

instead.


Solution 2:

android:gravity specifies how a parent will position its children. In this case the parent is the TextView and the child is the actual text within that view. If the TextView (parent) is not wider than the text (child) then gravity will have no effect.

The android:gravity docs for reference:

Specifies how to align the text by the view's x- and/or y-axis when the text is smaller than the view.

Set the width of your TextView like this:

android:layout_width="match_parent"

That should cause the Text to get centered vertically.


Solution 3:

When your are using Relative Layout, you can add to your TextView:

`android:layout_centerVertical="true"`

Solution 4:

One solution that DOES work (all the above don't) if you are only concerned about your text itself, is to make the textView just as high as the imageView (by aligning it to top without margin, and bottom to id of "other views") and then use android:gravity="center" in this text box (make sure no other alignments interfere with this gravity)


Solution 5:

replace your TextView with this code and it will work fine

        <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/icon"
        android:layout_alignParentTop="true"
        android:layout_alignTop="@+id/icon"
        android:layout_toRightOf="@+id/icon"
        android:shadowColor="@color/shadow"
        android:shadowRadius="5"
        android:text="@string/title_text"
        android:textColor="@color/color"
        android:textSize="@dimen/textsize"
        android:textStyle="bold" />

Post a Comment for "Can´t Center Textview´s Text Vertically"