Android home screen widget with .NET MAUI

Hrvoje Hudo 46 Reputation points
2023-07-17T16:00:18.17+00:00

Does new .NET MAUI with .NET 7 supports building of Android (home screen) widget application?

Can't find any reference to widgets or code example (except this https://marcofolio.net/app-widget-xamarin-android/ looks like not MAUI, but old Xamarin app).

Developer technologies .NET .NET MAUI
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2023-07-18T02:31:39.8833333+00:00

    Hello,

    After investigation and testing, you could use Android widgets in MAUI.

    Please refer to the following steps.

    Step 1. Open the Platforms/Android/Resources folder in the MAUI project and create layout, xml and drawable folders in it.

    Step 2. The interface for creating widgets under the layout folder.

    Widget.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/widgetBackground"
          android:orientation="vertical">
          <TextView
                android:id="@+id/widgetMedium"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="test" />
          <TextView
                android:id="@+id/widgetSmall"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="test1" />
    </LinearLayout>
    

    Step 3. Add a picture as a preview image of the widget under the drawable folder. In the test, I added a file called "test.png".

    Step 4. Create appwidget-provider-metadata in the xml folder. In the test, I added a file called "appwidgetprovider.xml".

    <?xml version="1.0" encoding="utf-8"?>
    <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
        android:minWidth="288dip"
        android:minHeight="72dip"
        android:resizeMode="horizontal"
        android:minResizeWidth="144dip"
        android:updatePeriodMillis="1000"
        android:initialLayout="@layout/Widget"
        android:previewImage="@drawable/test" />
    

    Step 5. Create the AppWidgetProvider class in the Android folder. In the test, I added a file called "appwidgetprovider.xml".

    // Add Exported = true for your app to run as expected in Android 12 and above.
     [BroadcastReceiver(Label = "HellApp Widget" , Exported = true)]
        [IntentFilter(new string[] { "android.appwidget.action.APPWIDGET_UPDATE" })]
        [MetaData("android.appwidget.provider", Resource = "@xml/appwidgetprovider")]
        [Service(Exported = true)]
        public class AppWidget : AppWidgetProvider
        {
            public override void OnUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
            {
                var me = new ComponentName(context, Java.Lang.Class.FromType(typeof(AppWidget)).Name);
                appWidgetManager.UpdateAppWidget(me, BuildRemoteViews(context, appWidgetIds));
            }
    
            private RemoteViews BuildRemoteViews(Context context, int[] appWidgetIds)
            {
                var widgetView = new RemoteViews(context.PackageName, Resource.Layout.Widget);
    
                SetTextViewText(widgetView);
    
                return widgetView;
            }
            private void SetTextViewText(RemoteViews widgetView)
            {
                widgetView.SetTextViewText(Resource.Id.widgetMedium, "HelloAppWidget");
                widgetView.SetTextViewText(Resource.Id.widgetSmall,
                    string.Format("Last update: {0:H:mm:ss}", DateTime.Now));
            }
    
            public override void OnReceive(Context context, Intent intent) { }
           
        }
    

    After that, you can deploy the program to your device and find the widget by long pressing the app icon.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.