Monday, June 18, 2012

How To Handle Screen Orientation In Android



This is a sample activity which shows How to Handle Screen Orientation in android.
Underlying Algorithm:
Basic description of algorithm in step by step form:
1.) Create a Project ScreenOrientation.
2.) Put the following code snippet in res/layout/main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"android:layout_height="fill_parent" android:layout_width="fill_parent"android:padding="30dip" android:orientation="horizontal">
    <LinearLayout android:orientation="vertical"android:layout_height="wrap_content" android:layout_width="fill_parent"
android:layout_gravity="center">
        <TextView android:text="My App" android:layout_height="wrap_content"android:layout_width="wrap_content" android:layout_gravity="center"android:layout_marginBottom="25dip" android:textSize="24.5sp" />
        <Button android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="one" />
        <Button android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="two" />
        <Button android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="three" />
        <Button android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="four" />
    </LinearLayout>
</LinearLayout>
3.) Now create a different layout for landscape mode. The layouts that should be used for landscape mode should be placed in a directory called layout-land :
4.) Put the following code snippet in res/layout-land/main.xml (for landscape mode):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent" android:layout_width="fill_parent"
android:padding="15dip" android:orientation="horizontal">
<LinearLayout android:orientation="vertical"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:layout_gravity="center" android:paddingLeft="20dip"
android:paddingRight="20dip">
<TextView android:text="My App" android:layout_height="wrap_content"android:layout_width="wrap_content" android:layout_gravity="center"android:layout_marginBottom="20dip" android:textSize="24.5sp" />
<TableLayout android:layout_height="wrap_content"android:layout_width="wrap_content" android:layout_gravity="center"android:stretchColumns="*">
        <TableRow>
                <Button android:text="one" />
                <Button android:text="two" />
        </TableRow>
        <TableRow>
                <Button android:text="three" />
                <Button android:text="four" />
        </TableRow>
</TableLayout>
</LinearLayout>
</LinearLayout>
5.) Run the application.
Steps to Create:
1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. ScreenOrientation. Enter following information:
Project name: ScreenOrientation
Build Target: Android 2.1
Application name: ScreenOrientation
Package name: com.app.ScreenOrientation
Create Activity: ScreenOrientation
On Clicking Finish ScreenOrientation code structure is generated with the necessary Android Packages being imported along with ScreenOrientation.java. ScreenOrientation class will look like following:
package com.app.ScreenOrientation;
import android.app.Activity;
import android.os.Bundle;
public class ScreenOrientation extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

Output
 –The final output:
Portrait Mode:
On changing orientation:

No comments:

Post a Comment