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.