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>
Community
  • 1
  • 1
Nick Fanelli
  • 265
  • 1
  • 6
  • 19
  • did you try layout_gravity instead of gravity on yout textview? – PedroHawk Sep 09 '14 at 14:52
  • I hadn't tried it before your answer. It does solve the problem, as well as @Tanis.7x answer does. Thank you! – Nick Fanelli Sep 09 '14 at 15:15
  • 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. – Nick Fanelli Sep 09 '14 at 15:17
  • upvote both and simply choose one to close your question... preferably that resolves directly your kind of problem. – PedroHawk Sep 09 '14 at 15:29
  • 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?) – Nick Fanelli Sep 09 '14 at 15:31
  • http://stackoverflow.com/tour – PedroHawk Sep 09 '14 at 15:35

3 Answers3

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.

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
  • 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.7x – Nick Fanelli Sep 09 '14 at 15:14
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>
PedroHawk
  • 622
  • 5
  • 19
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"
 />

Khalid Almannai
  • 162
  • 1
  • 1
  • 13