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 page to make the widgets approximately the same size on different Android versions. I've tested the widget in the emulator on API 10, 14 and 15 and it works fine. The widget looks the same in all versions. However, when I test it on my SGS2 phone with Android 4.0.3 there are no margins! How can this be? What am I missing? Did anyone else experience this behaviour?
!
Here's my res/xml/widget_info.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget"
android:minHeight="40dp"
android:minWidth="110dp"
android:updatePeriodMillis="60000" >
</appwidget-provider>
res/layout/widget.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget_mainlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/widget_margin"
android:background="@drawable/widget_background" >
</RelativeLayout>
res/drawable/widget_background.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="2dp"
android:color="#222222" />
<gradient
android:angle="225"
android:startColor="#DD2ECCFA"
android:endColor="#DD000000" />
<corners android:radius="7dp" />
</shape>
res/values/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="widget_margin">8dp</dimen>
</resources>
res/values-v14/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="widget_margin">0dp</dimen>
</resources>
At first I thought it had something to do with the widget not being properly updated as described here, but removing the widget and adding it again doesn't help. I've also experimented a bit with the settings:
- Setting
maxHeight
andmaxWidth
in thewidget_info.xml
file doesn't work, it seems to be ignored by both the emulator and the phone. - Changing
layout_margin
topadding
in thewidget.xml
file has no effect, widget looks the same (but on emulator API10 widget now also has no margins) - Changing the
0dp
to16dp
in theres/values-v14/dimens.xml
file does have an effect in both the phone and the emulator, but the widget's size on the phone and emulator still is different.
EDIT: I've installed GO Launcher Ex on the phone and then the widget does have margins, so I guess this is a specific issue for Samsung's TouchWiz launcher? Can anyone confirm this? I am also interested in solutions to make the widget look (more or less) the same when using TouchWiz.
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:
To detect TouchWiz and other launchers having the same problem, you could use the following code:
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.