How to change the position of layout when the orientation of screen change in android?

Martin picture Martin · Jan 20, 2014 · Viewed 7.5k times · Source

How to change the position of layout when the orientation of screen change in android ?

I have a button in the Relativelayout.

When the phone is LANDSCAPE, the Relativelayout is at the bottom.

And it turn to the right of screen , when phone is PORTRAIT.

It like the following picture.

I get the orientation of screen , but how to setting the Relativelayout change it position from bottom to right ?

enter image description here

----------------------------------EDIT----------------------------

The code of xml in layout-land.

There are two button in top_buttonlayout1 ,the another two button is in bottom_buttonlayout1.

The top_buttonlayout1 is the Relative-2 in the picture , and the bottom_buttonlayout1 is the bottom relativelayout.

And I want it change to straight , how to do ?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:baselineAligned="false"
    android:orientation="vertical" >


    <LinearLayout
        android:id="@+id/top_buttonlayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/top_buttonlayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:background="#00000000" >

           <ImageButton
                android:id="@+id/SettingButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scaleType="fitXY"
                android:layout_marginTop="5dp"
                android:background="#00000000"
                android:src="@drawable/parmeter_setting" />

            <ImageButton
                android:id="@+id/FileSavebutton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scaleType="fitXY"
                android:layout_marginTop="5dp"
                android:layout_marginRight="5dp"  
                android:background="#00000000"        
                android:src="@drawable/save_in_camera" />

        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/bottom_buttonlayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="vertical"
        android:background="#454749"
         >

       <RelativeLayout
            android:id="@+id/bottom_buttonlayout"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">


            <ImageButton
                android:id="@+id/FileButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:scaleType="fitXY"
                android:layout_centerVertical="true"
                android:background="#454749"
                android:src="@drawable/file_viewer"/>

             <ImageButton
                android:id="@+id/photo_record_mode"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="20dp"
                android:layout_alignParentRight="true"  
                android:layout_centerVertical="true"            
                android:scaleType="fitXY"
                android:background="#454749"
                android:src="@drawable/recordmode"/>

        </RelativeLayout>
    </LinearLayout>

</RelativeLayout>

Answer

Jigar Pandya picture Jigar Pandya · Jan 20, 2014

You will have to create two versions of xml files and put in layout-port and layout-land folder inside res folder.

eg :

res/layout-land [Landscape Mode]
main.xml 
res/layout-port [Portrait Mode ]
main.xml

You can refer further more on the same at http://developer.android.com/training/basics/supporting-devices/screens.html

source : here