lock Activity Orientation - Android android.app

Android examples for android.app:Activity

Description

lock Activity Orientation

Demo Code

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;

public class Main{

    public static int lockActivityOrientation(Activity activity) {
        int oldOrientation = activity.getRequestedOrientation();

        int currentOrientation = activity.getResources().getConfiguration().orientation;
        if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
        } else {//from ww w.  j av a2 s. c om
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
        }

        return oldOrientation;
    }

}

Related Tutorials