Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to provide different android widget for 1.6 and 3.0+?

I am targeting 1.6 but I would like to have nice widget that can use stackview and other improvement Android SDK provides for widget if user is on 3.0 above device. and a plain widget on 1.6-2.3/

How should I do that two version of widget?

Thanks a lot

like image 634
cheng yang Avatar asked May 02 '12 17:05

cheng yang


2 Answers

My recommendation:

Create two versions of the app widget and use the resource values 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>
like image 125
dagalpin Avatar answered Nov 23 '22 17:11

dagalpin


You can make sub folders in your res directory API level specific by adding a suffix like -v11.

For example, suppose your layout is called main.xml. You can have your 1.6 - 2.3 main.xml file in the layout folder, and then put your new, fancy main.xml file that includes your new widget in the layout-v11 folder. When using Honeycomb and up, the layout in the -v11 folder will be chosen when you refer to your file like R.layout.main.

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

For more details on this, check out the Qualifier name rules from the Android documentation.

like image 44
wsanville Avatar answered Nov 23 '22 19:11

wsanville