Introduction
In this tutorial, we will learn how to handle the Placeholder issue in Xamarin.Forms for Picker Control. Xamarin.Forms Picker Control is a combination of EditText plus AlertDialog for Android and UITextField plus UIPicker for iOS. The Title property of Xamarin.Forms Picker Control is a property to set title for AlertDialog plus Hint for EditText for Android and Placeholder for UITextField.
For example, we are setting the title property of the picker control is "Title" and it has items source and selected index of the control as "-1" then the control will shows "Title" in Placeholder and title of the alert or popup. Sometimes, it is meaning less to use this control due to this.
To avoid this issue, I have created a customer render for Android and iOS to override this default behavior of picker. Without much delay, we will skip into the coding part of the article.
Steps
I have split the coding part into 5 steps as in the following.
- Step 1 - Creating new Xamarin.Forms Projects.
- Step 2 - Creating Custom Picker inherited from picker.
- Step 3 - Custom Renderer for Custom Picker in Android.
- Step 4 - Custom Renderer for Custom Picker in iOS.
- Step 5 - Implementation of the Custom Picker Control.
Step 1 - Creating new Xamarin.Forms Projects
- Create new project by selecting Xamarin Cross Platform App and click OK.

- Then select Android, iOS and UWP Platforms as shown below with Project template as "Blank" and click OK.

Step 2 - Creating Custom Picker inherited from picker
- Create a Custom Picker Control class named as “PLPicker” and it is inherited from Xamarin.Forms Picker.
- Create a property for Placeholder and is named as Placeholder like below.
- public class PLPicker : Picker
- {
- public static readonly BindableProperty PlaceholderProperty = BindableProperty.Create(
- propertyName: nameof(Placeholder),
- returnType: typeof(string),
- declaringType: typeof(string),
- defaultValue: string.Empty);
- public string Placeholder
- {
- get { return (string)GetValue(PlaceholderProperty); }
- set { SetValue(PlaceholderProperty, value); }
- }
- }
Step 3 - Custom Renderer for Custom Picker in Android
In this step, we will create a custom renderer in Android for PLPicker created in “step 2”.
- Create a class named as “PLPickerRenderer.cs” and it is inherited from “Xamarin.Forms.Platform.Android.AppCompat.PickerRenderer”.
- Then create a method “UpdatePickerPlaceholder” and copy the below code in the custom renderer class.
- void UpdatePickerPlaceholder()
- {
- if (picker == null)
- picker = Element as PLPicker;
- if (picker.Placeholder != null)
- Control.Hint = picker.Placeholder;
- }
- Call this method, in the OnElementChanged method and OnElementPropertyChanged method when the placeholder property changes.
- You can find the full coding part for “PLPickerRenderer.cs” below for Android.
- [assembly: ExportRenderer(typeof(PLPicker), typeof(PLPickerRenderer))]
- namespace PickerPlaceholder.Droid
- {
- public class PLPickerRenderer : Xamarin.Forms.Platform.Android.AppCompat.PickerRenderer
- {
- PLPicker picker = null;
- public PLPickerRenderer(Context context) : base(context)
- {
- }
- protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
- {
- base.OnElementChanged(e);
- if (e.NewElement != null)
- {
- picker = Element as PLPicker;
- UpdatePickerPlaceholder();
- if (picker.SelectedIndex <= -1)
- {
- UpdatePickerPlaceholder();
- }
- }
- }
- protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
- {
- base.OnElementPropertyChanged(sender, e);
- if (picker != null)
- {
- if (e.PropertyName.Equals(PLPicker.PlaceholderProperty.PropertyName))
- {
- UpdatePickerPlaceholder();
- }
- }
- }
- protected override void UpdatePlaceHolderText()
- {
- UpdatePickerPlaceholder();
- }
- void UpdatePickerPlaceholder()
- {
- if (picker == null)
- picker = Element as PLPicker;
- if (picker.Placeholder != null)
- Control.Hint = picker.Placeholder;
- }
- }
- }


Join the conversation! Your thoughts help the community grow.