android- handling the screen orientation a bit differently - android

I'm trying to make a UI which when the device is rotated, the icons get rotated.
Normally, when a device is rotated the onXXX lifecycle methods get called, and I'd like to avoid this when the screen orientation changes and would like to rotate the icons on the main UI instead. How can I go about achieving this?

Related

Black screen on Activity.setRequestedOrientation(...) call in Android

Does anybody know how to cope the problem?
Using the android.app.Activity#setRequestedOrientation(SCREEN_ORIENTATION_LANDSCAPE) method almost always causes the showing a black screen for a split second during the rotation.
But if I rotate the smartphone itself, then the black screen never appears.
How to get rid of the black screen? Is it the Android issue of the setRequestedOrientation() method, or I do something wrong?
EDIT: AFAIK the black screen with setRequestedOrientation() call could happen if we have quite heavy layout, when it takes some more time to initialize it in the UI thread. If you try to call setRequestedOrientation() for instance in a "Hello World!" app with a light layout, you won't see the black screen. So, the loading on the UI thread can be the cause of the black screen when you rotate with setRequestedOrientation(). But, in other hand why it never appears when I rotate the screen changing the phone orientation?
The only solution I've found.
Consider I want to turn screen to landscape and what do I do:
I remove the root Fragment with heavy stuff from the Activity I want to rotate.
Inside the Activity I want to rotate I open an empty white Activity for a moment to cover the black screen. The Activity is calling setRequestedOrientation(SCREEN_ORIENTATION_LANDSCAPE) in its onCreate callback and closes itself after the timeout = 500 ms. I open/close the Activity with fade-in/fade-out animation.
Inside the Activity I want to rotate I call setRequestedOrientation(SCREEN_ORIENTATION_LANDSCAPE) post delayed with 100 ms. This starts the rotation.
After the rotation complete, in the onCreate I add the removed Fragment back.
That's it. Rotation to landscape is finished and the black screen was completely covered.
Edited:
I had the same problem. In older versions of android, if the content of the layout is heavy (if it contains high resolution image, etc.), the running activity is trying to re-create the layout to change the orientation but can't achieve this.
As a solution, we can create 2 different layout resources as portrait and landscape, specifying which one we will use and making it easier for the android.

How to handle Display rotation in android

I need to handle display rotation in android and I know about handling Activity.onConfigurationChange with android:configChanges flags in manifest, but it does not work for me.
Unfortunately, onConfigurationChange is handled only when I change device orientation from portrait to landscape and vice versa, but it is not handled when I change orientation directly from landscape to landscape inverted i.e. from rotation 90 to 270. So, I need some callback, that will be called when result of android.view.Display.getRotation() changes. Any ideas?
When your orientation changes , Android system will create a new View and your app probably getting crashes.reason behind this is your background thread is trying to change the state on the previous one .
the another reason is that your background thread isn't on the UI thread.
my suggestion is that to making one handler that volatile and updating it when the screen orientation changes.

WebView rotates even if manifest declares that activity will handle orientation changes

I have a WebView embedded in a Fragment. In the manifest file, I have declared that the activity will handle orientation changes:
android:configChanges="orientation|screenSize"
and in the Activity, I have over-ridden onConfigurationChanged() in order to capture the orientation.
I thought this means that we have to explicitly take care of any changes in the screen orientation. But what I see is that the screen is still rotated (although the activity is not re-created).
If I use the following line:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
it does prevent the screen from being rotated, but I don't get the rotation event.
So, in short, I don't want the system to rotate the screen, and at the same time, I want to get an event from the system that the orientation has changed from portrait to landscape.
Thanks,
Rajath
If you override onConfigurationChanged(Configuration newConfig), you should be able to handle the changes.
Thanks for editing the question, what you're after is now clear to me. I've got two suggestionsScreen orientation (i.e. portrait, landscape, reversePortrait, reverseLandscape, etc) just depends on orientation of the device in 3D space. So one idea is to capture the 3D orientation of the device yourself, which is the same information that the operating system uses to make the screen orientation decision. This means that you need to capture the accelerometer and the magnetic field sensor readings. One example of capturing that information is in my answer to Android Compass that can Compensate for Tilt and Pitch.Alternatively, you might try setting up a dummy activity that exists purely to capture the screen orientation information. That activity could sit on the activity stack behind your main activity. Although I'm not sure whether activities that aren't on top of the activity stack are notified of screen orientation changes.

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.

Android orientation change glitch

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?

Categories

Resources