I have an activity which must always be launched in landscape mode. In my onCreate() method, I use the dimensions of the screen (width and height) to set up the interface, so it is important that the activity is not initially created with the wrong orientation.
To do this, I have the following in the manifest:
<activity android:name="app.myapp.WideActivity"
android:screenOrientation="landscape"
android:configChanges="keyboardHidden|orientation"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen"/>
This seems to work completely fine on my phone, but on my tablet (samsung galaxy tab, android 3.1) the activity is launched in portrait mode and switches orientation almost immediately (after the onCreate() method - it seems). On my phone (android 2.3) the screen is already landscape before the onCreate() method is called.
Putting setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
in the onCreate() method does't seem to help. How can I achieve what I'm trying to do?
I am not sure if you can fix the exact problem. It seems for me the problem is in samsung's implementation of activity/window management framework part.
However, you can try to prevent a crappy UI to appear by delaying the rendering.
In onCreate() show some default layout - just a plain black layout stretched to the entire screen.
In onResume() check the screen orientation, if it is not landscape then you post a delayed runnable which will apply your layout (setContentView(my_UI)).
Inside the runnable you also check if screen orientation is landscape, and re-post the runnable if orientation is still portrait, otherwise set the UI.
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.
I have an Activity with two layouts for landscape and portrait orientation. Everything works fine but! If I will lock a screen by power button, then press power button again, rotate device and unlock, my Activity will be empty. I have checked onCreate() method, onDestroy() and onResume(). If lock a screen and unlock without rotation everything will be ok and only onResume() will be called. But if unlock device with screen rotation methods onDestroy(), onCreate(), onResume(), onResume() will called (onResume() two times after rotate and after unlock).
I tried to use android:configChanges="orientation" - no result. Also I tried android:configChanges="orientation|screenSize" it is bad idea because after this activity will use only one layout depend on what orientation app was started
My device is Nexus 7 2013
I m building an app which an a main layout in portrait format
I locking orientation with
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
However, I need to detect screen orientation changes anyway because I want to load another Activity if the screen is rotated.
How to do that ?
I tryed onConfigurationChanged but it s never called.
Some things to look at if onConfigurationChanged isn't firing:
When you declare the manifest, make sure you have defined at least orientation and screenSize for the activity configChanges. Like this:
<activity android:name="com.test.act" android:configChanges="orientation|screenSize">
Make sure the device doesn't have rotation blocked. For example if you are using a Nexus 7:
http://www.howtogeek.com/120056/how-to-enable-landscape-orientation-on-the-nexus-7s-home-screen/
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 );
I encountered weird problem when changing screen orientation rather fast, or when something heavy goes on background. When I call setContentView(R.layout.main);on onConfigurationChanged (I handle orientation changes myself),after screen is rotated android for a brief moment sets layout for opposite to orientation. For example - when you change orientation layout changes like this Portrait>Landscape, but sometimes, in my case, it goes like this Portrait>Portrait(when phone is already in in landscape position)>Landscape.
Sometimes you dont even notice this, and sometimes screen shrinks to about 1/3 for a second.
Well, It's not really a big deal, but in my activity I have Coverflow widget (based on Gallery) and to make it look good on most of the screens I set image sizes based on parent container size (in onSizeChanged of widget), and when orientation changes goes as described above coverflow widget picks up a wrong size.
The only way I see now is to either call onCreate on orientation change (Which I cant do, because of heavy UI populated online), or set a delay on initializing UI (which is ugly).
So I wonder, is there any callback of setContentView() or anything else, so I could know when layout is actually ready, so I can init UI.
Or maybe there is another way around?
PS: For some reason this glitch mostly occurs on Desire HD.
AndroidManifest.xml
android:configChanges="orientation"
Is it what you expect?