Sunday, June 12, 2011

Handling Screen Orientation - Part 1

Sometimes or I may say many times user will want to rotate the screen – this may be because user wants to view the app in landscape mode, or user wants to type something using on-screen keyboard.
If user rotates the device from portrait to landscape, your app must rotate with it or must handle the change elegantly, but the question is how??
So let’s begin exploring the answer to this question….
First of all let us understand the concept of rotation and how you can set the behavior in your Android powered device.
On your device’s home screen go to press “MENU” key and go to “SETTINGS” menu and then go to “DISPLAY SETTINGS” You will see the “Orientation/ Auto-rotate screen” option somewhat like this.
 
Note: Location may change based on the Android OS version.

When this option is unchecked, then screen display changes from portrait to landscape if the on – screen keyboard is open and back to portrait when it is closed but nothing happens when user actually rotates the device.
With this setting on screen display always changes when keyboard is open and when user changes the device orientation.
Now let’s see how you can handle this change in your application, there are three things that you must do in your application:
  1. 1.       You should always create layouts that can handle orientation and should always create separate layouts for portrait and landscape modes; we’ll see how you can do this later.
  1. 2.       Then set the orientation behavior in the Application’s manifest file.
  1. 3.       Then handle the rotation by handling appropriate events in your code.
Now let’s start with these points.

Creating layouts that can handle rotation
Now with this I mean that your layout should be able to adjust itself whenever some change like this happens, it’ll be better if your application can provide different layouts for different orientations.
Wherever possible choose from the layouts which adjust to the orientation changes:
LinearLayout, RelativeLayout, or TableLayouts are some of the layouts that that adjust for screen changes, but it might not be a good way because the screen designed for portrait mode might not work for landscape mode and vice versa, so, it’s better to have different layouts for both the modes.
These specialized layouts for Portrait and Landscape modes go in Separate specialized folders “res/layout” for Portrait mode and “res/layout-land” for landscape mode, you can also on similar lines have separate folders for drawables as well.
Setting rotation behavior in Application’s manifest file
This only involves adding one parameter “android:screenOrientation” in <activity> tag in your manifest XML for each activity that must handle rotation.
If you omit this parameter default will be whatever you have set in “DISPLAY SETTINGS” i.e.whatever is “Auto-rotate screen/Orientation” setting. This actually has some benefits listed as below:
1.       If your application uses intents to call some other application views, then, using Orientation settings your app user will not have to turn device to move to that view and turn back to move to your app.
2.       User is free to choose orientation behavior, more preferable by most users.
3.       Makes your app more compatible with wide array of Android devices.
Setting this parameter as “android:screenOrientation=”unspecified”” or “android:screenOrientation=”user” will also have the same effect as not specifying at all
If you set this parameter as “android:screenOrientation=”sensor”” this will mean that display will always rotate when the device is turned or the on-screen keyboard is opened or closed, irrespective of “Orientation” settings.
If you set this parameter as “android:screenOrientation=”portrait”” or “landscape” your activity will remain in one orientation even if the device is rotated or on-screen keyboard is opened or closed. You’ll also not have to worry about layouts that adjust to orientation changes or handle roation in your code.

Handling Screen Orientation - Part 2 >> 
Cheers!!
Vivek Jain
Hello Everyone, I came across an application which allowed me to produce nice effects on Images that are in my phone and Images that I take from my phones camera, its available here, 

7 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.

    blueprism training in chennai | blueprism training in bangalore | blueprism training in pune | blueprism online training

    ReplyDelete
  3. I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. In fact your creative writing abilities has inspired me to start my own BlogEngine blog now. Really the blogging is spreading its wings rapidly. Your write up is a fine example of it.
    Java training in Annanagar | Java training in Chennai

    Java training in Chennai | Java training in Electronic city

    ReplyDelete
  4. Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.
    Data Science training in kalyan nagar | Data Science training in OMR

    Data Science training in chennai | Data science training in velachery

    Data science training in tambaram | Data science training in jaya nagar


    ReplyDelete