Search code examples
androidandroid-orientation

How to make the UI to look the same in landscape mode?


My LinearLayout changes with the screen orientation. In portrait mode, it looks perfect:

portrait mode

But in landscape mode it looks like this:

landscape mode

How can I fix my UI to look the same on both modes?

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/border"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/imgfourth1"
            android:layout_width="70dp"
            android:layout_height="50dp"
            android:layout_marginRight="25dp"
            android:layout_marginLeft="25dp"
            android:paddingTop="2dp"    
            android:src="@drawable/amlet1" />

        <ImageView
            android:id="@+id/imgfourth2"
            android:layout_width="70dp"
            android:paddingTop="2dp"
            android:layout_height="50dp"  
            android:layout_marginRight="25dp"    
            android:contentDescription="@null"
            android:src="@drawable/lnch1" />

        <ImageView
            android:id="@+id/imgfourth3"
            android:layout_marginRight="25dp"
            android:paddingLeft="20dp"
            android:layout_width="70dp"
            android:layout_height="50dp"
            android:paddingTop="2dp"
            android:contentDescription="@null"   
            android:src="@drawable/supper" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:background="@drawable/border4"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/txtfth1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="2dp"
            android:layout_marginLeft="25dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:gravity="center"
            android:contentDescription="@null"
            android:text="Breakfast" />

        <TextView
            android:id="@+id/txtfth2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="25dp"
            android:paddingLeft="10dp"
            android:gravity="center"
            android:paddingRight="35dp"      
            android:layout_marginTop="2dp"   
            android:contentDescription="@null"
            android:text="Lunch" />

        <TextView
            android:id="@+id/txtfth3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="2dp"
            android:gravity="center"
            android:paddingRight="30dp"
            android:contentDescription="@null"
            android:text="Supper" />
    </LinearLayout>
</LinearLayout>

Solution

  • You can create two different layouts, one for Portrait mode and one for landscape mode. That will solve your problem.

    Keep your
    1. portrait mode layout in res/layout folder and
    2. landscape mode layout in res/layout-land folder.
    

    Android will take the appropriate layout based on your orientation.

    *EDIT : single layout* **

    If you want to use single layout for portrait and landscape orientation then following should help you

    I have used android:weightsum and android:layout_weight to make it look proper.

    <?xml version="1.0" encoding="utf-8"?>
    

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="100">
    
        <ImageView
            android:id="@+id/imgfourth1"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="33"            
            android:src="@drawable/ic_launcher" />
    
        <ImageView
            android:id="@+id/imgfourth2"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="33" 
            android:src="@drawable/ic_launcher" />
    
        <ImageView
            android:id="@+id/imgfourth3"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="33"  
            android:src="@drawable/ic_launcher" />
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="20dp"        
        android:orientation="horizontal"
        android:weightSum="100" >
    
        <TextView
            android:id="@+id/txtfth1"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="33"  
            android:gravity="center_horizontal" 
            android:text="Breakfast" />
    
        <TextView
            android:id="@+id/txtfth2"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="33"   
            android:gravity="center_horizontal" 
            android:text="Lunch" />
    
        <TextView
            android:id="@+id/txtfth3"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="33"  
            android:gravity="center_horizontal"  
            android:text="Supper" />
    </LinearLayout>