10

[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, but I´m still not able to center a textview´s text vertically.

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/relativelayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/icon"
            android:layout_width="@dimen/icon_width"
            android:layout_height="@dimen/icon_height"
            android:src="@drawable/picture" />

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

        </RelativeLayout>
</ScrollView>

Normally this should work with

android:gravity="center_vertical"

but it has no effect on the textview...

Weird thing is that I have a second app with the exact same code and it´s working there without any problems.

/edit

To clarify my question: This is what I have right now:
http://i.imgur.com/WwHCegq.png

This is what I want:
https://i.stack.imgur.com/m7CNT.png

sky91
  • 3,060
  • 2
  • 19
  • 26
f4bzen
  • 301
  • 2
  • 6
  • 17
  • Since you don't have extra space on height, android:gravity do not have space to center the text on, change android:gravity for android:layout_gravity – Martin Cazares Sep 10 '13 at 16:22
  • Why do you have `android:layout_alignBottom="@+id/icon"` `android:layout_alignTop="@+id/icon"` as a part of the xml file? If you want the `TextView` to align center, that should be the only attribute. – BC2 Sep 10 '13 at 19:28
  • Because I thought that I then could easily center the text vertically in the textview. – f4bzen Sep 10 '13 at 20:35

8 Answers8

9

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.

lomza
  • 9,412
  • 15
  • 70
  • 85
  • This only centers the textview on the screen for me and that´s not what I want to do (please take a look on the 2 examples after the /edit in the original question) – f4bzen Sep 10 '13 at 19:12
  • No, this will center the TextView inside your relative layout which height is `wrap_content` so it wraps your ImageView and writing `android:layout_centerVertical="true"` positions TextView vertically inside this container which is kinda what you want. – lomza Sep 11 '13 at 08:56
  • No, that´s not what I want. I don´t want to center the textview in the relative layout. This is what I want: http://i.imgur.com/9T8CyT6.png And this is what I have right now: http://i.imgur.com/WwHCegq.png – f4bzen Sep 12 '13 at 02:18
  • In this case, just add another RelativeLayout which will hold just the ImageView and TextView ;) – lomza Sep 12 '13 at 06:52
4

To center text vertically precisely within the bounds of a TextView, you can use:

android:gravity="center_vertical"

If you still have a small mis-alignment, where the text doesn't look perfectly vertically centered, this could be down to your font padding. Try:

android:includeFontPadding="false"

The font padding sometimes moves the text slightly off-centre so this can help.

1

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.

FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
  • Well, but shouldn´t the textview be as high as the imageview since I´m using `android:layout_alignBottom="@+id/icon"` & `android:layout_alignTop="@+id/icon"` ? Also, I want to center the text vertically, not hoizontally – f4bzen Sep 10 '13 at 17:21
1

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

`android:layout_centerVertical="true"`
Houssem
  • 36
  • 2
  • 1
    but you should also eliminate android:layout_alignBottom="@+id/icon" android:layout_alignTop="@+id/icon" before adding: android:layout_centerVertical="true" I have tested it, and it worked for me – Houssem Sep 10 '13 at 17:49
  • This only centers the textview on the screen for me and that´s not what I want to do (take a look on the 2 examples after the /edit in the original question) – f4bzen Sep 10 '13 at 19:07
  • @f4bzen please try to be more specific in your description, so we can help you – Houssem Sep 11 '13 at 07:43
  • Please look at the edit in the original question: This is what I have right now: http://i.imgur.com/WwHCegq.png This is what I want: http://i.imgur.com/9T8CyT6.png – f4bzen Sep 12 '13 at 02:21
1

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)

FlorianB
  • 2,188
  • 18
  • 30
0

I think you can try one of these ways:

  1. in TextView ,set android:layout_centerVertical="true"

or

  1. as the 1st answer, but I think it should be android:layout_height="match_parent"

why your code in second app works, I guess , maybe the height of image equals the textView

  • The images are more than twice as big in both apps as the text(view), so that shouldn´t be the problem. The only difference is, that the first one was created with eclipse, the second one with android studio. And as mentioned in the other answers, `android:layout_height="match_parent"`didn´t work as well. – f4bzen Sep 10 '13 at 17:30
  • sorry, I'm focus on you code carefully before. Can you try to set the TextView height to android:layout_height="0dp" instead of "wrap_content"? I think wrap_content height conflict with the alignTop and Bottom – daddy in LA Sep 10 '13 at 17:41
0

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" />
wSakly
  • 387
  • 1
  • 10
  • This only centers the textview on the screen for me and that´s not what I want to do (please take a look on the 2 examples after the /edit in the original question) – f4bzen Sep 10 '13 at 19:14
  • You need to clarify your question more, you can take this officiel doc in consideration. http://developer.android.com/guide/topics/ui/layout/relative.html – wSakly Sep 10 '13 at 22:35
  • PLease look at my edit in the original question: This is what I have right now: http://i.imgur.com/WwHCegq.png This is what I want: http://i.imgur.com/9T8CyT6.png – f4bzen Sep 12 '13 at 02:20
  • i have replaced the TextView with this, i have added android:layout_alignParentTop="true" to make it in the top of your Relative Layout – wSakly Sep 12 '13 at 07:50
  • 1
    Sadly that didn't fix it, but I was sort of able to fix myself: I had to add `android:fillViewport="true"` to the ScrollView and the text in my TextView is now centered perfectly, even with my own code. – f4bzen Sep 12 '13 at 16:20
0

for others that have this problem there is a bug in Relative Layouts after api 18 see https://code.google.com/p/android/issues/detail?id=59368

a fix that worked for me was to replace my relative layout with this code (found in the above link)

DISCLAIMER : this only worked in one specific situation. It failed horrible in another. Feel free to try it out if you want

public class FixRelativeLayoutBug extends RelativeLayout {
    public FixRelativeLayoutBug(Context context) {
        super(context);
    }

    public FixRelativeLayoutBug(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FixRelativeLayoutBug(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    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 not work with 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);
    }
}
Tyler Davis
  • 2,420
  • 2
  • 23
  • 19