Home > Articles > Mobile Application Development & Programming

  • Print
  • + Share This
From the Rough Cut Adapting to Screen Orientation

Adapting to Screen Orientation

As with almost all smartphones, Android supports two screen orientations: portrait and landscape. When the screen orientation of an Android device is changed, the current activity being displayed is destroyed and re-created automatically to redraw its content in the new orientation. In other words, the onCreate() method of the activity is fired whenever there is a change in screen orientation.

Portrait mode is longer in height and smaller in width, whereas landscape mode is wider but smaller in height. Being wider, landscape mode has more empty space on the right side of the screen. At the same time, some of the controls don’t appear because of the smaller height. Thus, controls needs to be laid out differently in the two screen orientations because of the difference in the height and width of the two orientations.

There are two ways to handle changes in screen orientation:

  • Anchoring controls—Set the controls to appear at the places relative to the four edges of the screen. When the screen orientation changes, the controls do not disappear but are rearranged relative to the four edges.
  • Defining layout for each mode—A new layout file is defined for each of the two screen orientations. One has the controls arranged to suit the Portrait mode, and the other has the controls arranged to suit the Landscape mode.

Anchoring Controls

For anchoring controls relative to the four edges of the screen, we use a RelativeLayout container. Let’s examine this method by creating an Android project called ScreenOrientationApp. To lay out the controls at locations relative to the four edges of the screen, write the code in the layout file activity_screen_orientation_app.xml as shown in Listing 3.15.

Listing 3.15 The Layout file activity_screen_orientation_app.xml on Laying Out Controls Relative to the Four Edges of the Screen

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button  
        android:id="@+id/Apple"  
        android:text="Apple"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="15dip"  
        android:layout_marginLeft="20dip" />
    <Button  
        android:id="@+id/Mango"  
        android:text="Mango"
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:padding="28dip"
        android:layout_toRightOf="@id/Apple" 
        android:layout_marginLeft="15dip"
        android:layout_marginRight="10dip"
        android:layout_alignParentTop="true" />
    <Button  
        android:id="@+id/Banana"  
        android:text="Banana"
        android:layout_width="200dip" 
        android:layout_height="50dip"    
        android:layout_marginTop="15dip"  
        android:layout_below="@id/Apple" 
        android:layout_alignParentLeft="true" />
    <Button  
        android:id="@+id/Grapes"  
        android:text="Grapes"
        android:layout_width="wrap_content" 
        android:layout_height="match_parent"    
        android:minWidth="100dp"
        android:layout_alignParentRight="true"     
        android:layout_below="@id/Banana" />
    <Button  
        android:id="@+id/Kiwi"  
        android:text="Kiwi"
        android:layout_width="100dip" 
        android:layout_height="wrap_content"    
        android:layout_below="@id/Banana"
        android:paddingTop="15dip"     
        android:paddingLeft="25dip" 
        android:paddingRight="25dip" />
</RelativeLayout>

Listing 3.15 shows five Button controls arranged in a RelativeLayout container. The controls are aligned relative to the edges of the container or in relation to each other. Let’s keep the activity file ScreenOrientationAppActivity.java unchanged with the default code, as shown in Listing 3.16.

Listing 3.16 Default Code in the Java Activity File ScreenOrientationAppActivity.java

package com.androidunleashed.screenorientationapp;
import android.app.Activity;
import android.os.Bundle;

public class ScreenOrientationAppActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen_orientation_app);
    }
}

When the application is run while in the default Portrait mode, the controls appear as shown in Figure 3.16 (left). Because the five Button controls are placed in relation to the four edges of the container and in relation to each other, none of the Button controls disappear if the screen is rotated to Landscape mode, as shown in Figure 3.16 (right). To switch between portrait mode and landscape mode on the device emulator, press the Ctrl+F11 keys.

Figure 3.16 (left) Controls in Portrait mode, and (right) the controls in Landscape mode

Now that we understand the concept of adapting to screen orientation through anchoring controls, let’s have a look at another approach.

Defining Layout for Each Mode

In this method, we define two layouts. One arranges the controls in the default portrait mode, and the other arranges the controls in landscape mode. To understand this, let’s write code as shown in Listing 3.17 for laying out the controls for portrait mode in the default layout file activity_screen_orientation_app.xml (found in the res/layout folder).

Listing 3.17 The Layout File activity_screen_orientation_app.xml on Laying Out Controls in Portrait Mode

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button  
        android:id="@+id/Apple"  
        android:text="Apple"
        android:layout_width="300dp" 
        android:layout_height="wrap_content" 
        android:padding="20dip"
        android:layout_marginTop="20dip" />
    <Button  
        android:id="@+id/Mango"  
        android:text="Mango"
        android:layout_width="300dp" 
        android:layout_height="wrap_content" 
        android:padding="20dip"
        android:layout_marginTop="20dip" />
    <Button  
        android:id="@+id/Banana"  
        android:text="Banana"
        android:layout_width="300dip" 
        android:layout_height="wrap_content"   
        android:padding="20dip" 
        android:layout_marginTop="20dip"  />
    <Button  
        android:id="@+id/Grapes"  
        android:text="Grapes"
        android:layout_width="300dip" 
        android:layout_height="wrap_content"    
        android:padding="20dip"
        android:layout_marginTop="20dip"   />
    <Button  
        android:id="@+id/Kiwi"  
        android:text="Kiwi"
        android:layout_width="300dip" 
        android:layout_height="wrap_content"    
        android:padding="20dip"
        android:layout_marginTop="20dip"  />
</LinearLayout>

In Listing 3.17, we can see that five Button controls are vertically arranged in a LinearLayout container, one below the other. This vertical arrangement makes a few of the Button controls disappear when the screen is in Landscape mode.

If we run the application without defining the layout for the Landscape mode, we find the controls arranged in Portrait mode, as shown in Figure 3.17 (left). But when we switch the screen orientation to Landscape, we find the last two Button controls have disappeared, as shown in Figure 3.17 (right).This is because in Landscape mode, the screen becomes wider but shorter in height.

Figure 3.17 (left) Controls in Portrait mode, and (right) some controls disappear in Landscape mode.

To use the blank space on the right side of the screen in Landscape mode, we need to define another layout file, activity_screen_orientation_app.xml, created in the res/layout-land folder. The layout-land folder has to be created manually inside the res folder. Right-click on the res folder in the Package Explorer window and select the New, Folder option. A dialog box opens, asking for the name for the new folder. Assign the name layout-land to the new folder, and click the Finish button. Copy the activity_screen_orientation_app.xml file from the res/layout folder and paste it into res/layout-land folder. Modify the activity_screen_orientation_app.xml file in the res/layout-land folder so as to arrange the controls in Landscape mode. The code in the newly created activity_screen_orientation_app.xml is modified as shown in Listing 3.18.

Listing 3.18 The Layout File activity_screen_orientation_app.xml in the res/layout-land Folder

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button  
        android:id="@+id/Apple"  
        android:text="Apple"
        android:layout_width="250dp" 
        android:layout_height="wrap_content" 
        android:padding="20dip"
        android:layout_marginTop="20dip" />
    <Button  
        android:id="@+id/Mango"  
        android:text="Mango"
        android:layout_width="250dp" 
        android:layout_height="wrap_content" 
        android:padding="20dip"
        android:layout_marginTop="20dip" 
        android:layout_toRightOf="@id/Apple" />
    <Button  
        android:id="@+id/Banana"  
        android:text="Banana"
        android:layout_width="250dip" 
        android:layout_height="wrap_content"   
        android:padding="20dip" 
        android:layout_marginTop="20dip" 
        android:layout_below="@id/Apple" />
    <Button  
        android:id="@+id/Grapes"  
        android:text="Grapes"
        android:layout_width="250dip" 
        android:layout_height="wrap_content"    
        android:padding="20dip"
        android:layout_marginTop="20dip"  
        android:layout_below="@id/Apple"
        android:layout_toRightOf="@id/Banana"  />
    <Button  
        android:id="@+id/Kiwi"  
        android:text="Kiwi"
        android:layout_width="250dip" 
        android:layout_height="wrap_content"    
        android:padding="20dip"
        android:layout_marginTop="20dip" 
        android:layout_below="@id/Banana" />
</RelativeLayout>

In this code block, we can see that, to fill up the blank space on the right side of the screen, the Mango and Grapes button controls are set to appear to the right of the Apple and Banana button controls.

We can also detect the screen orientation via Java code. Let’s modify the activity file ScreenOrientationAppActivity.java to display a toast message when the screen switches between landscape mode and portrait mode. The code written in the Java activity file ScreenOrientationappActivity.java is shown in Listing 3.19.

Listing 3.19 Code Written in the Java Activity File ScreenOrientationappActivity.java

package com.androidunleashed.screenorientationapp;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class ScreenOrientationAppActivity extends Activity {
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen_orientation_app);
   if(getResources().getDisplayMetrics().widthPixels>getResources().getDisplayMetrics().
            heightPixels) 
        {  
            Toast.makeText(this,"Screen switched to Landscape mode",Toast.LENGTH_SHORT).show(); 
        } 
        else 
        { 
            Toast.makeText(this,"Screen switched to Portrait mode",Toast.LENGTH_SHORT).show(); 
        }
    }
}

Now, when we run the application, the controls appear in portrait mode as shown in Figure 3.18 (left) and in landscape mode as shown in Figure 3.18 (right). We can see that none of the Button controls are now hidden in landscape mode.

Figure 3.18 (left) Controls in Portrait mode, and (right) all controls are visible in Landscape mode.

  • + Share This
  • 🔖 Save To Your Account