is it possible to show widget in "Add to home screen > Widgets" Dialog only for a certain android version without uploading two APKs and assigning to proper SDK version numbers?
Asked
Active
Viewed 2,060 times
25
-
2it would be interesting, not only dependent on version but more things. – berlindev Aug 15 '11 at 10:46
-
of course it would :) i just wrote android version because i am having problems with 2.1 because of the API restrictions. – Cloudgiant Aug 15 '11 at 10:49
2 Answers
36
I have a way to do it without any code e.g.:
In your res\values dir create a bools.xml file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="honeycombOrAbove">false</bool>
</resources>
Create a values-v11 dir (for honeycomb or above values), copy the file to it and set the value to true.
Then in the manifest for the widget receiver use: android:enabled="@bool/honeycombOrAbove". I also used it in the configuration activity for the widget.
This means the widget is disabled at install time.

Mark
- 3,334
- 1
- 22
- 27
-
1Brilliant solution since it works when the application is installed and this is more of a provisioning issue. Thanks for this - you saved me a bunch of hunting for a solution this morning. – Jerry Brady Sep 26 '11 at 14:51
-
1Perfect solution. This really should have been the accepted answer! – Artiom Chilaru Oct 10 '11 at 23:45
-
1
9
You can do it with the componentEnableSetting. Just disable the widget you don't want to have listed. The change will get active after phone-restart.
Context context = getApplicationContext();
String str1 = "org.classname.to.widget.provider";
ComponentName componentName = new ComponentName(context, str1);
PackageManager packageManager = getPackageManager();
int versioncode = Integer.valueOf(android.os.Build.VERSION.SDK);
//enable widget
packageManager.setComponentEnabledSetting(componentName, 1, 1);
//disable widget
packageManager.setComponentEnabledSetting(componentName, 2, 1);

berlindev
- 1,753
- 14
- 22
-
Yep this solution works although i had to change it a little bit since this code looks for version number of the app and not the SDK. Thanks a lot! – Cloudgiant Aug 15 '11 at 12:28
-
what did you change? i can edit and then you can approve the answer as the solution for your problem – berlindev Aug 15 '11 at 12:30
-
just this line "int versioncode = Integer.valueOf(android.os.Build.VERSION.SDK);" and added if conditions for 2.1 to disable and 2.2 or above to enable – Cloudgiant Aug 15 '11 at 12:33
-
1Approved. What i like about this answer is that you can use it to disable widget for almost every possible situation. Thanks – Cloudgiant Aug 15 '11 at 12:38
-
You should use `int VERSION.SDK_INT` instead of the deprecated and parsing requiring `String VERSION.SDK`. – Rayne Aug 23 '12 at 20:26