How to vertically align text within TextView
Asked Answered
L

3

0

I am trying to get some text vertically aligned within a TextView, but for some reason it just sticks to the top edge of such TextView.

Trust me, I've read several questions here on Stack Overflow regarding the same, or similar, questions and tried applying the provided solutions, but nothing seems to make a change.

For instance, what I thought would solve the issue right away was to add the attribute android:gravity="center_vertical|left", but that didn't help either.

This user's question describes almost exactly my issue, except that his TextView is inside a RelativeLayout which in turn is inside a ScrollView, which I don't (and don't need or want to) use. And the images he provided are also perfectly applicable to what I'm trying to describe and achieve.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="2" >

    <ImageView
        android:id="@+id/account_server_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/account_server_icon"
        android:paddingLeft="16dp"
        android:gravity="left" />    

    <TextView
        android:orientation="vertical"
        android:id="@+id/tv_account"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="16dp"
        android:gravity="left|center_vertical" />

</LinearLayout>
Lyrism answered 9/9, 2014 at 14:46 Comment(6)
did you try layout_gravity instead of gravity on yout textview?Forsake
I hadn't tried it before your answer. It does solve the problem, as well as @Tanis.7x answer does. Thank you!Lyrism
Is there a reference to choose which answer is "more correct" between the ones both of you provided? I could accept either one, but I don't want to be unffair, as both solved my problem.Lyrism
upvote both and simply choose one to close your question... preferably that resolves directly your kind of problem.Forsake
1) Both answers resolve directly my problem. I applied both independently and they both work. 2) I can't upvote answers. I don't have 15 rep (btw, how do I get rep?)Lyrism
stackoverflow.com/tourForsake
Q
3

The problem is that your TextView's height is set to wrap_content. This means that the size of the text's container will be the same as the height of the text, thus there really is nowhere within the view to center the content (it is effectively already centered vertically).

If the LinearLayout that you posted only contains the ImageView and the TextView, then you can simple change the height to match_parent and it should work fine.

If the LinearLayout contains other Views, you might want to consider using a RelativeLayout and setting the TextView to align with both the top and bottom of the image.

Quadrivial answered 9/9, 2014 at 14:50 Comment(1)
Oh boy! That did the trick. I'm still new to developing in Android, so it wasn't as clear to me as it obviously was to you. Thank you, @Tanis.7xLyrism
F
1

as your achievement suggest, you only need to change your gravity to layout_gravity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="2" >

    <ImageView
        android:id="@+id/account_server_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/account_server_icon"
        android:paddingLeft="16dp"
        android:gravity="left" />    

    <TextView
        android:orientation="vertical"
        android:id="@+id/tv_account"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="16dp"
        android:layout_gravity="left|center_vertical" />

</LinearLayout>
Forsake answered 9/9, 2014 at 14:55 Comment(0)
E
0

You need to set gravity then you set text alignment to gravity

<TextView
    android:orientation="vertical"
    android:id="@+id/tv_account"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="16dp"
    android:gravity="left|center_vertical"
    android:textAlignment="gravity"
 />

Ermines answered 12/1, 2018 at 13:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.