Forum Xamarin.Forms

How to make a horizontally centered Entry expand to be just wide enough to show it's entire Text ?

JohnHardmanJohnHardman GBUniversity admin

How can one reliably implement a horizontally centered Entry that expands to the width required to show its Text, without expanding beyond that?
(I want a readonly Entry of the minimum width that shows its entire Text, where the readonly Entry is centered horizontally on the page).

Some code that demonstrates an Entry with HorizontalOptions = CenterAndExpand but where the Entry does not expand to show its entire Text can be found at https://xamarin.github.io/bugzilla-archives/59/59391/bug.html

TBH, I do have a hacky workaround, that involves using a Grid to overlay the Entry on a Label that has its Text property bound to the same property on the ViewModel as the Entry, but that's hacky as I have to apply a Margin to the Label using some magic numbers. In that hacky workaround, the Label expands to show the entire Text, resulting in the Grid expanding, resulting in the Entry also expanding. I'd rather have a solution that doesn't involve magic numbers.

Answers

Sign In or Register to comment.