In android, we can set the value 0/1 to orientation Accelerometer to lock the phone at portrait mode or rotate based on sensor state. I have make vary searching on the web but i can't find what value i should set to lock the phone at landscape mode. Some one can help me?
android.provider.Settings.System.putInt( getContentResolver(),
android.provider.Settings.System.ACCELEROMETER_ROTATION,
isLocking ? 0 : 1);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
set above line after your setContentView(view); this will set you device orientation of that activity to landscape
Related
I have an Activity which override public void onConfigurationChanged(Configuration newConfig) method. It receive event when user change orientation from landscape to portrait or from portrait to landscape . But don't show any event when i change orientation Landscape Right to Landscape Left or Landscape Left to Landscape Right. Is there any way to get even while changing orientation Landscape to Landscape ??
You can detect rotation side with accelerometer. When you are certain of some orientation landscape/portrait set the current angle. If orientation is changed calculate the difference to see if its 90>(roughly) and if it is, obviously screen was rotated from portrait to potrait or landscape to landscape. With that calculation you could also detect was the rotation made by the left or the right side. You get the point. Also check these answers:
https://stackoverflow.com/a/10383164/5577679
how to detect orientation of android device?
Get rotation and display in degrees
And sensors overview:
https://developer.android.com/guide/topics/sensors/sensors_overview.html
My application is in Portrait mode and it has one video view in it, on some events i want to rotate only the video view to landscape mode is it possible to do so?
From the code can we modify a single view to be of landscape mode as other will remain in portrait mode?
Also i have onOrientationChanged listener in my activity which provides orientation values as device orientation changes. how can i determine if device is in landscape or portrait mode from these values?
Yeap, it's possible.
You can get the orientation in runtime with this code:
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
//orientation will contain Configuration.ORIENTATION_PORTRAIT or Configuration.ORIENTATION_LANDSCAPE
int orientation = display.getOrientation();
So, if you want to make something when changing the orientation you can override the onConfigurationChanged method.
And if you want to change the orientation in runtime you can use this (in example, change to portrait):
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
If want more details, read this
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!
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.
Use setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR), it will disregard the auto rotate option and just rotate whenever your device is rotated.
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);
}
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.
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.
I'm displaying a camera activity in Android 2.2 and I lock the screen orientation in landscape mode. Is there a way to get the device rotation?
I mean, getOrientation() always returns LANDSCAPE and getRotation() always returns 1. How can I get the actual rotation of the device even if I lock the orientation in landscape mode?
You can use the SensorManager class, see http://www.workingfromhere.com/blog/2009/03/30/orientation-sensor-tips-in-android/
In my onCreate method, I'd like to detect the orientation and set an appropriate background image.
I can get the orientation like so:
Display dis = getWindowManager().getDefaultDisplay();
int orientation = dis.getOrientation();
I've tested this on both the Galaxy S and the Moment.
When I am in portrait mode, this returns a value of 0. When I am in landscape mode, this returns a value of 1.
However, the define Configuration.ORIENTATION_LANDSCAPE has a value of 2 and the define Configuration.ORIENTATION_PORTRAIT has a value of 1.
So, when I turn the phone to landscape mode, it gives me Configuration.ORIENTATION_PORTRAIT. And when I turn the phone to portrait mode, it gives me Configuration.ORIENTATION_UNDEFINED.
What is going on???
I'm using API level 7
One thing you could do is just put the landscape drawable into a /drawable-land/ folder, and Android will pull it automatically depending on the orientation. Rather than relying on that, though, you would be better off to make a landscape version of the layout under /layout-land/ that has the alternate version as its background.
Look at the answers in Check orientation on Android phone