force Current Orientation - Android android.app

Android examples for android.app:Screen

Description

force Current Orientation

Demo Code

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.view.Surface;

public class Main {

  public static int forceCurrentOrientation(Activity activity) {
    int prevOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR;
    if (activity != null) {

      prevOrientation = activity.getRequestedOrientation(); // save current position

      if (activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
        if (activity.getWindowManager().getDefaultDisplay().getRotation() == Surface.ROTATION_0
            || activity.getWindowManager().getDefaultDisplay().getRotation() == Surface.ROTATION_90) {
          activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        } else {/*  w  w w  . j  a v  a  2  s.c om*/
          activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
        }
      } else if (activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
      } else {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
      }
    }
    return prevOrientation;
  }

}

Related Tutorials