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
Activity Not Started, Its Current Task Has Been Brought to the Front
Android Play Services 6.5: Locationclient Is Missing
File Res/Drawable/Abc_Ic_Ab_Back_Material.Xml from Drawable Resource Id #0X7F020016
Gcm Service_Not_Available on Android 2.2
Firebase Auth Using Phone Number and Password
Overriding Referenced Style Attributes
Is Ondestroy Not Always Called
Upload an Image and Audio in One Request in Android
How to Get the Fragment Instance from the Fragmentactivity
Android Path to Asset Txt File
Android: Determine Active Input Method from Code
Not Able Disable Home Button on Specific Android Devices
Adb Server Is Out of Date. Killing
Starting Frame-By-Frame Animation
All Com.Android.Support Libraries Must Use the Exact Same Version
How to Get the Current Orientation (Activityinfo.Screen_Orientation_*) of an Android Device