Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable/disable an App Widgets in Android based on sdk version?

I need to implement a widget that uses Stackview which is supported on sdk 11 and higher. My app uses sdk 8, so can I enable/disable a widget based on the sdk version?

like image 929
Gallal Avatar asked Dec 04 '22 19:12

Gallal


1 Answers

If the app widget is to be totally unavailable, mark the <receiver> as disabled in the manifest (android:enabled="false"). Then, on first run of your app, check your version and conditionally enable the app widget via PackageManager and setComponentEnabledSetting(). The only downside is that your app widget will not show up as available until they launch your app for the first time.

If the app widget is simply to have different behavior on different versions, use res/layout-v11/ for your StackView implementation and res/layout/ for your non-StackView implementation. Your AppWidgetProvider can check the API level and configure the RemoteViews and related stuff accordingly.

like image 91
CommonsWare Avatar answered Jan 05 '23 00:01

CommonsWare