Xamarin.forms: Label HorizontalTextAlignment (Center or End) is not kept when navigating back to a page

Created on 14 Dec 2017  Â·  27Comments  Â·  Source: xamarin/Xamarin.Forms

Description

The horizontal text alignment of a label is lost on Android when navigating to another page and coming back. It's not updated until the text is updated (via binding). It's the same erroneous behavior whether the Label is in a Grid or a StackLayout. It works as expected on iOS and I haven't tested other platforms.

Steps to Reproduce

  1. Have a label with HorizontalTextAlignment = Center or End
  2. Navigate to another page
  3. Come back

Expected Behavior

The text is aligned center or right as configured.

Actual Behavior

The text is aligned left until the text is updated (via binding).

Basic Information

  • Version with issue: 2.5.0.121934
  • IDE: Rider
  • Platform Target Frameworks:

    • Android: 7.1.1

  • Android Support Library Version: 26.1.01
  • Affected Devices: Android simulator

Related

It seems to be the same bug as described here : https://stackoverflow.com/questions/43147929/xamarin-label-losing-text-alignment-when-page-returned-to. Sadly, I can't use the proposed workaround which is to set HorizontalOptions=EndAndExpand or End because the Label flicker when its size changes because of text changes.

Android bug

Most helpful comment

Using HorizontalOptions in conjunction with HorizontalTextAlignment does
the trick for me.

On Wed, Jul 25, 2018, 5:34 AM Hentze90 notifications@github.com wrote:

@PureWeen https://github.com/PureWeen After a good night sleep, a few
cups of coffee, I found my issue. Sorry for causing trouble ;)

@rboyette https://github.com/rboyette can you share what you issue was,
maybe someone else (me and others) have the same issue..?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/xamarin/Xamarin.Forms/issues/1396#issuecomment-407738973,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AO9GE4sIOveMsw2RkvxBNTBNxubTjFvLks5uKGXVgaJpZM4RBV7j
.

All 27 comments

Possibly related: https://bugzilla.xamarin.com/show_bug.cgi?id=55243 - there seem to be quite a few issues with Android text alignment resetting in various ways.

Any progress on this bug? I'm having several mobile app projects suffering this issue.

I can confirm, this is happening on Android Emulator and Device for me too.

hi everyone, i have the same issules, hide the label using isvisible = false and then use isvisible= true but the label dont center the text alignment.

any suggestion serves, thanks

Label lblSaldo = new Xamarin.Forms.Label { HorizontalTextAlignment = TextAlignment.Center, TextColor = Helpers.Color.Amipass.ToFormsColor(), BackgroundColor = Color.LightGray, FontSize = 50, };

@hartez
This doesn't only happen with horizontaltextalignment, but also with verticaltextalignment

Bug still exists with Xamarin.Forms 3.0.0.446417

Exists in latest version too.

@manikandansai: What Version?

Seems to be fixed with Xamarin.Forms 3.0.0.482510. Thanks!
@manikandansai can you confirm?

I'm still seeing this issue with the version you specified @gayko1976 when HorizontalTextAlignment is set to End.

@rboyette are you seeing this issue with Labels or with Entry?

https://github.com/xamarin/Xamarin.Forms/issues/2167

@PureWeen
Seeing it with labels.

On Tue, May 29, 2018, 1:08 PM Shane Neuville notifications@github.com
wrote:

@rboyette https://github.com/rboyette are you seeing this issue with
Labels or with Entry?

2167 https://github.com/xamarin/Xamarin.Forms/issues/2167

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/xamarin/Xamarin.Forms/issues/1396#issuecomment-392926320,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AO9GE71g3OSj8yIBzCN2crHittx8MCN3ks5t3arNgaJpZM4RBV7j
.

can you provide a small repro @rboyette so we can see your issue?

@PureWeen

After a good night sleep, a few cups of coffee, I found my issue. Sorry for causing trouble ;)

Great to hear! Thank you for checking back in @rboyette

@PureWeen After a good night sleep, a few cups of coffee, I found my issue. Sorry for causing trouble ;)

@rboyette can you share what you issue was, maybe someone else (me and others) have the same issue..?

Using HorizontalOptions in conjunction with HorizontalTextAlignment does
the trick for me.

On Wed, Jul 25, 2018, 5:34 AM Hentze90 notifications@github.com wrote:

@PureWeen https://github.com/PureWeen After a good night sleep, a few
cups of coffee, I found my issue. Sorry for causing trouble ;)

@rboyette https://github.com/rboyette can you share what you issue was,
maybe someone else (me and others) have the same issue..?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/xamarin/Xamarin.Forms/issues/1396#issuecomment-407738973,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AO9GE4sIOveMsw2RkvxBNTBNxubTjFvLks5uKGXVgaJpZM4RBV7j
.

Thanks..!

Any news with that?

My label has both HorizontalOptions and HorizontalTextAlignment set to Center, but still when I'm navigating back my label is aligned to the left. I don't even know how to work around it.

@tomerdomshlak can you create a repro and open a new issue? Are you on the latest Forms version?

@PureWeen

I had this exact issue with Font Awesome labels on XF 3.2.0.637442-pre1. For me, the problem was when I backgrounded the app and came back, one of the labels on the screen would be left aligned even though its HorizontalTextAlignment was set to Center. It looks like, in my case, the fix was to not auto size the row definition of its containing grid. I've had issues before with grid auto sizing on Android like #2923.

Easy to reproduce in Android:

  • put a label in AbsoluteLayout
  • set label WidthRequest
  • at runtime update label Text (for instance with a simple TapGestureRecognizer and a command)
  • put application in background then go back to it

=> Text alignment is lost.

It seems to only happen with android:minSdkVersion >= 19.
Please find a repro attached (using 3.2.0.839982).

Xamarin.samples.navigation.zip

I tested on xamarin.forms (3.0.0.446417) with this settings

HorizontalOptions="CenterAndExpand"
HorizontalTextAlignment="Center"

Seems to be ok. My label is on Grid with row definitions

my steps was:
1- Launch my app,
2- Fill data by providing from web service
3- Tap on home button
4- Restore the app from the Apps button.

This bug is still present in 3.2.0.871581.

Why there is no fix to this issue as a simple customRenderer resolves the prob ?

@Maxima078 I tried with a fixed Height for the row, and I still got the prob. Whatever sizing option I choose, prob remains.

public override void ChildDrawableStateChanged(Android.Views.View child)
{
   base.ChildDrawableStateChanged(child);
   Control.Text = Control.Text;
}

@Maxima078 Thanks for dropping your project in here! I managed to reproduce it on your project. I've also tried (using a custom renderer) to force it to check my alignment when setting up:

Android.Widget.TextView textView = new Android.Widget.TextView(context);
switch (CustomElement.HorizontalTextAlignment)
{
    case Xamarin.Forms.TextAlignment.Start:
        textView.TextAlignment = Android.Views.TextAlignment.TextStart;
        Control.TextAlignment = Android.Views.TextAlignment.TextStart;
        break;

    case Xamarin.Forms.TextAlignment.Center:
        textView.TextAlignment = Android.Views.TextAlignment.Center;
        Control.TextAlignment = Android.Views.TextAlignment.Center;
        break;

    case Xamarin.Forms.TextAlignment.End:
        textView.TextAlignment = Android.Views.TextAlignment.TextEnd;
        Control.TextAlignment = Android.Views.TextAlignment.TextEnd;
        break;
}

However, the TextAlignment for both the textView and Control stays at Gravity. I don't know if this has anything to do with it, but I can't seem to budge this. Defaulting to Gravity might actually be causing this...

This is still happening for me on Xamarin.Forms 3.3.0.967583. Can we get this issue reopened, @hartez ?

As a workaround, I've just been wrapping my Labels in a ContentView and using Horizontal/VerticalOptions instead of Horizontal/VerticalTextAlignment.

@kpchua Could you open a new issue? Comments on closed issues tend to get buried, so a new issue will be more likely to get assigned.

Was this page helpful?
0 / 5 - 0 ratings