Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set font size for items in Xamarin Picker?

I try to change the items font size in Xamarin.Picker for my Android app. In my project, I use BindablePicker that inherits from Picker class. Source here.

enter image description here

I spent some time to do research and I found that I should create a PickerRenderer class and render the Picker.

My renderer class:

public class BindablePickerRenderer : PickerRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
    {
        base.OnElementChanged(e);

        var picker = e.NewElement;
        BindablePicker bp = (BindablePicker)this.Element;

        if (this.Control != null)
        {
            var pickerStyle = new Style(typeof(BindablePicker))
            {

                Setters =
                {
                    new Setter { Property = VisualElement.BackgroundColorProperty, Value = Color.Red }

                }
            };
            picker.Style = pickerStyle;
        }
    }
}

For test purposes I set the backgroundColor for Picker and it works fine. However, in my PickerRenderer class I only have access to Control property which is type of Android.Widget.EditText.

The effect :

enter image description here

Question

How can I access to Picker items, and set the font size for them? Is this possible?

Here is my repository with an example project.

https://github.com/k8mil/PickerRendererXamarin

Related links

https://developer.xamarin.com/api/type/Xamarin.Forms.Picker/

Changing the default text color of a Picker control in Xamarin Forms for Windows Phone 8.1

Font size in Picker control in xamarin forms

like image 726
kamwysoc Avatar asked Dec 01 '16 10:12

kamwysoc


1 Answers

I has been able to resolve this by add the line:

Control.TextSize = 30;

On OnElementChanged method:

public class BindablePickerRenderer : PickerRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
    {
        base.OnElementChanged(e);

        if (this.Control != null)
        {
            Control.TextSize = 30;
        }
    }
}

Maybe this can help someone that is looking for change font size of a Bindable picker.

like image 124
Rafael Carvalho Avatar answered Sep 30 '22 21:09

Rafael Carvalho