Problem detecting orientation of phone - 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

Related

Codename One: Starting portrait app in landscape orientation and getDisplayWidth()

My app should only run in portrait and not in landscape orientation. For iOS this is a build hint (ios.interface_orientation) which works fine. For Android this has to be set in code using lockOrientation(true), which I put in public void init(Object context). After locking the orientation I call getDisplayWidth() in public void start(). When I start/test the app on an Android device that is in landscape orientation I run into problems. Apparently switching orientation takes time and sometimes the returned display width is actually the display height. I tried wrapping the code using getDisplayWidth() in a callSerially but that doesn't help. I also tried calling getDisplayHeight() as well and take the minimum of both values, but that doesn't work because if the width is returned as height then the status bar height is deducted and we end up with a display "width" value that is too small.
As a result, 1) my splash form (that has an image that is scaled to the display width) is first incorrectly shown - which is corrected while waiting for the next form to show, and 2) my GUI sometimes uses the wrong display width - so not the full width is used.
My question is how to make sure to get the correct (portrait) display width after a call to lockOrientation(true) when the device has to adjust from landscape to portrait.
I'm sure there are a lot of additional edge cases that reproduce this problem that you missed such as multi-tasking/side-by-side views etc. Display size can change even when locking orientation.
There are multiple things you can do such as override the components onLayout callback or listen to the forms size change event.

Get android device orientation even when autoRotation is turned off

I have an android app in which the screen orientation is set to portrait in manifest file.
It uses a custom camera in b/w , to take a photo and I need to save the photo.
Before I save the image I have to rotate the image depending on how the user is holding the camera, so that the image gets saved in the right orientation.
So, is there any way by which I can get the device orientation even when my app is running in portrait mode and android "auto-rotation" feature is ON.
How can I achieve this?
You should do the rotation depending on the orientation information within the image, not depending on the device orientation.
Here is some more information on how to determine the rotation.
Since this does not help, have you tried hackpod's answer here:
getResources().getConfiguration().orientation
To get current device orientation try this:
getWindowManager().getDefaultDisplay().getRotation();
Simply check, the height and width of the image. If the height is less than your width, it means the user was holding the phone horizontally, so you have to rotate it.
I am currently using android orientation sensor to solve this problem and its working perfectly. Till someone gives a better working method , am accepting this as answer for other who have the same question.

Android: correct way to get screen dimensions for target orientation?

My application is bitmap intensive, with pixel-exact layout (it's a sort of game, actually, and it's pretty hard to avoid this pixel-based coordinates).
What I wanted to do is to perform some layout calculations and bitmap pre-scaling in my onCrete - I use well known API - getWindowManager().getDefaultDisplay().getSize() - to retrieve the screen size and do my calculations.
However, I've just hit an unexpected problem. My activity is configured as landscape only, but if I start my application on emulator and onCreate() is called while the emulator is locked, the screen size returned by getSize() indicates portrait orientation. Once I unlock the screen, onCreate() is called again, this time correctly in line with expected landscape mode dimensions.
I'm not sure how to handle this situation. I see the following options:
for each onCreate() call perform full layout calculation and resource scaling again. This is the logically correct solution, but I don't want to the same work twice, just to throw away the first result.
if onCreate() is called for portrait mode, just do nothing, and set black background (I can see there's a silly rotate animation when I unlock the screen, so this would become pretty much a fade-in animation)
Actually I'd prefer second option, but I'm slightly afraid of any side-effects. Anyone faced this problem?
Update (2012-07-08):
I've probably assigned a slightly misleading title to this question (sorry!), as the problem is not in retrieving the dimensions itself, nor calculating the layout. It's more about the activity being first created in portrait mode, and then recreated in landscape mode again, despite being declared as landscape-only. I initially expected (reasonably, huh?) the activity to be created in landscape orientation only.
I eventually decided to fill the activity with black color when it's created in portrait mode, no side effects observed. On Android 4.0 I can see actual rotation animation when I unlock the screen - a bit strange, but well, I guess it is supposed to inform the user that she should rotate the phone. Given that in portrait mode I just fill the screen with black color, this animation looks sort of like a fade-in and rotation combined - perfectly acceptable.
Use that
DisplayMetrics dm=new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
Using this(Look code at down) only gives you screen size and if your views has static size they will be seen in different size on every different screen.
Display screen=getWindowManager().getDefaultDisplay().getSize();
How to use:
every screen has diffrent density. So use:
float density=dm.density;
with this density, you can set your views size like that:
(YOUR_ITEM_SIZE)*density;
also look here for additional information:
http://developer.android.com/reference/android/util/DisplayMetrics.html
if the emulator is locked , can't you assume that the user can't run anything anyway , so the app doesn't need to handle this end case ?
anyway , as bmavus wrote , use getMetrics for getting the screen size . also , if you need to change the screen orientation of the app , you can do so either in the manifest or in code.
for games , i would advice using opengl solutions , and if you don't have much time digging for it , you can use third party engines that can help you , such as andengine and libgdx.

How can I make my android tablet app to look the same when it runs on phone in landscape mdoe

I have a android app layout for tablet. Its layout is 'landscape' based (i.e. it looks the way i want when I run on tablet (by default landscape).
But when I run the same app on a phone, android runs in portrait mode, (it squeeze my 'landscape' layout into a portrait ).
I have tried putting 'android:screenOrientation='landscape' in my activity in my Manifest file. But that does not fix it.
Basically, what I want is when I rotate the phone by 90 % (the width is > height), I want the phone layout looks the same as what i see on tablet (which is landscape by default).
How can I do that? android:screenOrientation='landscape' does not work.
Basically, I want some thing the game 'AngryBird', it always runs in landscape mode regardless it is phone or tablet and whether the phone is rotated.
I think you need to use 2 flags, not just one.
First is
android:screenOrientation='landscape'
Second is
android:configChanges="orientation"
First will tell android to run activity in landscape mode, where as second will tell android that do not change the orientation even when user rotates the phone. Basically with 2nd flag you are overriding orientation config changes.
Haven't done this myself, but instead of putting the main.xml layout in res/layout, try putting it in res/layout-land, so it will display this layout in landscape mode, and in res/layout-port, maybe a portrait alternative to main.xml?
Create folder \res\layout-land\ and place there your XML file with landscape layout. Phone choose between portrait and landscape layouts automatically.
Add this line in the onCreate method before you call setContentView:
setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE );

Check a screen orientation when it's fixed by manifest file

I have an camera activity in my app, where user can make a photo. For some reasons it's fixed to landscape mode(by the screenOrientation in manifest). All works perfectly but I need to know - what screen mode on picture? User can rotate device and try to make a photo in portrait mode.
I've tried to use getRotation method but it returnes only 0 degrees, cause i'm set to portrait screenOrientation.
I want to ask - is there some ways to solve this problem?
Thanks for any help!
Use the orientation sensor, ain't as difficult as you might think.
http://www.workingfromhere.com/blog/2009/03/30/orientation-sensor-tips-in-android/
Android phone orientation overview including compass

Categories

Resources