Search code examples
androidandroid-layoutmargins

layout setMargins doesn't work in landscape mode


I have one static layout and I set margin in programmatic dynamic. Margin works fine but just in portrait view only. This is xml layout

<LinearLayout
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:gravity="center_vertical" >

    <RelativeLayout
        android:id="@+id/rl_home"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/menubackground"
        android:layout_weight="1.0"
        android:gravity="center" >

        <Button
            android:id="@+id/btn_home"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableTop="@drawable/icon1"
            android:gravity="center"
            android:background="@null"
            android:onClick="ActiveMenu"
            android:text="@string/home"
            android:textColor="@color/White"
            android:textStyle="bold" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rl_browse"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1.0"
        android:background="@drawable/menubackground"
        android:gravity="center" >

        <Button
            android:id="@+id/btn_browse"
            android:background="@null"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableTop="@drawable/icon2"
            android:gravity="center"
            android:onClick="ActiveMenu"
            android:text="@string/browse"
            android:textColor="@color/White"
            android:textStyle="bold" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rl_account"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1.0"
        android:background="@drawable/menubackground"
        android:gravity="center" >

        <Button
            android:background="@null"
            android:id="@+id/btn_account"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableTop="@drawable/icon3"
            android:gravity="center"
            android:onClick="ActiveMenu"
            android:text="@string/account"
            android:textColor="@color/White"
            android:textStyle="bold" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rl_mybag"
        android:background="@drawable/menubackground"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1.0"
        android:gravity="center" >

        <Button
            android:background="@null"
            android:id="@+id/btn_mybag"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableTop="@drawable/icon4"
            android:gravity="center"
            android:onClick="ActiveMenu"
            android:text="@string/my_bag"
            android:textColor="@color/White"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tv_no_Of_items"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignRight="@+id/btn_mybag"
            android:layout_marginRight="0.0dip"
            android:background="@drawable/red_circle"
            android:gravity="center"
            android:text="1"
            android:textColor="@color/White"
            android:textSize="15.0dip"
            android:textStyle="bold"
            android:visibility="invisible" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rl_more"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1.0"
        android:background="@drawable/menubackground"
        android:gravity="center" >

        <Button
            android:background="@null"
            android:id="@+id/btn_more"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableTop="@drawable/icon5"
            android:gravity="center"
            android:onClick="ActiveMenu"
            android:text="@string/more"
            android:textColor="@color/White"
            android:textStyle="bold" />
    </RelativeLayout>
</LinearLayout>

I wrote following code for margin



    Button btn_home,    btn_browse, btn_account,    btn_mybag,  btn_more;
    RelativeLayout  rl_home,    rl_browse,  rl_account, rl_mybag,   rl_more;
    LinearLayout LinearLayout1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.browse);


        btn_home=(Button) findViewById(R.id.btn_home);
        btn_browse=(Button) findViewById(R.id.btn_browse);
        btn_account=(Button) findViewById(R.id.btn_account);
        btn_mybag=(Button) findViewById(R.id.btn_mybag);
        btn_more=(Button) findViewById(R.id.btn_more);

        rl_home=(RelativeLayout) findViewById(R.id.rl_home);
        rl_browse=(RelativeLayout) findViewById(R.id.rl_browse);
        rl_account=(RelativeLayout) findViewById(R.id.rl_account);
        rl_mybag=(RelativeLayout) findViewById(R.id.rl_mybag);
        rl_more=(RelativeLayout) findViewById(R.id.rl_more);
        LinearLayout1=(LinearLayout) findViewById(R.id.LinearLayout1);
    }

    public void ActiveMenu(View v) {


        if (R.id.btn_home == v.getId()) {
            rl_home.setBackgroundResource(R.drawable.current_menu);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
            params.setMargins(0, 0, 0, 0);
            rl_home.setLayoutParams(params);
            LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
            params1.setMargins(0, 15, 0, 0);

            rl_browse.setBackgroundResource(R.drawable.menubackground);
            rl_browse.setLayoutParams(params1);
            rl_account.setBackgroundResource(R.drawable.menubackground);
            rl_account.setLayoutParams(params1);
            rl_mybag.setBackgroundResource(R.drawable.menubackground);
            rl_mybag.setLayoutParams(params1);
            rl_more.setBackgroundResource(R.drawable.menubackground);
            rl_more.setLayoutParams(params1);

        }

        if (R.id.btn_browse== v.getId()) {
            rl_browse.setBackgroundResource(R.drawable.current_menu);

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
            params.setMargins(0, 0, 0, 0);
            rl_browse.setLayoutParams(params);
            LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
            params1.setMargins(0, 15, 0, 0);

            rl_home.setBackgroundResource(R.drawable.menubackground);
            rl_home.setLayoutParams(params1);
            rl_account.setBackgroundResource(R.drawable.menubackground);
            rl_account.setLayoutParams(params1);
            rl_mybag.setBackgroundResource(R.drawable.menubackground);
            rl_mybag.setLayoutParams(params1);
            rl_more.setBackgroundResource(R.drawable.menubackground);
            rl_more.setLayoutParams(params1);

        }

        if (R.id.btn_account == v.getId()) {

            rl_account.setBackgroundResource(R.drawable.current_menu);

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
            params.setMargins(0, 0, 0, 0);
            rl_account.setLayoutParams(params);
            LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
            params1.setMargins(0, 15, 0, 0);

            rl_browse.setBackgroundResource(R.drawable.menubackground);
            rl_browse.setLayoutParams(params1);
            rl_home.setBackgroundResource(R.drawable.menubackground);
            rl_home.setLayoutParams(params1);
            rl_mybag.setBackgroundResource(R.drawable.menubackground);
            rl_mybag.setLayoutParams(params1);
            rl_more.setBackgroundResource(R.drawable.menubackground);
            rl_more.setLayoutParams(params1);
        }

        if (R.id.btn_mybag == v.getId()) {
            rl_mybag.setBackgroundResource(R.drawable.current_menu);

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
            params.setMargins(0, 0, 0, 0);
            rl_mybag.setLayoutParams(params);
            LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
            params1.setMargins(0, 15, 0, 0);

            rl_browse.setBackgroundResource(R.drawable.menubackground);
            rl_browse.setLayoutParams(params1);
            rl_account.setBackgroundResource(R.drawable.menubackground);
            rl_account.setLayoutParams(params1);
            rl_home.setBackgroundResource(R.drawable.menubackground);
            rl_home.setLayoutParams(params1);
            rl_more.setBackgroundResource(R.drawable.menubackground);
            rl_more.setLayoutParams(params1);
        }

        if (R.id.btn_more== v.getId()) {
            rl_more.setBackgroundResource(R.drawable.current_menu);

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
            params.setMargins(0, 0, 0, 0);
            rl_more.setLayoutParams(params);
            LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
            params1.setMargins(0, 15, 0, 0);


            rl_browse.setBackgroundResource(R.drawable.menubackground);
            rl_browse.setLayoutParams(params1);
            rl_account.setBackgroundResource(R.drawable.menubackground);
            rl_account.setLayoutParams(params1);
            rl_home.setBackgroundResource(R.drawable.menubackground);
            rl_home.setLayoutParams(params1);
            rl_mybag.setBackgroundResource(R.drawable.menubackground);
            rl_mybag.setLayoutParams(params1);
        }

    }

Is any mistake I made?? Portrate view enter image description here

landscape view enter image description here

after changing current item in landscape view enter image description here


Solution

  • Finally I got the solution. In my old posted code I wrote this code

    LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
    params1.setMargins(0, 15, 0, 0);
    

    And I have change below code.. this solved my problem.

    final LinearLayout.LayoutParams params1 = (LinearLayout.LayoutParams) rl_home
                    .getLayoutParams();
    params1.setMargins(0, 15, 0, 0);
    rl_home.setLayoutParams(params1);