Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android. Setting left/right margin doesn't work

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)

enter image description here

enter image description here

Sony Ericsson Xperia Arc S (Android 4.0.4) (works strange!)

enter image description here

enter image description here

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?

like image 971
Max Kachinkin Avatar asked Nov 01 '22 15:11

Max Kachinkin


1 Answers

Sony Ericsson Xperia Arc S (Android 4.0.4) Sony LT26ii (Android 4.0.4) both doesn't work I have no choice but replace it with RelativeLayout

like image 152
user1999680 Avatar answered Nov 11 '22 10:11

user1999680