Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

AlertDialog.Builder Rendererer from Picker (sharing code).

$
0
0

Code is raw and has not been cleaned up, but allows you to create an Alert modal window for the picker control. code works, nothing to handle the events of the AlertDialog control. I share the solution as is if asked.

[assembly: Xamarin.Forms.ExportRenderer(typeof(MyPicker), typeof(PickerDemo.Android.MyPickerRenderer))]
namespace PickerDemo.Android
{
    public class MyPickerRenderer : Xamarin.Forms.Platform.Android.PickerRenderer
    {
        private EditText native;
        private MyPicker picker;
        private AlertDialog alert;
        protected override void OnElementChanged (Xamarin.Forms.Platform.Android.ElementChangedEventArgs<Xamarin.Forms.Picker> e)
        {
            base.OnElementChanged (e);

            if (native == null) {

                picker = (MyPicker)e.NewElement;

                native = (EditText)this.Control;
                native.Click += (sender, e2) => {
                    AlertDialog.Builder builder = new AlertDialog.Builder (Xamarin.Forms.Forms.Context);
                    builder.SetTitle ("Error");
                    builder.SetMessage ("This is a test message");
                    var l = (LayoutInflater)Xamarin.Forms.Forms.Context.GetSystemService(
                        global::Android.App.Application.LayoutInflaterService);

                    var cx = global::Android.App.Application.Context;

                    var t = cx.GetSystemService(global::Android.App.Application.WindowService);

                    var window = Xamarin.Forms.Forms.Context.GetSystemService(
                        global::Android.App.Application.WindowService);

                    var window2 = MainActivity.Window;


                    View v = l.Inflate(Android.Resource.Layout.PickerForm, null);
                    ListView lst = (ListView)v.FindViewById(Android.Resource.Id.listView1);
                    Button b = (Button)v.FindViewById(Android.Resource.Id.button1);
                    b.Click += (object s, EventArgs evt) => {
                        alert.Cancel();
                    };
                    var items = new string[] { "Vegetables Vegetables Vegetables Vegetables Vegetables Vegetables END","Fruits","Flower Buds","Legumes","Bulbs","Tubers",
                        "Vegetables","Fruits","Flower Buds","Legumes","Bulbs","End" };
                    var adapter = new ArrayAdapter<String>(Xamarin.Forms.Forms.Context, 
                        global::Android.Resource.Layout.SimpleListItem1, items);
                    //lst.Adapter = adapter;


                    var ax = (ActivityManager)Xamarin.Forms.Forms.Context.GetSystemService(
                        global::Android.App.Application.ActivityService);



                    lst.Adapter = new MyListViewAdapter(items);
                    builder.SetView(v);
                    v.SetMinimumWidth( (int)(window2.DefaultDisplay.Width * 0.80f));
                    v.SetMinimumHeight( (int)(window2.DefaultDisplay.Height * 0.80f));

                    builder.SetCancelable (false);
//                  builder.SetPositiveButton("Ok", delegate {
//                      var done = 1;
//                  });
                    alert = builder.Create();
                    alert.Show();
                    //builder.Show ();
                };      
            }
        }
    }
}

Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>