Xml layout changes while changing orientation

10,602

Solution 1

You have to create separate XML files for portrait and landscape modes and place it in different directories. The device will automatically select the right one. You can use the following directory structure

res/layout/my_layout.xml   
res/layout-land/layout.xml

For further reference you can check: http://developer.android.com/guide/practices/screens_support.html

Solution 2

You can make a folder in your project called "layout-land" (in the "res" directory). In there you can copy all your xml styles from the normal "layout" folder and restyle them for landscape mode. Android will automatically use those layouts for landscape mode.

Your project would look like this:

enter image description here

Share:
10,602
jxgn
Author by

jxgn

Updated on June 04, 2022

Comments

  • jxgn
    jxgn about 2 years

    I have two activities which opens depending on the SD card presence. One activity has three Buttons and a TextView and the other an ImageView, a button and zoom control. When I change the orientation, the buttons get scrambled around while in horizontal direction. How to go around this?

    My 'SD' card layout

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#1E1E1E"
        android:orientation="vertical" >
    
        <Button
            android:id="@+id/button_print"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="44dp"
            android:background="@drawable/my_button"
            android:text="@string/print" />
    
        <TextView
            android:id="@+id/text_SDmissing"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="80dp"
            android:text="@string/SDmissing"
            android:textSize="20dp" />
    
        <Button
            android:id="@+id/button_camera"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/button_print"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="58dp"
            android:background="@drawable/my_button"
            android:text="@string/camera" />
    
        <Button
            android:id="@+id/button_insert"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/text_SDmissing"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="40dp"
            android:background="@drawable/my_button"
            android:text="@string/insert" />
    
    </RelativeLayout>
    
  • jxgn
    jxgn over 12 years
    fine :) another question of mine is how to use a layout to support different screen sizes.....what i am doing is that each time i rearrange and run the app and checking in my device but its not the same if i run the same in different app.....
  • Hardik Joshi
    Hardik Joshi about 8 years
    Both file names must be same.