Java tutorial
//package com.java2s; /** This file is part of Adguard Content Blocker (https://github.com/AdguardTeam/ContentBlocker). Copyright 2016 Performix LLC. All rights reserved. Adguard Content Blocker is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Adguard Content Blocker is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Adguard Content Blocker. If not, see <http://www.gnu.org/licenses/>. */ import android.app.Activity; import android.content.pm.ActivityInfo; import android.content.res.Configuration; import android.view.*; public class Main { /** * Locks specified activity's orientation until it is unlocked or recreated. * * @param activity activity */ public static void lockOrientation(Activity activity) { Configuration config = activity.getResources().getConfiguration(); final int deviceOrientation = config.orientation; int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); int orientation = ActivityInfo.SCREEN_ORIENTATION_NOSENSOR; if (deviceOrientation == Configuration.ORIENTATION_PORTRAIT) { orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; if (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_180) orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; } else if (deviceOrientation == Configuration.ORIENTATION_LANDSCAPE) { orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; if (rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270) orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; } activity.setRequestedOrientation(orientation); } }