Picker Custom Renderer not working properly!

I have implemented an custom android renderer for a Picker , just a basic one i found online. But the issue i have found, is when i click the picker to show the dialog options, on the first click it doesn't show the custom changes(default dialog) but when i close the picker option and then click the picker again to show the dialog option, then it shows my custom dialog changes.

I have tried using it with using PickerRenderer = Xamarin.Forms.Platform.Android.AppCompat.PickerRenderer; as well;

Any help would be appreciated thanks.

Stef

renderer added below:

using System;
using System.Linq;
using Android.App;
using Android.Content;
using Android.Widget;
using BindablePicker;
using BindablePicker.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Color = Android.Graphics.Color;
using Orientation = Android.Widget.Orientation;

[assembly: ExportRenderer(typeof(CustomPicker), typeof(CustomPickerRenderer))]
namespace BindablePicker.Droid
{
public class CustomPickerRenderer : PickerRenderer//, ViewRenderer<Picker, EditText>
{

    IElementController ElementController => Element;


    public CustomPickerRenderer(Context context) : base(context)
    {
        AutoPackage = false;

    }


    private AlertDialog _dialog;


    protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
    {
        base.OnElementChanged(e);
        Control.Click += Control_Click;
    }


    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            Control.Click -= Control_Click;
            //var picker = (Picker)Element;
            //picker.PropertyChanged -= Control_Click;
        }


        base.Dispose(disposing);
    }

    private void Control_Click(object sender, EventArgs e)
    {
        Picker model = Element;
        model.Title = "changed";

        var picker = new NumberPicker(Context);

        if (model.Items != null && model.Items.Any())
        {
            // set style here

            picker.MaxValue = model.Items.Count - 1;
            picker.MinValue = 0;

            picker.SetBackgroundColor(Color.Purple);
            picker.SetDisplayedValues(model.Items.ToArray());
            picker.WrapSelectorWheel = false;
            picker.Value = model.SelectedIndex;

        }


        var layout = new LinearLayout(Context) { Orientation = Orientation.Vertical };
        layout.AddView(picker);
        layout.SetBackgroundColor(Color.Purple);

        var titleView = new TextView(Context);

        titleView.Text = "hmmmm";
        titleView.SetBackgroundColor(Color.ForestGreen);

        ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, true);

        var builder = new AlertDialog.Builder(Context);
        builder.SetView(layout);

        builder.SetTitle(model.Title ?? "");

        //builder.SetCustomTitle(titleView);
        builder.SetNegativeButton("Cancel  ", (s, a) =>
        {
            ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
            // It is possible for the Content of the Page to be changed when Focus is changed.
            // In this case, we'll lose our Control.
            Control?.ClearFocus();
            _dialog = null;
        });
        builder.SetPositiveButton("Ok ", (s, a) =>
        {
            ElementController.SetValueFromRenderer(Picker.SelectedIndexProperty, picker.Value);
            // It is possible for the Content of the Page to be changed on SelectedIndexChanged.
            // In this case, the Element & Control will no longer exist.
            if (Element != null)
            {
                if (model.Items.Count > 0 && Element.SelectedIndex >= 0)
                    Control.Text = model.Items[Element.SelectedIndex];
                ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
                // It is also possible for the Content of the Page to be changed when Focus is changed.
                // In this case, we'll lose our Control.
                Control?.ClearFocus();
            }
            _dialog = null;
        });

        Control.Text = "Control";

        _dialog = builder.Create();
        _dialog.DismissEvent += (ssender, args) =>
        {
            ElementController?.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
        };
        _dialog.Show();
    }

}

}

Best Answer

  • stefman123stefman123 ✭✭
    Accepted Answer

    Hi, I just updated my xamarin forms version from 3.4.0.1008975 to the latest 3.4.0.1029999, that seemed to work.

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    I tested your renderer on my side, it worked properly the first time I clicked the Picker:

    Could you please share a sample to help us reproduce your issues?

  • stefman123stefman123 Member ✭✭

    Hi LandLu, Thanks for your reply.

    I have attached an simple sample that doesn't work for me. I hoping maybe it possibly the simulator i am using , which i am attached.

    Thanks
    Stef

  • LandLuLandLu Member, Xamarin Team Xamurai

    @stefman123 It could be. I tested your sample but got the same result. Try to increase your emulator's ram size, usually, I set it to 2048.
    And do you have a real device? You can try it again on a real device.

  • stefman123stefman123 Member ✭✭

    @LandLu I have a pixel and it still shows my issue. It possibly could be my android SDK's setup below.

    I cant think what else it could be really and i am going to update my visual studio as well now, but really doubt it would be that.

  • stefman123stefman123 Member ✭✭
    Accepted Answer

    Hi, I just updated my xamarin forms version from 3.4.0.1008975 to the latest 3.4.0.1029999, that seemed to work.

Sign In or Register to comment.