Skip to content

Instantly share code, notes, and snippets.

@PureWeen
Last active August 29, 2019 19:40
Show Gist options
  • Save PureWeen/15a3c7ae2b774c69802827b91be921d0 to your computer and use it in GitHub Desktop.
Save PureWeen/15a3c7ae2b774c69802827b91be921d0 to your computer and use it in GitHub Desktop.
Picker
[assembly:ExportRenderer(typeof(Picker), typeof(PlaceHolderPicker), new[] { typeof(VisualMarker.MaterialVisual), typeof(CustomMaterial) })]
namespace VisualPicker.iOS
{
    public class PlaceHolderPicker : MaterialPickerRenderer, IMaterialEntryRenderer
    {
        public PlaceHolderPicker()
        {
        }

        string IMaterialEntryRenderer.Placeholder => Element?.Title;

        Color IMaterialEntryRenderer.TextColor => Color.Purple;
    }
}
namespace VisualPicker
{
    public class CustomMaterial : IVisual
    {
    }
}
<Picker
                        x:Name="Picker"
                        Title="Country Code"
                        FontSize="18"
                        SelectedIndex="0"
                        VerticalOptions="Fill"
                        Margin="0,10"
                        Visual="CustomMaterial"
                        HorizontalOptions="Fill"
                        Grid.Row="3"
                        Grid.Column="0">
                    </Picker>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.Content.Res;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using VisualPicker.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Material.Android;
using Xamarin.Forms.Platform.Android;

[assembly:ExportRenderer(typeof(Picker), typeof(PlaceHolderPicker), new[] { typeof(VisualMarker.MaterialVisual) })]
namespace VisualPicker.Droid
{
    public class PlaceHolderPicker : MaterialPickerRenderer
    {
        public PlaceHolderPicker(Context context) : base(context)
        {
        }


        protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
        {
            base.OnElementChanged(e);
            this.EditText.FocusChange += OnEditTextFocusChanged;
        }

        void OnEditTextFocusChanged(object sender, FocusChangeEventArgs e)
        {
            // workaround android bug
            Device.BeginInvokeOnMainThread(() => SetPlaceHolder());
        }

        protected override void Dispose(bool disposing)
        {
            if (EditText != null)
                EditText.FocusChange -= OnEditTextFocusChanged;

            base.Dispose(disposing);
        }

        public void SetPlaceHolder()
        {
            Control.HintEnabled = true;
            Control.Hint = Element.Title;

            int[][] States =
            {
                new []{ global::Android.Resource.Attribute.StateEnabled, global::Android.Resource.Attribute.StatePressed  },
                new int[0] { }
            };

            var colors = new int[] { Color.Gray.ToAndroid(), Color.Gray.ToAndroid() };
            Control.DefaultHintTextColor = new ColorStateList(States, colors);
        }

        protected override void ApplyTheme()
        {
            base.ApplyTheme();
            SetPlaceHolder();
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment