Handling Screen Orientation - Part 2

Posted by muamar Minggu, 12 Juni 2011 0 komentar

How to handle rotation?
Now if you choose to rotate your activity with display either specifying “rotate as sensor” or using “orientation setting” you must handle the steps that are taken when the display is rotated.
Whenever the window rotates, your activity is destroyed and recreated, and “onCreate(Bundle)” is executed again.
You may need to save the activity state before this happens. To achieve that you can override the
public void onSaveInstanceState(Bundle outState)
For Example: You may want to save the Value of a String array before screen orientation change restarts your activity.
To do this override the onSaveInstanceState method of Activity class like this:
public class SampleActivity extends Activity{
      String[] testArray = null;
      public static final String TEST_ARRAY = “KEY_FOR_ARRAY”;

      @Override
      public void onCreate(Bundle State){
            if(State != null){
testArray =
State.getStringArray(SampleActivity.TEST_ARRAY);
            }
            else{
                  testArray = new String[6];
}
      }

      @Override
public void onSaveInstanceState(Bundle outstate){
outstate.putStringArray(SampleActivity.TEST_ARRAY,
      testArray);
}
}

Otherwise your activity will use the default implementation of this function which saves some amount of state.
Some of the values that you can save before orientation change are as follows:
  • Serializable and its subclasses.
  • String
  • Parcelable and its Subclasses
  • boolean, int, float.
  • Arrays etc.

However you may not want to store the values because this may slow down your application you can attain faster screen orientation by caching the data before orientation changes.
Your activity can cache data before rotation this can be accomplished by overriding
public Object onRetainNonConfigurationInstance()
Let’s take the above example again but instead of saving the String array we’ll cache the data and retrieve reference to that after restart.
public class SampleActivity extends Activity{
      String[] testArray = null;
      public static final String TEST_ARRAY = “KEY_FOR_ARRAY”;

      @Override
      public void onCreate(Bundle State){
testArray = (String[])getLastNonConfigurarionInstance();
if(testArray == null)
      testArray = new String[6];
      }

      @Override
public Object onRetainNonConfigurationInstance(){
      return testArray;
}
}
Change orientation at runtime
You can use Activity.setRequestedOrientation() from your activity to set that orientation
e.g. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
if you do not use this the default value is SCREEN_ORIENTATION_UNSPECIFIED and you must not specify any orientation in Applications manifest XML. And each time orientation is changed the activity is stopped and restarted.
Check orientation settings
You can query the orientation settings for device using
public static int myOrient;
myOrient = this.getResources().getConfiguration().orientation;

this function will return either of the following values:
ORIENTATION_LANDSCAPE or ORIENTATION_PORTRAIT, it can also return ORIENTATION_SQUARE on a square device.

<<Handling Screen Orientation - Part 1

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, 
 

Baca Selengkapnya ....

Handling Screen Orientation - Part 1

Posted by muamar 0 komentar
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, 


Baca Selengkapnya ....
Trik SEO Terbaru support Online Shop Baju Wanita - Original design by Bamz | Copyright of android list.