This is strange, but setting the left or right margin in Java code does not 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 have a green view inside FrameLayout. When turning, I change the field of view. (my activity has android: configChanges = "orientation | screenSize")
Here are the results:
Samsung Galaxy S 3 (Android 4.1.2) (works great)


Sony Ericsson Xperia Arc S (Android 4.0.4) (it works weird!)


As you can see, the code works correctly on Android 4.1.2. And on Android 4.0.4, the left and right fields are not set, but the top field is set. Why top?
I tested emulators. All platforms> = Android 4 work fine. On Android 2.3 brands are not installed at all, even the top margin.
Does anyone know how to deal with this? How to change the margin in the code so that it works on all platforms?
source share