Open In App

Designing the Landscape and Portrait Mode of Application in Android

Whenever the user switch to landscape mode an issue is encountered in which some of the widgets becomes invisible (as you can see the below image) and so in this scenario there is a need to design the separate layout for the landscape mode. At the end of this article you will be able to do that.



Step 1 : Create a new project in android studio :

Step 2 : Can change color combination of the base theme of the application






<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#0f9d58</color>
    <color name="colorPrimaryDark">#006d2d</color>
    <color name="colorAccent">#55cf86</color>
</resources>

Step 3 : Design portrait mode




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    tools:ignore="HardcodedText">
  
    <!--This image is custom made GeeksforGeeks logo-->
    <ImageView
        android:layout_width="200dp"
        android:layout_height="150dp"
        android:layout_gravity="center"
        android:layout_marginTop="25dp"
        android:src="@drawable/gfg_logo_1" />
  
    <!--A simple EditText view-->
    <EditText
        android:id="@+id/edit_text_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="35dp"
        android:layout_marginEnd="16dp"
        android:hint="Name" />
  
    <Button
        android:id="@+id/button_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="30dp"
        android:layout_marginEnd="16dp"
        android:text="Button 1" />
  
    <Button
        android:id="@+id/button_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="25dp"
        android:layout_marginEnd="16dp"
        android:text="Button 2" />
  
    <Button
        android:id="@+id/button_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="25dp"
        android:layout_marginEnd="16dp"
        android:text="Button 3" />
  
</LinearLayout>

Output UI looks like :

Step 4: Design the layout for landscape mode




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity"
    tools:ignore="HardcodedText">
  
    <!--This image is custom made GeeksforGeeks logo-->
    <ImageView
        android:layout_width="250dp"
        android:layout_height="150dp"
        android:layout_gravity="center"
        android:src="@drawable/gfg_logo_1" />
  
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
  
        <!--A simple EditText view-->
        <EditText
            android:id="@+id/edit_text_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="10dp"
            android:layout_marginEnd="16dp"
            android:hint="Name" />
  
        <Button
            android:id="@+id/button_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="30dp"
            android:layout_marginEnd="16dp"
            android:text="Button 1" />
  
        <Button
            android:id="@+id/button_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="25dp"
            android:layout_marginEnd="16dp"
            android:text="Button 2" />
  
        <Button
            android:id="@+id/button_3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="25dp"
            android:layout_marginEnd="16dp"
            android:text="Button 3" />
  
    </LinearLayout>
  
</LinearLayout>

Output UI looks like (to see this output one needs to rotate the device) :

Method 2: Another way to solve this issue is to wrap entire view groups inside the scroll view (meaning the scroll view should be the root view of the layout), by doing this there is no need to create the separate layout design for the landscape mode.




<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="HardcodedText">
  
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
  
        <!--This image is custom made GeeksforGeeks logo-->
        <ImageView
            android:layout_width="200dp"
            android:layout_height="150dp"
            android:layout_gravity="center"
            android:layout_marginTop="25dp"
            android:src="@drawable/gfg_logo_1" />
  
        <!--A simple EditText view-->
        <EditText
            android:id="@+id/edit_text_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="35dp"
            android:layout_marginEnd="16dp"
            android:hint="Name" />
  
        <Button
            android:id="@+id/button_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="30dp"
            android:layout_marginEnd="16dp"
            android:text="Button 1" />
  
        <Button
            android:id="@+id/button_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="25dp"
            android:layout_marginEnd="16dp"
            android:text="Button 2" />
  
        <Button
            android:id="@+id/button_3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="25dp"
            android:layout_marginEnd="16dp"
            android:text="Button 3" />
  
    </LinearLayout>
  
</ScrollView>

By invoking the above code you will achieve the following output :

Things to be taken care while making the separate layout for the landscape mode.


Article Tags :