Xamarin.forms: [Android] HorizontalTextAlignment="Center" loses alignment when scrolled

Created on 18 Dec 2017  路  7Comments  路  Source: xamarin/Xamarin.Forms

Moved from https://bugzilla.xamarin.com/show_bug.cgi?id=49311

Description

I've confirmed this issue. I also confirmed the Label is sized full width, and this issue is still present with Fast Renderers.

From original Bugzilla description:

It's a bit odd, but sometimes the alignment of a Label inside a StackLayout inside a Frame (inside a ViewCell) is messed up.

If you scroll, then sometimes the Label is not centered and sometimes it's centered.

Expected Behavior

Text should be always centered on the screen

Actual Behavior

Text occasionally appears left aligned when scrolled.

Basic Information

  • Version with issue: 2.3.3.175, 2.5.0.121934
  • Last known good version: ?
  • IDE:
  • Platform Target Frameworks:

    • Android: 8.0

  • Android Support Library Version: 25.4.0.2

Screenshots

https://github.com/lhaussknecht/CenterBug/blob/master/assets/centerBug.gif

https://www.dropbox.com/s/z4hzr7rl9o2lvqb/Screenshot%202017-12-18%2009.04.09.png?dl=0

https://www.dropbox.com/s/evk3zhdu0ivfi5p/Screenshot%202017-12-18%2009.23.53.png?dl=0

Reproduction Link

https://github.com/lhaussknecht/CenterBug

Android bug

Most helpful comment

FYI: It isn't just when scrolled. If you are in a view (Page) and you navigate forward and back... there's a 50:50 change the Label will left-justify again.

Workaround

So this sort of shows the approach you have to take. You can't try to color or expand the labels themselves because they always want to shift to the left. Just accept they are going to be left-justified.
You have to nest the Label in a Grid then manipulate the Label as an object and not the text of the label. An then center the label _inside_ the grid. If you want to change the background color: Don't. Color the grid the label is in.

Here we see the Label as fushcia for ease of understanding. Imagine it transparent and you can see what the final result will look like. The Label gets centered instead of being expanded and then trying to center the text.
image

All 7 comments

Maybe related with #1396

This issue is still happening with v2.5.0.280555. Is this has been resolved ? What is the work around ?

FYI: It isn't just when scrolled. If you are in a view (Page) and you navigate forward and back... there's a 50:50 change the Label will left-justify again.

Workaround

So this sort of shows the approach you have to take. You can't try to color or expand the labels themselves because they always want to shift to the left. Just accept they are going to be left-justified.
You have to nest the Label in a Grid then manipulate the Label as an object and not the text of the label. An then center the label _inside_ the grid. If you want to change the background color: Don't. Color the grid the label is in.

Here we see the Label as fushcia for ease of understanding. Imagine it transparent and you can see what the final result will look like. The Label gets centered instead of being expanded and then trying to center the text.
image

This issue also applies for the VerticalTextAlignment

Another instance of this issue. When I exit the app on android and come back, it resets left.

https://www.screencast.com/t/SDvfVjjaMXD8

This is on 3.0.0.446417

iOS is fine.

@numb3r Someone posted a workaround on StackOverflow that works for me here

tlhintoq, your solution works for me

Was this page helpful?
0 / 5 - 0 ratings