3
votes

I'm using xamarin.forms and displaying Bindable picker on screen.

And I'm using following code to update initial text of Picker contol.

        public class MyCustomBindablePickerRenderer :PickerRenderer
        {
            // Override the OnElementChanged method so we can tweak this renderer post-initial setup
            protected override void OnElementChanged (ElementChangedEventArgs<Picker> e)
            {
                base.OnElementChanged (e);

                //Background = A Android.Graphics.Color.Rgb (0, 0, 0);
                if (Control != null) { 
                    // do whatever you want to the textField here!

                    Control.SetBackgroundColor(global::Android.Graphics.Color.Rgb(241,241,241));
                    //Control.ShadowRadius = 2;
                    //Control.SetCursorVisible = true;


                    var label = (TextView)Control; // for example
                    Typeface font = Typeface.CreateFromAsset (Forms.Context.Assets, "avenir-book.ttf");
                    label.Typeface = font;
                    label.TextSize = CustomFonts.EntryFontSize;
                    label.SetHintTextColor (global::Android.Graphics.Color.Rgb(162,162,162));
                    label.SetTextColor (global::Android.Graphics.Color.Rgb(162,162,162));


                }
            }
        }

Now, i want to increase the size of popup that appears while clicking the text.

I have googled and found event OnWindowFocusChanged, but the event fires after popup appears so there is no way i can access the height of popup.

Can any one suggest the event fires when i click on textbox of picker control?

And how can I change height of the popup?

1

1 Answers

2
votes

You need to create a SpinnerAdapter, so you can customize each cell. Override GetDropDownView method like below:

[assembly: ExportRenderer(typeof(UIPicker), typeof(UIPickerRenderer))]
namespace SampleApp.Android
{
    public class UIPickerRenderer: ViewRenderer<UIPicker, Spinner>
    {
        bool initialized = false;
        Spinner picker;
        static int selectedIndex=-1;
        protected override void OnElementChanged (ElementChangedEventArgs<UIPicker> e)
        {

            base.OnElementChanged (e);
            if(e.OldElement == null)
            {
                Element.SelectedIndexChanged += NotifyVM;

                selectedIndex = Element.SelectedIndex;
                this.picker = new Spinner(Forms.Context, SpinnerMode.Dropdown);
                var adapter = new SpinnerAdapter(Forms.Context, Android.Resource.Layout.SimpleSpinnerItem,Element.Items);

                picker.Adapter = adapter;
                picker.SetSelection(selectedIndex);
                picker.ItemSelected += picker_ItemSelected;
                this.SetNativeControl(picker);
                Element.SelectedIndexChanged += (object s, EventArgs args) => { 
                    if (picker.SelectedItemPosition != Element.SelectedIndex) 
                        picker.SetSelection(Element.SelectedIndex); 
                };
            }
        }

        private void picker_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
        {
            if(! initialized)
            {
                initialized = true;
                return;
            }
            Element.SelectedIndex = picker.SelectedItemPosition;
            var g = picker.SelectedItemPosition;
        }

        private async void NotifyVM(object sender, EventArgs e)
        {

            if (selectedIndex == Element.SelectedIndex)
                return;
            selectedIndex = Element.SelectedIndex;


            if (Element.ParentView.BindingContext != null && Element.IsVisible && Element.ShouldUpdateServices)
            {
               //Do your binding things
            }
        }

        internal class SpinnerAdapter : ArrayAdapter<string>
        {
            public SpinnerAdapter(Android.Content.Context context, int id, System.Collections.Generic.IList<string> items)
                : base(context, id, items)
            {
            }

            public override Android.Views.View GetDropDownView(int position, Android.Views.View convertView, Android.Views.ViewGroup parent)
            {
                convertView = base.GetDropDownView(position, convertView, parent);

                convertView.Visibility = Android.Views.ViewStates.Visible;
                FormsViewGroup.LayoutParams p = convertView.LayoutParameters;
                p.Height = 60; //adjusting cell height
                p.Width = 300; //Adjusting cell width
                convertView.LayoutParameters = p;
                return convertView;
            }
        }

    }
}