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

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.

Related

Android - How to make a landscape layout on a fixed portrait screen?

My application's orientation is fixed to portrait due to some functionalities that always in action.
I need to add to the main activity a fragment with a landscape orientation, so it's layout should be portrait, but rotated 90 degrees.
My problem is that a TextView won't fit the screen because it's width is longer than the screen's width. Also, rotating it (or anything else) won't work properly because the rotation is being done after it's being located.
Is there any library that can handle such thing? Or what can I do to handle that problem?

Force rendering in landscape, largest mode, lock the aspect ratio so everything fits the screen

I'd like to force my webview application in landscape ( largest mode), lock the aspect ratio so it'll always fill the screen, disable zoom ( when double tapping) & disable scrolling as well.
Here's what I've tried this far.
WebView b=(WebView)findViewById(R.id.webview);
b.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
b.setScrollbarFadingEnabled(false);
b.getSettings().setJavaScriptEnabled(true);
b.getSettings().setLoadWithOverviewMode(true);
b.getSettings().setUseWideViewPort(true);
b.getSettings().setBuiltInZoomControls(false);
b.setPadding(0,0,0,0);b.setInitialScale(getScale());
private int getScale(){
Display display=((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width=display.getWidth();Double val=new Double(width)/new Double(800);
val=val*100d;return val.intValue();
}
Here's the rendering on my Galaxy note
Initial view: http://i.snag.gy/ol0Zh.jpg
View when double tapping on the screen ( the one I want as initial view): http://i.snag.gy/j6i3f.jpg
To force it to landscape, in your manifest add android:screenOrientation="landscape" to the activity. That will prevent the activity from ever going portrait.
I wouldn't suggest disabling scrolling- what if your app doesn't fit on a smaller screen device? How will they see your content? You could shrink the display ratio, but that hurts people with vision problems (or even normal people if it becomes small enough). I'm only in my 30s and I zoom in on most websites to read the text more easily.
Set
webView.getSettings().setUseWideViewPort(false);
We can then manually set the scale. Refer to stackoverflow.com/a/3916700/519955 for this.
For landscape we can programmatically set it using:
myActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
We can make changes in the manifest too as answered earlier also.

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.

Is it safe to assume that in Landscape mode, height is always less than width?

I'm starting out on graphics for my Android game, the game is always in landscape mode. At the moment I am scaling my graphics by setting their width/height as some fraction of the window height. This way they are always the same proportion of the screen. It seems to be working well, but with I have done very limited testing. But is it dangerous to assume that while working in landscape mode, the window height will always be less than window width, and therefore making it the main constraining factor?
This may not be a direct answer to your question; but the Android API defines the following orientations:
Orientation.LANDSCAPE, Orientation.PORTRAIT and Orientation.SQUARE.
Judging by these, I would assume that in landscape orientation, the window height will be less than window width. If the width were lower, it would amount to Portrait orientation, and if the width and height are equal, it should be treated as Square.
Having said that, you specifically ask about window dimensions; while I believe orientation is based on screen dimensions. So, I may be totally off track here!
Your safest bet is to have a setting for this. There are some non phone devices with android that have square screens or are landscape by default.
Just decide your target audience before anything. There's no such thing as a "universal" app.

Problem detecting orientation of phone

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

Categories

Resources