How to provide various Android widgets for 1.6 and 3.0+?

I focus on 1.6, but I would like to have a good widget that can use stackview and other improvements. The Android SDK provides a widget if the user is located 3.0 above the device. and a simple widget on 1.6-2.3 /

How do I make this two versions of the widget?

Many thanks

+6
source share
3 answers

My recommendation:

Create two versions of the application widget and use the resource value method to enable / disable.

in res / values ​​/bools.xml:

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

in res / values-v11 / bools.xml:

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

in AndroidManifest.xml:

 <receiver android:name="MyOldAppWidgetProvider" android:enabled="@bool/notHoneycomb"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/example_oldappwidget_info" /> </receiver> <receiver android:name="MyNewAppWidgetProvider" android:enabled="@bool/atLeastHoneycomb"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/example_newappwidget_info" /> </receiver> 
+13
source

You can create auxiliary folders in your res directory API level by adding a suffix like -v11 .

For example, suppose your layout is called main.xml . You can have your file 1.6 - 2.3 main.xml in the layout folder, and then enter a new, fancy main.xml file containing the new widget in the layout-v11 . When using Honeycomb and up, the layout in the -v11 folder will be selected if you link to your file, for example R.layout.main .

From there, you can have some kind of logic in your activity that checks for the presence of your new widget object (or just check the Build.VERSION class) and branches, respectively.

For more information, see the classifier name rules in the Android documentation.

+7
source

You can check the api version of the device, for example:

int apiLevel = android.os.Build.VERSION.SDK_INT;

from apiLevel, you can use if-else to load different layouts / views for different apis /

+2
source

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


All Articles