Detecting orientation like in camera app - android

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

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"

how do i restrict the landscape orientation dynamically in mobile ?and no restricion in tablet in the samecode?

int currentOrientation = getResources().getConfiguration().orientation;
if (currentOrientation == Configuration.ORIENTATION_PORTRAIT) {
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}
else {
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}
when i am rotating the mobile , my content is also rotating.In landscape and portrait i want same screen only.in the same way i want screen rotation in tablet.
To detect whether app is running on phone or tablet, refer to this approach https://stackoverflow.com/a/9308284/923820 as Marius suggested.
Then, put this code in those Fragments which you want to prevent from orientation change:
#Override
public void onResume() {
super.onResume();
if (!tabletSize)
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
This would lock your screen in portrait mode when your app is running on phones. Change to ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE if you want to lock it in landscape mode instead.

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

Categories

Resources