Friday 10 May 2013

Handling Orientation in Android

There is a advantage in mobile device that it can change there orientation. It depends upon user wish that in which mode(Landscape or Portrait) he want to view your application.So it is become necessary for you to set GUI(graphics/layout)of your application in  both mode so that user can feel the charm of both mode.


    Some time It become typical/ hectically   for developer to put both mode(Landscape and Portrait) design in there application. the reason  is one of the following:- 
  • Due Data pull/push(from local database/Server side DB)
  • Rich content of image and graphics
  • Hardware related Application(Camera, Bluetooth ,etc)
Actually when a user change his portable device screen from Landscape to Portrait or vice-versa  Android activity call oncreate() method to show the content of activity every time . So In the above mention scenario it become delay  to show the content of screen .Hence  in this case we  can use one of the following tactics to overcome this problem.
Approach No 1. 
Lock Screen Orientation Using Manifest File

To block Android Activity from rotating on screen orientation change,We can  add android:screenOrientation ("portrait" or "landscape") field into the <activity> element,
just like this Example

   <activity
        android:name="com..sks.recognition.MyActivity"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Black.NoTitleBar" />


This is the simplest way to block the screen orientation change . The main draw back of  this is that it fixed the mode of Screen orientation  That means User have to forcefully open your app in the given mode. So it is not dynamic. it is rigid way to solve the problem
Approach No.2.
You can check the orientation of screen during the app(activity) start and then lock it to change from other screen orientation   As for Example if user open his app in Portrait mode then if he want to change in landscape mode then it not happen at that time. Same way if user open it app in landscape mode then data shown at this time in landscape mode but it not change again in Portrait mode. Use following condition


int prevOrientation = getRequestedOrientation();
    if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    } else if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    } else {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
    }
    setRequestedOrientation(prevOrientation);

This is better approach than Approach no1, because it is dynamic as compare to first approach. User can view your application in both screen mode(Landscape and Portraits ).
Approach No.3.
This is the pure dynamic approach to show the application in both screen mode(Landscape and portrait).

Coming Soon !!!1...




1 comment:

Build a Custom Kernel Module for Android

Hi Guys!!!Hope you are doing well !!!. Today I will describe how you can write a custom kernel module(Hello world) for Android and load it a...