6

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>
Martin
  • 2,813
  • 11
  • 43
  • 66

3 Answers3

6

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

Community
  • 1
  • 1
Jigar Pandya
  • 2,129
  • 2
  • 17
  • 27
  • How to turn the relativelayout to straight in layout-land ?? – Martin Jan 20 '14 at 08:35
  • as i said in my answer.make two layout files each for landscape and portrait.And you cant make relative layout straight.you can place views relative to each other.For horizontal or vertical layouts use linear layout. – Jigar Pandya Jan 20 '14 at 08:38
  • I have create the same layout in res/layout-land. But the relativelayout also at the bottom – Martin Jan 20 '14 at 09:50
  • in layout-land you have to place button at right of parent according to figure – Jigar Pandya Jan 20 '14 at 10:40
  • I mean...not only the button , I have set the background to Relativelayout. And I want the Relativelayout change from horizontal to straight. – Martin Jan 20 '14 at 12:53
  • I have post the code of mxl , please teach me how to turn the layout to straight , Thanks – Martin Jan 21 '14 at 01:52
  • Should I need to write the code in JAVA , like if(screen is land){ use layout-land } ??? or it will change auto ? – Martin Jan 21 '14 at 02:26
2

Create different layout for lanscape and put it under same name in layout-land folder

Androider
  • 257
  • 3
  • 11
  • I have post the code of mxl in latout-land , please teach me how to turn the layout to straight , Thanks – Martin Jan 21 '14 at 01:53
1

You should create two versions of xml files and put in layout and layout-land folder inside res folder.

somthing like:

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

Fore more about layout design visit this link: http://developer.android.com/training/basics/supporting-devices/screens.html

Chirag Ghori
  • 4,231
  • 2
  • 20
  • 35