How to enable / disable application widgets in Android based on sdk version?

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

+4
source share
2 answers

If the application widget is completely unavailable, mark <receiver> as disabled in the manifest ( android:enabled="false" ). Then, when you first launch your application, check your version and conditionally enable the application widget through PackageManager and setComponentEnabledSetting() . The only drawback is that the application widget will not appear as accessible until it launches your application for the first time.

If the application widget has simple behavior across versions, use res/layout-v11/ for your StackView implementation and res/layout/ for your StackView implementation. Your AppWidgetProvider can check the API level and configure RemoteViews and related materials accordingly.

+10
source

Make xml bools.xml in the res \ values ​​folder and add the following line

Res \ Values

 <?xml version="1.0" encoding="utf-8"?> <resources> <bool name="widgetenable">false</bool> </resources> 

and just add bool.xml to the -v11 values ​​folder also just replace widgetenable so that it is

Res \ Values-v11

  <?xml version="1.0" encoding="utf-8"?> <resources> <bool name="widgetenable">true</bool> </resources> 

and after that in your manisfest.xml in the widget receiver tag add this line

 android:enabled="@bool/widgetenable" 

This will enable or disable the sdk based widget.

+4
source

Source: https://habr.com/ru/post/1382712/


All Articles