I'm trying to achieve this behaviour:
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?
setRequestedOrientation(ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE); Called on an activity, will lock it to landscape.
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.
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.
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)
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();
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With