Tuesday, August 19, 2014

Screen Rotation in Android

In this tutorial, we shall see how to adjust the app screen when a user rotates his device. We shall see a simple tutorial to display "Hello World" on the screen and adjust the display depending on user's screen rotation. We shall follow these steps:
i. Put the text in a XML layout
ii. Update the Manifest file to respond to screen rotation
iii. Check the screen orientation and display toast message using Java code to show screen orientation

1. XML layout files

Open the main.xml file and write the following code. 
File: res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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" >
    <TextView android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerHorizontal="true"

        android:layout_centerVertical="true"

        android:text="Hello World" />
</RelativeLayout>

2. Manifest.xml

Open the AndroidManifest.xml file and write the following code.
File: AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.endeavour.tutorial"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.endeavour.tutorial.Main"
            android:label="@string/app_name"
            android:configChanges="orientation|screenSize|keyboardHidden">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
</application>
</manifest>

3. Java Code

We shall create a toast message in Java to display the screen orientation mode.
File: Main.java

package com.endeavour.tutorial;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.widget.Toast;

public class Main extends Activity {
@Override
    protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    //Check screen orientation or screen rotate event here
  @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        //Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Toast.makeText(this, "Landscape Mode", Toast.LENGTH_SHORT).show();
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            Toast.makeText(this, "Portrait Mode", Toast.LENGTH_SHORT).show();
}
    }
}

4. Output

The following outputs shall be displayed for the two screen orientations.

i. Portrait Mode


ii. Landscape Mode


No comments:

Post a Comment