Getwrong
Getwrong

Reputation: 187

text alignment on a picker xamarin forms

i am having trouble with aligning the text on an expanded picker to the centre and i would have thought it would be something like this...

enter image description here

and here is the picker on the emulator...

enter image description here

How would i get that SIZE text to sit in the middle but keep the line expanded also can it be done through the xaml code instead of c# code

cheers.

Upvotes: 8

Views: 10340

Answers (4)

Diego Venâncio
Diego Venâncio

Reputation: 6007

UPDATE***

Actually is very easy:

<Picker Title="Selecione o bairro" HorizontalTextAlignment="Center"/> 

...OLD

You need to use a CustomRenderer for this.

ANDROID

[assembly: ExportRenderer(typeof(BetterPicker), typeof(BetterPickerRenderer))]

namespace MDWS.Droid
{
    public class BetterPickerRenderer : PickerRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                Control.Gravity = GravityFlags.CenterHorizontal;
            }
        }
    }
}

IOS:

[assembly: ExportRenderer(typeof(BetterPicker), typeof(BetterPickerRenderer))]

namespace YourNameSpace.iOS
{
    public class BetterPickerRenderer : PickerRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                Control.TextAlignment = UITextAlignment.Center;
            }
        }
    }
}

Upvotes: 23

Humdeep Singh
Humdeep Singh

Reputation: 11

I have just had the same issue and noticed that if you set up the android renderer using the "using"

using Xamarin.Forms.Platform.Android.AppCompat;

It retains the original styling

Upvotes: 1

Adit Kothari
Adit Kothari

Reputation: 421

Can You try LayoutOptions.Center

Otherwise we need custom renderer for this.

Upvotes: -2

EvZ
EvZ

Reputation: 12179

I am afraid you will have to implement a CustomRenderer for this.
For each platform OnElementChanged you will have to set the alignment for Picker's title.

Luckily this topic is very nicely explained in official guide.

P.S.: Welcome to stackoverflow.com, please take a moment to read this post which nicely explains why posting a screenshot of code is a bad practice.

Good luck!

Upvotes: 3

Related Questions