2

As you can see Youtube app for Android that can rotate screen from portrait to landscape even when option "Auto-rotate screen" is disable in setting. How can i do same it? Thank all guys!

Stefan
  • 4,997
  • 8
  • 26
  • 51
tungdx
  • 111
  • 2
  • 7

5 Answers5

3

Use - ActivityInfo.SCREEN_ORIENTATION_SENSOR if you want to rotate screen irrespective of user's choice else use ActivityInfo.SCREEN_ORIENTATION_USER. This will only rotate screen if its turned on.

Pawan Maheshwari
  • 14,658
  • 1
  • 47
  • 50
1

Use setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR), it will disregard the auto rotate option and just rotate whenever your device is rotated.

Lendl Leyba
  • 2,330
  • 3
  • 31
  • 48
0

You can also use

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

//This is the default value
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

public static void setAutoOrientationEnabled(ContentResolver resolver, boolean enabled)
{

    Settings.System.putInt(resolver, Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0);

}
Pratik
  • 30,557
  • 17
  • 84
  • 156
Shadow
  • 6,754
  • 5
  • 39
  • 88
  • if Auto-rotate screen" was disabled in setting, I don't know when my phone rotate. Are you understand my objective? – tungdx Jan 24 '13 at 10:15
  • Thank Helios-Ignite, i can set Orientation of Android device when use your method "setAutoOrientationEnabled" above. Thank you again! – tungdx Jan 24 '13 at 10:31
0

As it's shown here http://developer.android.com/guide/topics/sensors/sensors_overview.html you can register your own sensor (TYPE_ACCELEROMETER in this case) and then listen on it's events.

When you calculate that phone orientation has been changed you should programmatically change layout orientation then.

Pratik
  • 30,557
  • 17
  • 84
  • 156
Kamil Lelonek
  • 14,232
  • 12
  • 64
  • 89
  • [link](http://www.vogella.com/articles/AndroidSensor/article.html) and [link](http://www.slideshare.net/info_zybotech/android-accelerometer-sensor-tutorial) - here are the best tutorials in this topic which I know. – Kamil Lelonek Jan 24 '13 at 10:28
0

Use this in onCreate of that activity you want to be able to rotate the screen:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

It won't affect other activities or the whole device settings.

I have tried that. It works.

user1914692
  • 2,893
  • 5
  • 34
  • 57