Xml layout change when orientation changes

I have two actions that open depending on the availability of an SD . One action has three Buttons and a TextView , and the other a ImageView , button control and zoom. When I change the orientation, the buttons become scrambled in the horizontal direction. How to get around this?

Map layout "SD"

 <?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> 
+6
source share
2 answers

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

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

For more help, you can check out: http://developer.android.com/guide/practices/screens_support.html

+13
source

You can create a folder in your project called "layout-land" (in the "res" directory). There you can copy all your xml styles from the regular "layout" folder and restore them in landscape mode. Android will automatically use these layouts for landscape mode.

Your project will look like this:

enter image description here

+8
source

Source: https://habr.com/ru/post/910324/


All Articles