mytechead

Android & Wordpress Help center

Android: Set device orientation

Android has introduced two more orientation modes for devices with API 8+: ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT & ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE. You can set the screen orientation to these modes by using the following code in you Activity class:

try {
    if(Integer.valueOf(android.os.Build.VERSION.SDK_INT) >= 8){
        if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
            // setting the orientation as per the device orientation for API level 8+
            if(getWindowManager().getDefaultDisplay().getRotation() == Surface.ROTATION_0){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
       }else if(getWindowManager().getDefaultDisplay().getRotation() == Surface.ROTATION_180){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
       }else if(getWindowManager().getDefaultDisplay().getRotation() == Surface.ROTATION_90){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
       }else if(getWindowManager().getDefaultDisplay().getRotation() == Surface.ROTATION_270){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
       }
     }else if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
          if(getWindowManager().getDefaultDisplay().getRotation() == Surface.ROTATION_0){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
          }else if(getWindowManager().getDefaultDisplay().getRotation() == Surface.ROTATION_180){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
          }else if(getWindowManager().getDefaultDisplay().getRotation() == Surface.ROTATION_90){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
          }else if(getWindowManager().getDefaultDisplay().getRotation() == Surface.ROTATION_270){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
          }
    }
}else if(Integer.valueOf(android.os.Build.VERSION.SDK_INT) < 8){
      // setting the orientation as per the device orientation for API level below 8
      if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
      }else {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
      }

}
} catch (Exception e1) {
e1.printStackTrace();
}

Please note that the above code will set you in infinite loop of orientation changes if we don’t add few more pieces of code:-
1) In android manifest file:- 
<activity android:name=”activity_name” android:label=”@string/app_name” android:configChanges=”orientation|screenSize”>
…………
…………
</activity>

2) Add following code to catch orientation changes:-
@Override

public void onConfigurationChanged(Configuration newConfig){
     super.onConfigurationChanged(newConfig);
}

The entry in manifest file tells the system that I will handle the orientation changes myself and OnCreate should not be called. onConfigurationChanged method is called instead when orientation changes and we can add in here any code we want to execute when the orientation changes.

Advertisements

Single Post Navigation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: