2
votes

I have a custom picker with this code in PCL and android that are shown below.

https://xamgirl.com/picker-with-right-side-icon-in-xamarin-forms/

The problem is when I open the picker and press on the background the picker will not open again. I tried to press ok and cancel the picker will act normally and can get focus again

Is there a way to handle the background click to the picker to prevent it from popping unless I click on cancel or ok?

PCL:

   using Xamarin.Forms;

namespace AppXamarin
{
    public class CustomPicker : Picker
    {
        public static readonly BindableProperty ImageProperty =
            BindableProperty.Create(nameof(Image), typeof(string), typeof(CustomPicker), string.Empty);
        public string Image
        {
            get { return (string)GetValue(ImageProperty); }
            set { SetValue(ImageProperty, value); }
        }
    }
}

Android:

using Android.Content;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.Support.V4.Content;
using AppXamarin;
using AppXamarin.Droid;
using Plugin.CurrentActivity;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(CustomPicker), typeof(CustomPickerRenderer))]
namespace AppXamarin.Droid
{
    public class CustomPickerRenderer : PickerRenderer
    {
        CustomPicker element;
        public CustomPickerRenderer(Context context) : base(context)
        {
            AutoPackage = false;
        }

        protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
        {
            base.OnElementChanged(e);

            element = (CustomPicker)this.Element;

            if (Control != null && this.Element != null && !string.IsNullOrEmpty(element.Image))
            {
                Control.Background = AddPickerStyles(element.Image);   
            }
        }
        public LayerDrawable AddPickerStyles(string imagePath)
        {
            ShapeDrawable border = new ShapeDrawable();
            border.Paint.Color = Android.Graphics.Color.Gray;
            border.SetPadding(10, 10, 10, 10);
            border.Paint.SetStyle(Paint.Style.Stroke);

            Drawable[] layers = { border, GetDrawable(imagePath) };
            LayerDrawable layerDrawable = new LayerDrawable(layers);
            layerDrawable.SetLayerInset(0, 0, 0, 0, 0);

            return layerDrawable;
        }
        private BitmapDrawable GetDrawable(string imagePath)
        {
            //int resID = Resources.GetIdentifier(imagePath, "drawable", this.Context.PackageName);
            var drawable = Resources.GetDrawable(imagePath);
            var bitmap = ((BitmapDrawable)drawable).Bitmap;
            var result = new BitmapDrawable(Resources, Bitmap.CreateScaledBitmap(bitmap, 70, 70, true));
            result.Gravity = Android.Views.GravityFlags.CenterVertical;
            return result;
        }

    }
}
3
Add your code let me take a lookFreakyAli
My code is same as in the URL in my question, but I edited my question with the code of PCL and android rendersmohammad anouti
SO what happens is, you click on the picker it opens and the next time you click it doesn't?FreakyAli
first time I click it opens, when I click on ok or cancel, the next time I click it opens normally. But when I click on the background of the picker it will close and when I try to open it again it will not open. If you got confused I can try to rephrase what I saidmohammad anouti
I understood your issue, is it happening in android or iOS bothFreakyAli

3 Answers

2
votes

There are actually two PickerRenderer's the Xamarin.Forms.Platform.Android.AppCompat.PickerRenderer and the basic Xamarin.Forms.Platform.Android one, the basic one has this issue what you actually need to do is something like for you Android Renderer:

using Android.Content;
using Android.Graphics;
using Android.Graphics.Drawables;
using Xamarin.Forms.Platform.Android;
using AppXamarin;
using AppXamarin.Droid;
using Plugin.CurrentActivity;
using Xamarin.Forms;


[assembly: ExportRenderer(typeof(CustomPicker), typeof(CustomPickerRenderer))]
namespace AppXamarin.Droid
{
     public class CustomPickerRenderer : Xamarin.Forms.Platform.Android.AppCompat.PickerRenderer
    {
        private CustomPicker element;
        public CustomPickerRenderer(Context context) : base(context)
        {
            AutoPackage = false;
        }
        protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
        {
            base.OnElementChanged(e);
            element = (CustomPicker)this.Element;

            if (Control != null && this.Element != null && !string.IsNullOrEmpty(element.Image))
            {
                Control.Background = AddPickerStyles(element.Image);
            }
        }
        public LayerDrawable AddPickerStyles(string imagePath)
        {
            ShapeDrawable border = new ShapeDrawable();
            border.Paint.Color = Android.Graphics.Color.Gray;
            border.SetPadding(10, 10, 10, 10);
            border.Paint.SetStyle(Paint.Style.Stroke);

            Drawable[] layers = { border, GetDrawable(imagePath) };
            LayerDrawable layerDrawable = new LayerDrawable(layers);
            layerDrawable.SetLayerInset(0, 0, 0, 0, 0);

            return layerDrawable;
        }
        private BitmapDrawable GetDrawable(string imagePath)
        {
            //int resID = Resources.GetIdentifier(imagePath, "drawable", this.Context.PackageName);
            var drawable = Resources.GetDrawable(imagePath);
            var bitmap = ((BitmapDrawable)drawable).Bitmap;
            var result = new BitmapDrawable(Resources, Bitmap.CreateScaledBitmap(bitmap, 70, 70, true));
            result.Gravity = Android.Views.GravityFlags.CenterVertical;
            return result;
        }
    }
}
0
votes

I just solved it by downgrading my Xamarin.Forms version, I had a pre-release version installed. Thank you all for your help!

0
votes

just inherit following,it's working fine for me

public class CustomPickerRenderer : Xamarin.Forms.Platform.Android.AppCompat.PickerRenderer