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
  • 5,203
  • 8
  • 27
  • 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
  • 15,088
  • 1
  • 48
  • 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,287
  • 3
  • 34
  • 49
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,639
  • 18
  • 84
  • 159
Shadow
  • 6,864
  • 6
  • 44
  • 93
  • 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,639
  • 18
  • 84
  • 159
Kamil Lelonek
  • 14,592
  • 14
  • 66
  • 90
  • [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
  • 3,033
  • 5
  • 36
  • 61