7

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?

Strider
  • 4,452
  • 3
  • 24
  • 35
facumedica
  • 658
  • 1
  • 7
  • 20

6 Answers6

5

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)
Alexander Ukhov
  • 458
  • 6
  • 10
2

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();
Biscuit
  • 4,840
  • 4
  • 26
  • 54
1

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"
0

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

android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize|uiMode" android:screenOrientation="fullSensor"

-1

Read this USER ROTATION

Use this:

android.provider.Settings.System.putInt(getContentResolver(), android.provider.Settings.System.USER ROTATION,user_rotation);

user_rotation could be 0, 1, 2 or 3;

  • 0 == 0º
  • 1 == 90º
  • 2 == 180º
  • 3 == 270º

You need to add the next line in the manifest.

<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>

PD: If you had tried to search, you could have found something. Like in this Post by Riddhish.Chaudhari

Community
  • 1
  • 1
Víctor Martín
  • 3,352
  • 7
  • 48
  • 94
  • Thank you for your reply Victor, but this does not work for me at all. Maybe I'm using it wrong, I putted that code inside a `OnClickListener`. Could you give me more details? Thank you (I've searched, but all I found does the same) – facumedica Feb 15 '16 at 16:01
  • with your line setRequestedOrientation(Build.VERSION.SDK_INT < 9 ? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT : ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT); You are locking the screen in portrait mode. – Víctor Martín Feb 15 '16 at 16:08
  • I don't have very clear, what is happen when you are in landscape. – Víctor Martín Feb 15 '16 at 16:12
  • I didn't use my old code, just what you posted, and it did the same. Okay, I'll try to explain myself better: The autorotation of the screen must always work. Also, I have a button in the landscape mode that rotates the screen back to portrait. I hope I explained myself better. Thank you for your interest! – facumedica Feb 15 '16 at 16:15
  • I've just posted you another answer. – Víctor Martín Feb 15 '16 at 16:22
  • Hi @facumedica,Did you got the solution for your query.Actually my requirement is exactly the same as yours,and i am still unable to find a proper solution to it – Arunabha Dutta Choudhury Aug 29 '16 at 06:51
-1

Try then to use a method to enable/disable auto rotation, after/before change the screen rotation. The code:

import android.provider.Settings;

public static void setAutoOrientationEnabled(Context context, boolean enabled)
        {
              Settings.System.putInt( context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0);
        }

Try to enable autorotation before rotate with your button

Víctor Martín
  • 3,352
  • 7
  • 48
  • 94