I created a virtual Android device, 480x800, ran it, and as I already found how to change the screen orientation (portrait-landscape, CTRL+F11/CTRL+F12/KP7/KP9), I used these keys to change the orientation.
But when I try one of these keys, the virtual screen rotates 90 degrees, but the orientation does not change. Hence, the Buttons are vertically placed on the screen, and all the text too. Just like a picture that is getting rotated 90 degrees.
How do I get the virtual degrees to actually switch over to the other layout file (/res/layout-land/activity_entry.xml)? Eclipse's graphical viewer does switching between landscape/portrait correctly.
EDIT
I added this part in the activity right under this the onCreateOptionsMenu(Menu menu)
method, but the application does not recognize screen orientation change:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Context context = getApplicationContext();
CharSequence text = "Orientation changed";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
EDIT 2
When I call this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) from within the onCreate() method, it works nicely.
check in settings tab if auto rotated is enabled, if it's turned off turn it on, then whenever you click rotation button it will rotate by himself
Check whether you have set any android:screenOrientation in the Manifest file for that acitvity. If yes, remove it and check.
If not working, it seems like a bug reported in android issues forums. Check here
First off number lock and then press 7 or press 9 and check emulator's orientation is changed and other way to change orientation of emulator is press ctrl+f12.
You may find, like I did that the orientation change was actually effected but the screen only changed a few seconds later than you would expect. In other words, the answer might be "patience grasshopper."
I ran into this recently and the answer provided here did not solve it. However I found this post:
Android emulator not rotating to landscape
Which mentions that there is a bug in some of the emulator targets (19 for example which I was using).
Simply changing the target to 17 made the rotation start working correctly.
First need to check in manifeast file you are setting android:screenOrientation="portrait" if yes then remove it. It's working for me.
Cold booting android emulator helps me to resolve the orientation not working issue.
Related
I am having issues with orientation change in multi-window mode.
The activity doesn't restart on changing orientation when the window size is 2/3 (in multi-window mode) in a tablet device. My layout looks improper due to that.
The activity restarts properly while changing orientation when the window size is 1/2 or 1/3 the display size in multi window mode.
I tried and confirmed this with Nexus 7 and Pixel C devices in (Android Studio) Emulator with Oreo and Pie images.
My app is targeting Oreo 8.1 and I am not using 'android:configChanges' or 'android:screenOrientation' in the manifest.
Note that 2/3 window size is not square and there is a small change in screen size in both orientation and if the orientation change doesn't trigger than the screensize change should trigger activity restart.
Steps to reproduce:
Start app in debug mode in a tablet device.
Put breakpoints inside onCreate and onResume.
Put it in multi-window mode (default 1/2 size).
Rotate the device and check if breakpoints trigger (it would).
Resize window size to 2/3.
Rotate the device and check if breakpoints trigger (it doesn't!).
Am I missing something which is causing this issue? or is this a bug or something else? How can I make my activity restart in this case?
An interesting question, I never knew that there are exceptions to the orientation change process. I was able to reproduce the problem with a Pixel C emulator(Android 8.1) and an app targeting API level 26.
I suppose your screen is not just another list because in this case the difference of 12 pixels (1688 vs. 1676 if I can trust LayoutInspector) would not matter, so I wanted to add an observation which may be helpful:
My app contains a custom View, and from the logs I can see that its methods onMeasure() and onLayout() are called even if the Activity'sonCreate() is not. So this is where you can step in and do some fine tuning.
Another option is to force the Activity to be recreated. You could do so by calling the Activity method recreate() (documented here) as soon as you detect that the screen dimensions have changed. To keep track of the screen size changes, you can create a custom ViewGroup overriding onLayout() and use this as the root of your Activity's layout file.
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.
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 );
In android 3.2, it adds a new compatibility zoom mode to give a new way to view fixed-sized apps on larger devices.
When switch fill screen mode(from zoom mode to stretch mode or from stretch mode to zoom mode), the activity will be re-created.
Is there any method to avoid re-creating activity?
Have you tried this in your AndroidManifest.xml
android:configChanges="keyboardHidden|orientation"
Hope this works for you.
I think this circumstance belongs to the category "Run Time Changes" - similar to the screen rotation.
Have a look here to see how to deal with that:
http://developer.android.com/guide/topics/resources/runtime-changes.html
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