3
votes

I already have an app created in Xamarin Forms that works in Android and iOS.

Now, I want to create an Android Widget.

Can I use existing Xamarin Forms pages and create an Android Widget out of it?

If so, is there a sample for the same.

I am unable to find much help for it. Even on Xamarin Forms Forums.

1
I am not sure if it works , but how about if you use CustomeReneder ? So in this case you can use PageRenderer and then you can capture it with "OnElementChanged" or something like that...Ahmad ElMadi

1 Answers

0
votes

I'm creating a toast message on the top of page my when i receive a push notification messages like this, so u can create widgets like this (or pages i suppose).Hope that helps u.

var pw = new Android.Widget.PopupWindow(Xamarin.Forms.Forms.Context); //create a window
var layout = new Android.Widget.RelativeLayout(Xamarin.Forms.Forms.Context);
pw.ContentView = layout; //create your layout holder
pw.Width = 100; //set window sizes
pw.Height = 100;
pw.WindowLayoutType = WindowManagerTypes.Toast; //window type (there are some other types too, just investigate :)
var tv = new Android.Widget.TextView(Xamarin.Forms.Forms.Context); //create a text and add to your layout holder
tv.Text = "Hello";
layout.AddView(tv);
pw.ShowAtLocation(layout, GravityFlags.Top, 0, 0); //show your window

You can dismiss the window via pw.Dissmiss() but i'm using a timerbecause it is a toast

var timer = new System.Timers.Timer();
            timer.Interval = 5000; //5 seconds
            timer.Elapsed += (sender, e) =>
            {
                timer.Stop();
                Device.BeginInvokeOnMainThread(() =>
                {
                    pw.Dismiss();
                });
            };
            timer.Start();

You can inflate a predefined xaml too i think but you have to google a little :)