Hide app widget depending on API level

I’ve created three app widgets with different sizes for pre Honecomb versions of Android and one resizable scrollable listview widget for Honeycomb and later.

Since the widgets offer the same functionality and the one targeted for Honeycomb and later is resizable I would like to remove the other versions of the widget on API level 12 or later. Is this possible somehow?

  • How to prevent calling onCreateView when back button pressed in fragment in android
  • Android: Speech Recognition without using google server
  • change edittext cursor color
  • How can I use onCreateOptionsMenu in a Fragment within a tab Activity? (Sherlock ActionBar)
  • What is the difference between SOAP 1.1, SOAP 1.2, HTTP GET & HTTP POST methods for Android?
  • Adb install failure: INSTALL_CANCELED_BY_USER
  • As far as I can tell there doesn’t seem to be any way to disable a widget from getting registered by the AppWidgetService if it can find the AppWidgetProviderInfo resource file. So it seems that I can only include widgets on later versions but not exclude versions defined in xml folders that targets earlier API versions.

  • Multiple choice list with custom view?
  • Why ITelephony.aidl works?
  • Xamarin Android (Visual Studio 2015) Could not connect to the debugger
  • Failure
  • Submitting tasks to a thread-pool gives RejectedExecutionException
  • Run code only once after an application is installed on Android device
  • 2 Solutions collect form web for “Hide app widget depending on API level”

    If it were a single app widget that had pre-HC or HC implementations, you could combine them into one AppWidgetProvider and use res/xml-v11/ and res/xml/ to have different metadata.

    The only way I can think of to handle your scenario, though, is to mark some of the AppWidgetProviders as disabled in the manifest (android:enabled="false"), then enable and disable your providers on the first run of your app based on android.os.Build.VERSION using PackageManager and setComponentEnabledSetting(), to give you the right set. Since on Android 3.1+, the user will need to launch one of your activities to be able to add the app widget, anyway, you at least have the entry point in which to apply this logic.

    The OP already got his solution, but for the rest of us, we can leverage bool values in xml and set them differently in different resource directories.

    So your AndroidManifest looks something like this:

    <receiver
      android:label="@string/app_widget_small"
      android:enabled="@bool/is_pre_api_11">
    
      <!-- App Widget Stuff Here -->
    
    </receiver>
    <receiver
      android:label="@string/app_widget_large"
      android:enabled="@bool/is_pre_api_11">
    
      <!-- App Widget Stuff Here -->
    
    </receiver>
    <receiver
      android:label="@string/app_widget_resizeable"
      android:enabled="@bool/is_post_api_11">
    
      <!-- App Widget Stuff Here -->
    
    </receiver>
    

    And in /res/values/attrs.xml:

    <resources>
        <bool name="is_pre_api_11">true</bool>
        <bool name="is_post_api_11">false</bool>
    </resources>
    

    Then in /res/values-v11/attrs.xml:

    <resources>
        <bool name="is_pre_api_11">false</bool>
        <bool name="is_post_api_11">true</bool>
    </resources>
    

    Now your API 10 and below will have two app widgets (small and large) and API 11 and higher will have the one resizeable app widget.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.