Run the application in Landscape mode only? - android

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).

Related

How to lock android screen orientation

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,

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"

Detecting orientation like in camera app

I am developing application that is somehow similar to camera, I wish that the app will always be in landscape mode witch I achieve with:
android:screenOrientation="portrait"
But I need to know when user rotates the phone to landscape mode so I can rotate the icons 90 degrees.
I have tried:
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
Toast.makeText(getApplicationContext(), "ORIENTATION_LANDSCAPE", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(getApplicationContext(), "ORIENTATION_PORTRAIT", Toast.LENGTH_SHORT).show();
}
But it only works when I add:
android:configChanges="keyboardHidden|orientation|screenSize"
And its also change my orientation automatically.
So how can I keep my app always in Landscape mode and only detect when the phone is in Portrait without that phone automatically change anything? Just like in camera app
Using android:configChanges="keyboardHidden|orientation|screenSize" is a good idea.
Now try setting your orientation manually:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
This will help. It did for me and I needed to do exactly what you needed to do.
https://stackoverflow.com/a/5183690/687245

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