How To Handle Screen Orientation

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"?-->

android:layout_gravity="center">

<button></button>
<button></button>
<button></button>
<button></button>

 

 

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"?-->
android:layout_height="fill_parent" android:layout_width="fill_parent"
android:padding="15dip" android:orientation="horizontal">
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:layout_gravity="center" android:paddingLeft="20dip"
android:paddingRight="20dip">

<button></button>
<button></button>

<button></button>
<button></button>

 

 

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:

Leave a Comment:

5 comments
GQGK says June 9, 2011

Is there a way to do this programmatically/dynamically?

Reply
    Sushant says June 10, 2011

    There are two ways to do this programmatically.

    1.) Override onRetainNonConfigurationInstance() special method provided by your activity. This method can be used to pass an arbitrary “object your future self” (the object to be used when orientation changed.) and Android is smart enough to call this method only when needed. In the new Object activity, in onCreate(), all you have to do to get your object back(in previous orientation) is to call getLastNonConfigurationInstance().

    2.) Override onConfigurationChanged() method of your activity. This method is passed a “Configuration” object that specifies the new device configuration. By reading fields in the “Configuration”, you can determine the new configuration and make appropriate changes by updating the resources used in your interface. At the time this method is called, your Activity’s Resources object is updated to return resources based on the new configuration, so you can easily reset elements of your UI. For example

    [code lang="java"]
    @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", Toast.LENGTH_SHORT).show();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
    Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    }
    }
    [/code]

    Reply
Sushant says June 14, 2011

Please check below code:

[code lang="java"]

package com.AppInfo;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
//import android.widget.ListView;
import android.widget.TextView;

public class AppInfo extends Activity {
/** Called when the activity is first created. */
GridView grid_main;
static ArrayList<PInfo> app;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridview);

app = getPackages();

grid_main = (GridView)findViewById(R.id.GridView01);
grid_main.setAdapter(new ImageAdapter(this));

PInfo p = new PInfo();
p.prettyPrint();
String s = p.appname;
System.out.println("s is ::::: "+ s);

}
class PInfo {
public String appname = "";
private String pname = "";
private String versionName = "";
private int versionCode = 0;
private Drawable icon;
private void prettyPrint() {
System.out.println(appname + "t" + pname + "t" + versionName + "t" + versionCode + icon);
try
{
ImageView iv;
iv = (ImageView)findViewById(R.id.imageView1);
Bitmap bitmap = ((BitmapDrawable)icon).getBitmap();
iv.setImageBitmap(bitmap);
} catch(Exception e) {
}
}
}

public ArrayList<PInfo> getPackages() {
ArrayList<PInfo> apps = getInstalledApps(false);
final int max = apps.size();
System.out.println("maxis "+max);
for (int i=0; i<1; i++) {
apps.get(i).prettyPrint();
}
return apps;
}

public ArrayList<PInfo> getInstalledApps(boolean getSysPackages) {
ArrayList<PInfo> res = new ArrayList<PInfo>();
List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);

for(int i=0;i<packs.size();i++) {
PackageInfo p = packs.get(i);

if ((!getSysPackages) && (p.versionName == null)) {
continue ;
}
Date d = new Date(11, 06,2011);
System.out.println("Date is "+d);

PInfo newInfo = new PInfo();
newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString();
newInfo.pname = p.packageName;
newInfo.versionName = p.versionName;
newInfo.versionCode = p.versionCode;
newInfo.icon = p.applicationInfo.loadIcon(getPackageManager());

res.add(newInfo);
}
return res;
}

public class ImageAdapter extends BaseAdapter{
Context mContext;
public static final int ACTIVITY_CREATE = 10;
public ImageAdapter(Context c){
mContext = c;
}
@Override
public int getCount() {
return app.size();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if(convertView==null){
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.icon, null);
TextView tv = (TextView)v.findViewById(R.id.icon_text);
tv.setText(" "+ app.get(position).appname);
ImageView iv = (ImageView)v.findViewById(R.id.icon_image);
iv.setImageResource(R.drawable.icon);

}
else
{
v = convertView;
}
return v;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
}

}

[/code]

I hope this is what you are looking for.

Reply
nate says May 1, 2012

This code works great, however when I change the layout via code and then I flip the screen, it goes back to the main layout again and not that of my newly set layout. Is this because it re-fires onCreate when the screen is flipped? What is happening here and how do I fix this? I am new to Android and I may be using the layout wrong. I am using them as new a new form or page if you will. Thanks in advance.

Reply
Add Your Reply