How to lock android screen orientation - android

I'm making an Android video player.It has a function like that user can watch video in any orientation.I just use code as follow:
Settings.System.putInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 1);
It works, but when I add a function that user can lock the orientation, I just did it:
Settings.System.putInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0);
So I met some trouble. when I'm in landscape orientation and try to lock the orientation, the screen just turns to portrait.
Can anyone solve it or tell me another way to do with it?

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
OR
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
More info here: Developing Orientation-Aware Android Applications

Use following code Based up on your condition change if else statement
int currentOrientation = getResources().getConfiguration().orientation;
if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE)
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}
else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}
or
you can set anyone landscape or portrait in your activity.its never change during the screen rotation
<activity android:name="MyActivity"
android:screenOrientation="landscape"
android:configChanges="keyboardHidden|orientation|screenSize">
...
</activity>

In your AndroidManifest.xml, for each activity put
android:screenOrientation="landscape"
It forces the activity to landscape.

just you have ot add following property in you manifest.xml file.
android:screenOrientation="portrait"
like this way,
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" >
</activity>
That.sit

You can request ScreenOrinentation public void setRequestedOrientation (int requestedOrientation). You can use it like this
// For landscape
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//OR for portrait
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//OR reverse landscape
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
//OR for reverse portrait
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
requestedOrientation An orientation constant as used in ActivityInfo.screenOrientation.
Added in API level 1
The preferred screen orientation this activity would like to run in. From the screenOrientation attribute, one of SCREEN_ORIENTATION_UNSPECIFIED, SCREEN_ORIENTATION_LANDSCAPE, SCREEN_ORIENTATION_PORTRAIT, SCREEN_ORIENTATION_USER, SCREEN_ORIENTATION_BEHIND, SCREEN_ORIENTATION_SENSOR, SCREEN_ORIENTATION_NOSENSOR, SCREEN_ORIENTATION_SENSOR_LANDSCAPE, SCREEN_ORIENTATION_SENSOR_PORTRAIT, SCREEN_ORIENTATION_REVERSE_LANDSCAPE, SCREEN_ORIENTATION_REVERSE_PORTRAIT, SCREEN_ORIENTATION_FULL_SENSOR, SCREEN_ORIENTATION_USER_LANDSCAPE, SCREEN_ORIENTATION_USER_PORTRAIT, SCREEN_ORIENTATION_FULL_USER, SCREEN_ORIENTATION_LOCKED,

Related

How to lock screen rotation in the code on Android 4.2 and lower?

I have used ActivityInfo.SCREEN_ORIENTATION_LOCKED and ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED to control screen rotation in Android 4.4,it is good.But when I set the ActivityInfo.SCREEN_ORIENTATION_LOCKED in Android 4.2 ,it is useless.
How can I do it?
You have to declare in your manifest
<activity android:name=".MyActivity"
android:label="#string/app_name"
android:screenOrientation="portrait">
also you can use android:screenOrientation="landscape"
And in your code something like
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Also you can see this guide.
You'll have to override it programatically. Get the current orientation, then if you toggle to fix the state, set that orientation as your application's orientation.
For Example:
int rotation=getResources().getConfiguration().orientation;
//ORIENTATION_LANDSCAPE = 2, ORIENTATION_PORTRAIT=1
if(rotation==1){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}else{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
This will lock it to the current orientation.
And in order to release it, simply use ActivityInfo.SCREEN_ORIENTATION_SENSOR like this:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
That's it.
Now, the final code will look something like this:
if(allow_screen_change.equals("yes")){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
}else if(allow_screen_change.equals("no")){
int rotation=getResources().getConfiguration().orientation;
//ORIENTATION_LANDSCAPE = 2, ORIENTATION_PORTRAIT=1
if(rotation==1){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}else{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
I just made this and tested on HTC One X, running Android 4.2 and it works just fine. Hope it helps!

Android: Prevent Orientation to change programmatically

I'm using:
setRequestedOrientation(getResources().getConfiguration().orientation);
and later on:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
it prevents the orientation to change until a task is finished, but it only works on portrait, when app is on landscape it doesn't stop orientation to change.
Any suggestions?
Oim~
I got it to work properly in all cases now.
To fix the screen:
if (getWindowManager().getDefaultDisplay().getRotation()== Surface.ROTATION_0)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
if (getWindowManager().getDefaultDisplay().getRotation()== Surface.ROTATION_90)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
if (getWindowManager().getDefaultDisplay().getRotation()== Surface.ROTATION_270)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
and then to allow rotations again:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Oim~
In manifest file with your activity try this
android:screenOrientation="sensorLandscape"
android:windowSoftInputMode="stateAlwaysHidden"
Inside application tag in manifest file try this
android:hardwareAccelerated="true"
android:largeHeap="true"

How to use onConfigurationChanged() and newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE in android 2.3.3

I am using onConfigurationChanged(). In that, when I am changing from LandScape to Portrait, it is calling if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) and shifting to Portrait from LandScape. But when I am changing from Portrait to Land-Scape, it is not changing to LandScape because it is calling if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) so, it is not changing from LandScape to Portrait.
Please help.
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
//int orientation = this.getResources().getConfiguration().orientation;
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Log.d("Entered to change as Portrait ","PPPPPPPPPPPPPPPPP");
setContentView(R.layout.settings);
} else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Log.d("Entered to change as LandScape ","LLLLLLLLLLLLLLLLLLLL");
setContentView(R.layout.settings);
}
}
Just write the below code into onConfigurationChanged method and test
if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
Log.e("On Config Change","LANDSCAPE");
}else{
Log.e("On Config Change","PORTRAIT");
}
and write the android:configChanges="keyboardHidden|orientation" into your manifiest file like this
<activity android:name="TestActivity"
android:configChanges="keyboardHidden|orientation">
it's working at my side, i hope it helps you.
If you're on tablet also add |screenSize to android:configChanges
you should also be aware of:
Caution: Beginning with Android 3.2 (API level 13), the "screen size"
also changes when the device switches between portrait and landscape
orientation. Thus, if you want to prevent runtime restarts due to
orientation change when developing for API level 13 or higher (as
declared by the minSdkVersion and targetSdkVersion attributes), you
must include the "screenSize" value in addition to the "orientation"
value. That is, you must decalare
android:configChanges="orientation|screenSize". However, if your
application targets API level 12 or lower, then your activity always
handles this configuration change itself (this configuration change
does not restart your activity, even when running on an Android 3.2 or
higher device).
The issue is with the android:configChanges="orientation|keyboardHidden" .
if you remove |keyboardHidden from the androidmanifest.xml, then the onConfigurationChanged is only fired when you rotate from landscape to portrait, not when you go from portrait to landscape (at least in the default emulator).
Hope this helps.
In addition to above answers: If you override the onConfigurationChanged method in combination with the updating of the manifest file you are saying you are handling the configurationchanges yourself. Therefore adding:
setContentView(R.layout.layout.xml);
In your onConfigurationChanged method - where layout is your layout file - will fix the problem.
Write below code in your activity file.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
Log.e("On Config Change","LANDSCAPE");
}
else{
Log.e("On Config Change","PORTRAIT");
}
}
Also write below code in your manifest file as follows:
<activity
android:name=".activities.TestActivity"
android:configChanges="orientation|screenSize|keyboardHidden"
/>
Write this line in the android Manifest.xml file:
<activity
android:name="MyActivity"
android:configChanges="orientation|keyboardHidden" />

Run the application in Landscape mode only?

I want to run a Hello World sample application only in landscape mode on my Device.
If the device changed to portrait, I would then like to raise one toaster. For example "please change to landscape mode to run your application".
How should I do this?
You can go for both programmatically as follows:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
or also in manifest file you can give here as
<activity android:name=".YourActivity" android:screenOrientation="landscape" android:configChanges="keyboardHidden|orientation"/>
add this to your activity tag in your manifest file:
android:screenOrientation="landscape"
It will fix your orientation to landscape mode and you need not to show any toast to user.
in your Manifestfile(xml)
<activity android:name=".ActivityName" android:screenOrientation="landscape">
</activity>
refer this also Is that possible to check was onCreate called because of orientation change?
You may try something like this
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
Log.v("log_tag", "display width is "+ width);
Log.v("log_tag", "display height is "+ height);
if(width<height){
Toast.makeText(getApplicationContext(),"Device is in portrait mode",Toast.LENGTH_LONG ).show();
} else {
Toast.makeText(getApplicationContext(),"Device is in landscape mode",Toast.LENGTH_LONG ).show();
}
You can use the configChanges-attribute for the Activity you want to catch the screen-orientation change for.
After that, the onConfigurationChanged()-method in your Activity will be called when the orientation changes.
To check which orientation is currently displayed, check this older post: Check orientation on Android phone
After that, you can show off your message or do whatever you like (of course you can also set it to only show in landscape).

Get Locked Orientation

Is there a method to detect if the Auto-Rotation Lock of the device is on or off? My app needs to access that method to lock the orientation during runtime.
Add this in your Manifest file in your activity for the fixed orientation you want for your Activity
android:screenOrientation="portrait"
like
<activity android:name=".Settings" android:label="#string/app_name" android:screenOrientation="portrait">
or in java code you can use
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
In iPhone there is a method
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return YES;
}
To unlock orientation. if u want to make it lock then change its value (YES to NO) where u want to lock orientation

Categories

Resources