How to Add Multiple Widgets in the Same App

How to add multiple widgets in the same app?

You need a receiver definition for each type in your manifest file like:

    <receiver android:name=".MyWidget" android:label="@string/medium_widget_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/medium_widget_provider" />
</receiver>

<receiver android:name=".MyWidget" android:label="@string/large_widget_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/large_widget_provider" />
</receiver>

This would allow you to have the same AppWidgetProvider class be used for multiple widgets, with different widget names and different sizes defined in the <appwidget-provider> XML.

Now if you need more differences in your widgets than what is in the <appwidget-provider> XML I would create a base widget class that implements all the common behavoir between the different types:

public abstract class MyBaseWidget extends AppWidgetProvider

And then each of your concrete implementations could extend MyBaseWidget. Then in your manifest file you would have a receiver definition for each of your concrete implementations like:

    <receiver android:name=".MyMediumWidget" android:label="@string/medium_widget_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/medium_widget_provider" />
</receiver>

<receiver android:name=".MyLargeWidget" android:label="@string/large_widget_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/large_widget_provider" />
</receiver>

I want to be able to have multiple widgets in a page, it isn't working though. I want to have containers like the image below. It only allows me text?

You can use the IndexedStack widget inside your homePage build method

replace

  body: Container(
color: Colors.black87,
child: Center(
child: Text(
_options[_currentIndex],
style: const TextStyle(
color: Colors.cyanAccent,
fontWeight: FontWeight.bold,
fontSize: 40),
),
),
),

to

   body: IndexedStack(
children: _options,
index: _currentIndex,
),

how to add multiple widgets?

I'm offering multiple widgets in my app and my manifest declarations are exactly the same as yours, so I would say your issue may be living in your code and not in your manifest.

Hope this helps pal,

-serkan

Way to efficiently create multiple same widgets with different text

You can extract your widget as Widget with text parameter. So just use the widget anywhere and pass text argument now you will get same widget with different text.

Issue when implementing two AppWidgetProvider for two different Android Home Screen Widgets

I see both AppWidgetProviders are identical except for their layouts; so you would create an abstract parent class that has an abstract Int field for the layout int resource id that you must set in each widget sub-class with the corresponding layout resource.

And keep any shared code normally in the parent class. Now the RemoteViews can be implemented in the parent class with that Int field which has been set in the implemented child classes.

Here's the implementation of that:

Parent class:: make it abstract, add the abstract widgetLayout field; and keep the shared functionality

And for the error you face; you need to keep the internal fun updateAppWidget as a part of your parent class.

abstract class ParentWidgetProvider : AppWidgetProvider() {

abstract var widgetLayout: Int // must be overiden by the child classes with the widget layout

override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
// There may be multiple widgets active, so update all of them
for (appWidgetId in appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId)
}
}

override fun onEnabled(context: Context) {
// Enter relevant functionality for when the first widget is created
}

override fun onDisabled(context: Context) {
// Enter relevant functionality for when the last widget is disabled
}

private fun updateAppWidget(context: Context, appWidgetManager: AppWidgetManager, appWidgetId: Int) {
val widgetText = context.getString(R.string.appwidget_text)
// Construct the RemoteViews object
val views = RemoteViews(context.packageName, widgetLayout) //
views.setTextViewText(R.id.appwidget_text, widgetText)

// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views)
}
}

And override the widgetLayout in the child classes:

Small widget

class EXAMPLEWidgetSmall : ParentWidgetProvider() {

override var widgetLayout
get() = R.layout.example_widget_small
set(_) {R.layout.example_widget_small}

}

Medium widget (the same but with different layout file)

class EXAMPLEWidgetMedium : ParentWidgetProvider() {

override var widgetLayout
get() = R.layout.example_widget_medium
set(_) {R.layout.example_widget_medium}

}


Related Topics



Leave a reply



Submit