This is strange, but setting left or right margin in Java code doesn't work on some devices/platforms.
Here is the code. Simple xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
>
<View
android:id="@+id/myView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:background="#00ff00" />
</FrameLayout>
Java:
@Override
public void onConfigurationChanged (Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams)findViewById(R.id.myView).getLayoutParams();
params.leftMargin = 50;
params.rightMargin = 50;
params.topMargin = 50;
params.bottomMargin = 50;
findViewById(R.id.myView).requestLayout();
}
}
So I've got a green view inside FrameLayout. On rotate I change margin of View. (my activity has android:configChanges="orientation|screenSize")
Here is results:
Samsung Galaxy S 3 (Android 4.1.2) (works fine)
Sony Ericsson Xperia Arc S (Android 4.0.4) (works strange!)
As you can see on the code works properly on Android 4.1.2. And on Android 4.0.4 left and right margins are not set, but top margin is set. Why top?
I've tested on emulators. All platforms >= Android 4 works fine. On Android 2.3 margins are not set at all, even top margin.
Does anyone know how to deal with it? How to change margin in code so it would work on all platforms?