I have an app with a widget. I know that Android 4.0 or later will automatically create a margin around the widget so I\'ve implemented the suggestion on this developers API
As you already pointed out, the trick is to use different margins/padding for TouchWiz devices or SDK level < 14. For SDK level < 14 you should continue to use dimens.xml, but for TouchWiz you'll need to create a separate layout file. Then if you detect TouchWiz, you switch the layout file in the constructor of the widget RemoteViews:
RemoteViews views = new RemoteViews(
context.getPackageName(),
isAlternativeMargins(context) ?
R.layout.alternative_layout :
R.layout.normal_layout
)
To detect TouchWiz and other launchers having the same problem, you could use the following code:
/* sorted alphabetically to be able to use Arrays.binarySearch later on */
private final static String[] LAUNCHER_ALTERNATIVES = {
"com.sec.android.app.launcher",
"com.sec.android.app.twlauncher",
};
private final static String[] LAUNCHER_ALTERNATIVE_EXCEPTIONS = {
"GT-I9100", // Galaxy S II
"GT-I9100G", // Galaxy S II
"GT-I9100T", // Galaxy S II
"GT-I9210T", // Galaxy S II
"GT-I9100M", // Galaxy S II
"SGH-I757M", // Galaxy S II
"SGH-I727R", // Galaxy S II
"SGH-I927", // Galaxy S II
"SGH-T989D", // Galaxy S II
"GT-I9108", // Galaxy S II
"GT-I9100P", // Galaxy S II
"ISW11SC", // Galaxy S II
"SC-02C", // Galaxy S II
"SHW-M250K", // Galaxy S II
"SHW-M250L", // Galaxy S II
"SHW-M250S", // Galaxy S II
"SGH-I777", // Galaxy S II
"SGH-I727", // Galaxy S II
"SGH-I927", // Galaxy S II
"SPH-D710", // Galaxy S II
"SGH-T989", // Galaxy S II
"SCH-R760", // Galaxy S II
"GT-N7000", // Galaxy Note
"SHV-E160K", // Galaxy Note
"SHV-E160L", // Galaxy Note
"SHV-E160S", // Galaxy Note
"SGH-I717", // Galaxy Note
"SC-05D", // Galaxy Note
"SGH-T879", // Galaxy Note
"GT-I9220", // Galaxy Note
"GT-I9228", // Galaxy Note
"SCH-I889", // Galaxy Note
"SGH-I717", // Galaxy Note
};
static {
Arrays.sort(LAUNCHER_ALTERNATIVE_EXCEPTIONS);
}
public static boolean isAlternativeMargins(Context context) {
if (Build.VERSION.SDK_INT < 14)
return false;
PackageManager pm = context.getPackageManager();
Intent i = new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
i.addCategory(Intent.CATEGORY_DEFAULT);
ResolveInfo info = pm.resolveActivity(i, PackageManager.MATCH_DEFAULT_ONLY);
if (
info != null && info.activityInfo != null &&
info.activityInfo.taskAffinity != null
) {
int pos = Arrays.binarySearch(
LAUNCHER_ALTERNATIVES, info.activityInfo.taskAffinity
);
// if TouchWiz
if (pos >= 0) {
// if >= Android 4.1.2 and (Galaxy S II or Galaxy Note)
if ( getSdk() >= 16 && Arrays.binarySearch(
LAUNCHER_ALTERNATIVE_EXCEPTIONS, Build.MODEL
) >= 0)
return false;
return true;
}
}
return false;
}
The code above detects the default launcher and returns true, if it is TouchWiz.
EDIT: Modified for additional launchers that seem to have the same problem as TouchWiz.
EDIT 2: Modified for Android 4.1.2 on Galaxy S II and Galaxy Note where the widget margins work as expected. In contrast on a Galaxy S 3 with Android 4.1.2, they don't work as expected which will probably change for future TouchWiz versions from Samsung.