0

Having fiddled with this for longer than I care to admit, how does text alignment work within a TextView?

enter image description here

Obviously I'm trying to get the 'N' to centre itself vertically in the TextView.

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/titleN"
    android:id="@+id/textView"
    android:height="114dp"
    android:textSize="120dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignBottom="@+id/textView3"
    android:layout_gravity="center"
    android:gravity="center"/>

When I remove height it looks like:

enter image description here

user1561108
  • 2,666
  • 9
  • 44
  • 69
  • Please post the code which shows what you have done. We cannot tell you how to fix this without knowing what you did in the first place. – Code-Apprentice Feb 04 '16 at 17:07
  • Is it because the `height` is less than the `textSize`? You don't need this value at all. – tallpaul Feb 04 '16 at 17:12
  • I've tried many different values trying to get it to line up with the TextViews next to it. I'll use whatever values work... – user1561108 Feb 04 '16 at 17:13
  • Remove android:height="114dp" attribute from your textview. Or Update android:height="114dp" to android:height="120dp". – Adarsh Yadav Feb 04 '16 at 17:13
  • Different fonts have differently alignment. What do you want to achieve? Maybe try setting the text in right as layout_alignBottom – Keshav Feb 04 '16 at 17:14
  • It doesn't help that the simulator dimensions are off compared to the Moto G that I test on. I'm trying to get the 'N' bottom to align with the bottom sentence next to it. – user1561108 Feb 04 '16 at 17:15
  • What happens if you remove the `layout_gravity` and `gravity` attributes? – Code-Apprentice Feb 04 '16 at 17:27
  • If you are setting the text size explicitly you can use `wrap_content` for the height of the text view. However, note that text size should be given in sp, not dp. – zgc7009 Feb 04 '16 at 17:29
  • wrap_content isn't a valid value for height? @Code-Apprentice nothing – user1561108 Feb 04 '16 at 17:30
  • @zgc7009 The OP has already set `layout_height` to `wrap_content`. `height` is different...and I'm not sure what the point is of having two attributes seemingly for the same thing.. – Code-Apprentice Feb 04 '16 at 17:31
  • My fault, looked at it wrong :P Yea I agree with @Code-Apprentice no need to set a height attribute in this case. Having the wrap_content attribute should handle the height for you. Not that it should have an effect, but try using sp instead of dp for the text size. – zgc7009 Feb 04 '16 at 17:31
  • Try align_bottom textView3 to textView – Keshav Feb 04 '16 at 18:02
  • I found this, see if it helps. http://stackoverflow.com/a/6594320/2346980 – Keshav Feb 04 '16 at 18:05
  • @Keshav the 'blue box' alignment appears correct, it's the position of the text within that is the issue, though from your link setting android:layout_marginTop to a negative figure appears to work for me – user1561108 Feb 04 '16 at 19:17

3 Answers3

0

The problem is that the TextView's height(114dp) is smaller than the size of the text (120dp) itself, remove the height attribute from the TextView, you don't need this line

android:height="114dp" // you need to remove this attribute from the TextView

What you need is android:gravity="center_vertical"

when the gravity of the textview is set for center_vertical text will remain in the center irrespective of the textView's height

Example:-

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:text="N"
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

Output

enter image description here

Pankaj Nimgade
  • 4,529
  • 3
  • 20
  • 30
0

as per @Keshav's link to https://stackoverflow.com/a/6594320/2346980, setting android:layout_marginTop to a negative value corrected the text position inside TextView.

Community
  • 1
  • 1
user1561108
  • 2,666
  • 9
  • 44
  • 69
0

Try using maring attributes, like margin-top or margin-bottom. If that doesnt work try using padding, like padding-top or padding-bottom

otboss
  • 621
  • 1
  • 7
  • 16