android:gravity fails on API 18+
Asked Answered
S

2

5

I have some TextViews on my app, I don't know why but the android:gravity attribute is not centering the text content where it should be on devices running the API 18+ (4.3+).

There is the code I use on my custom TextView, this is a child of RelativeLayout:

<com.package.custom.CustomTextFont
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/seekbar"
    android:layout_alignParentLeft="true"
    android:layout_alignTop="@+id/seekbar"
    android:layout_marginLeft="@dimen/margin_tiny_double"
    android:layout_toLeftOf="@+id/seekbar"
    android:gravity="center_vertical|left"
    android:paddingRight="@dimen/margin_tiny"
    android:text="@string/text1"
    android:textColor="@color/black"
    android:textSize="@dimen/size_text_normal" />

This code should take the edges of this TextView and align it to the top and bottom and put it to the Left of the SeekBar, this is working, but the TextView gets big, so with android:gravity I center the text to the center and left from it self. It works, but I don't know why, the text is not centered at center|left on devices running android 4.3 and 4.4. This issue can be reproduced on real devices and as well on the layout preview (Graphic layout) of Eclipse.

There is any changes made on API 18+ or on android:gravity that I'm missing?

PS: I'm targetting the API 19 on the project and on AndroidManifest.xml

PS2: My TextView is custom just to set an external font.tff

This is how it looks like on API 17-

enter image description here

This is how it looks like on API 18+

enter image description here

Thanks in advance!

= UPTADATE =

On my Manifest, I changed the android:targetSdkVersion to 17 instead of 19 and the problem disappeared, but this is a "trick", not a solution, what can I do since it could be an issue from the API ? And yes, I have the latest version of the API 18 and 19 (today, 01/30/2014).

Smite answered 29/1, 2014 at 22:53 Comment(10)
Just out of curiosity, where is the text in 4.3 and 4.4 if it's not center|left? Is the result the same with a non-custom text view? If not, show your custom textview code and your layout xml.Lecturer
can you please show us a screenshot of how it should look like, vs how it looks like? also, it seems weird that you used both "alignParentLeft" and "layout_toLeftOf" - did you want to align to a specific view or to the parent?Shanaeshanahan
I just added the ss you asked. @androiddeveloper, alignParentLeft is used to ajust the left edge of my TextView to the left edge of its "father", and toLeftOf align the right edge of my textView to the left edge of my SeekBar, this way I avoid "overwriten" if my text lenght is big enought to reach my seekbar : )Smite
@Adam, the text is still there, but like looks like there is no gravity attribute on it, the text is positioned at the top|left from it selfSmite
@MurilloHenrique yes about the alignment i am sorry. i was wrong and confused (thought it was align to the right of a view instead) and that's because i was very sleepy (wrote it at 2:22 ). anyway, i think you should just try out the ui designer for this. try to align the left textView to the view on the right. or you can also use layout_centerVertical="true" on the textView which . also, instead of re-inventing this whole view, you should try to customize an already existing API, which is SwitchPreference or CheckBoxPreference. just customize how they look...Shanaeshanahan
haven't you mixed gravity with layout_gravity?Coenzyme
@androiddeveloper, hahaha no problem : ). And the layout_centerVertical align the view to the center vertical of its parent, I have to align its content to the centerVertical of it it self. The problem is with the API, because I just changed the android:targetSdkVersion to 17 instead of 19 and the problem gone, I don't really know what is going on : (Smite
@gian1200, no, because the alignment is correct on API 17 and below, but not ont API 18 and above. I changed the android:targetSdkVersion to 17 instead of 19 and the problem disappeared, but it is a trick, not a solution : /Smite
Are you sure? Because android:gravity="center_vertical" is useless when you have android:layout_height="wrap_content"Coenzyme
Yes @gian1200, actually its height are defined by android:layout_below and android:layout_alignParentBottom, I could even set its height to 0dp for performance reasons since its height are defined by the view bounds, and I would have the same result I described.Smite
U
12

This appears to be a known issue in API 18+:

https://code.google.com/p/android/issues/detail?id=59368 https://code.google.com/p/android/issues/detail?id=59700

The problem seems to occur when a TextView is part of a scrollable container (e.g. ListView), making the view ignore the vertical gravity for some reason (some sources suggest this has to do with the TextView being the child of a RelativeLayout, though it's been my experience that this can happen even when no such layout is involved).

A possible workaround (albeit not a particularly elegant one), would be to wrap the TextView in a LinearLayout. You can then use "layout_gravity" on the TextView to center it inside the LinearLayout, instead of relying on "gravity" (just make sure to wrap_content so the text itself is properly centered).

E.g., in your example:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignBottom="@+id/seekbar"
    android:layout_alignParentLeft="true"
    android:layout_alignTop="@+id/seekbar"
    android:layout_toLeftOf="@+id/seekbar"
    android:layout_marginLeft="@dimen/margin_tiny_double" >

    <com.package.custom.CustomTextFont
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:gravity="left"
        android:paddingRight="@dimen/margin_tiny"
        android:text="@string/text1"
        android:textColor="@color/black"
        android:textSize="@dimen/size_text_normal" />

</LinearLayout>

This method does have the disadvantage of adding an otherwise-unnecessary level to your view hierarchy, but it currently seems to be the only way around this (other than reverting to an earlier API level).

Also see similar question at: Android sdk 18 TextView gravity doesn't work vertically

Ultimo answered 2/4, 2014 at 13:9 Comment(0)
L
1

Your textview height is "wrap_content", which means the height of the textview will be the same as the height of the text. If you change the background of the textview to black, it might be easier to see the bounds of the view. I'm guessing you'll find that the textview doesn't have as much height as you expect.

Try setting the height of the textview to match_parent. You can wrap the textview inside another view if needed and modify its height as appropriate.

Lecturer answered 30/1, 2014 at 15:33 Comment(1)
This have logic, I tried but nothing changed, I can change it to 0dp later for performance reasons since its size is defined but the alignment of its edges. The problem is with the API, because I just changed android:targetSdkVersion on my Manifest to 17 instead of 19 and the problem disappeared, but this it is not the right solution : /Smite

© 2022 - 2024 — McMap. All rights reserved.