Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Rotate screen programmatically without locking it [duplicate]

I'm trying to achieve this behaviour:

  • When I rotate the device to landscape, the screen does it too.
  • To get it back to portrait there are two ways:
    1. Rotating the device
    2. Clicking on a button that appears only in landscape mode that will rotate the screen back to portrait.

The problem: The button that puts the screen back to portrait works just fine but then I want to be able to rotate the device to landscape and rotate the screen, but it remains locked in portrait.

The behaviour is like youtube player, where you rotate or click the button to exit fullscreen.

My code for the button:

findViewById(R.id.exit_fs).setOnClickListener(new Button.OnClickListener(){
    @Override
    public void onClick(View arg0) {
        setRequestedOrientation(Build.VERSION.SDK_INT < 9 ?
        ActivityInfo.SCREEN_ORIENTATION_PORTRAIT :
        ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
    }
});

Any ideas?

like image 786
facumedica Avatar asked Feb 15 '16 15:02

facumedica


People also ask

How do I lock orientation in android programmatically?

setRequestedOrientation(ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE); Called on an activity, will lock it to landscape.

How do I stop android from restarting activity when changing orientations?

If you want the activity to not restart during screen orientation change, you can use the below AndroidManifest. xml. Please note the activity android:configChanges=”orientation|screenSize” attribute. This attribute makes the activity not restart when change screen orientation.

Can you force screen rotation on android?

The answer is that you cannot globally set orientation. Any app can choose to handle orientation itself, and if it does so then it can choose to ignore changes to the device's orientation, or to change orientation based on its own criteria.


3 Answers

I'v found this approach very nice:

// E.g. fullscreen button pressed - set landscape orientation;
// it will disable orientation by the sensor.
setRequestedOrientation(SCREEN_ORIENTATION_LANDSCAPE)

// Assume now user rotated the device... or not.

// Anyway enable orientation by the sensor after few seconds, so user may use app normal way.
someView.postDelayed(
    { setRequestedOrientation(SCREEN_ORIENTATION_SENSOR) }, delay = 5000L)
like image 198
Alexander Ukhov Avatar answered Oct 24 '22 05:10

Alexander Ukhov


I had the exact same problem. What I ended up with was using an OrientationListener to detect when the user had actually tilted the phone to landscape and then setting the orientation to SCREEN_ORIENTATION_SENSOR.

OrientationEventListener orientationEventListener = new OrientationEventListener(getActivity()) {
@Override
public void onOrientationChanged(int orientation) {
    int epsilon = 10;
    int leftLandscape = 90;
    int rightLandscape = 270;
    if(epsilonCheck(orientation, leftLandscape, epsilon) ||
       epsilonCheck(orientation, rightLandscape, epsilon)){
            getMainActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
        }
    }

    private boolean epsilonCheck(int a, int b, int epsilon) {
        return a > b - epsilon && a < b + epsilon;
    }
};
orientationEventListener.enable();
like image 22
Rohan Goyal Avatar answered Oct 24 '22 05:10

Rohan Goyal


Firstly put your Auto rotation On from Android setting.

then use the below code on your Activity where you are configuring Video to Landscape mode.

override fun onConfigurationChanged(newConfig: Configuration) { super.onConfigurationChanged(newConfig)

    if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        // if you tilt your screen to Portrait mode
         // send your seek position with intent to continue watching video to your previous screen
           finish()
    }else{
       // continue to playing video
    }
}

In your manifest file put screenOrientation to "fullSensor" like below:

android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize|uiMode"
        android:screenOrientation="fullSensor"
like image 44
Abhinav Srivastava Avatar answered Oct 24 '22 05:10

Abhinav Srivastava