Orientation issue with landscape mode - android

I am displaying Timer on a screen .
After starting the Timer , I am changing the orientation from the portrait to landscape mode. In order to avoid the timer getting reset when changing from the portrait to landscape mode , I am adding the following code to the activity declaration in the manifest:
android:configChanges="orientation"
The problem I am facing is that , when the screen is switching from the portrait to landscape mode , the layout which gets displayed is same as portrait & not in landscape mode. Only a part of the screen of the landscape orientation is being occupied.
Is there any way by which we can avoid resetting the timer from changing from the portrait to landscape mode & also have a proper landscape layout?
Kindly provide your inputs.
Thanks in advance.

There is a simple way of doing this.
if you have two different layouts for landscape and portrait then let the android handle all the stuff for you. i mean do not override methods onConfigurationChange() method unless strictly required and do not add android:configChanges="orientation". just make different folders for portrait mode and landscape mode. viz. layout and layout-land....
To save present state of views use bundle. whenever orientation changes android reload activity and call onCreate() method. This saved bundle is passed in onCreate() method. Now you can retrieve views' state from this bundle.
now next question will be how to use this bundle. Then here is the quick example.
override onSavedInstanceState() method to save bundle.
Thanks.

Is there any way by which we can avoid
resetting the timer from changing from
the portrait to landscape mode?
Yes, store the value somewhere during onPause and read it during onResume.
Also have a proper landscape layout
Read up on Providing Resources, in particular table 2's "Screen orientation" section which also links to a very nice page about handling runtime changes.

All you need to do is make the manifest file set the screenOrientation="landscape"
<activity android:name=".main"
android:label="#string/app_name"
android:screenOrientation="landscape">
<intent-filter>
That turns off the auto sensor on only the form that you name to have landscape.

Related

When would one justify overriding orientation handling

When would one justify adding this to manifest.xml?
android:configChanges="orientation|keyboardHidden|screenSize"
Thankyou
Yes its required based on your requirement. I had an app in which we had to show videos on orientation change the video would restart hence i had to overide the onConfigureation() method and handle it, As i said it depends on your requirement
Adding this to the manifest means that you will control what happens when orientation changes, keyboard is minimized, screen size changes. If you do not add this then the onCreate gets called again on these events.
But say you have a different layout for landscape and portrait. Then you would like to call onCreate again to render the landscape layout.

data lose and force close in landscape and portrait mode android

I am developing an android application I am facing below two problems,
data lose : login screen when I change the landscape to portrait mode the entered text will be lost.
force close: Inside the application when I captured the image form camera then I change the portrait to landscape mode some time getting force close and image not showing in ImageView.
How to solve this problem? I should not lock the application in portrait mode and I have no idea to solve this please suggest me any useful links and sample code it might be useful for me.
add below property in your mainifest file inside the activity.you can change as per your requirement
android:configChanges="orientation|screenSize|keyboard|screenLayout"
In order to prevent data loss, add configChanges to the manifest, and to it set the values: screenOrientation, screenSize, layoutDirections.
and if that too does not work,
override the onConfigurationChanged method, and handle what you have to do with the data in that method.
1)
android:configChanges="orientation|screenSize|keyboard|screenLayout"
add this to you AndroidManifest.xml file for which makes sure onCreate() is not called again when orientation change happens, but only the ui rendered will change maintaining the user entered data on UI.
<activity
android:name="LoginActivity"
android:configChanges="orientation|screenSize"
android:label="#string/app_name"
android:logo="#drawable/menu_button"
android:windowSoftInputMode="adjustPan|stateHidden" >
2)
need more info, however this might be case of when orientation gets changed ui views will be recreated hence will be null for some very small fraction of time and your code might be trying to access the same view in this time.
I feel 1) and 2) are related, solve 1) using above and try to reproduce 2) again.

Recreating WebViews while changing the orientation in android

I have built an application and the best part is, it is running fine and the worst part is,whenever I hold the device and turn it from portrait to landscape or vice versa,the views are regenerating each and every time when the orientation changes.I have done all the possible things to the best of my knowledge i.e.
1.created layout-land folder and placed the xml file for the landscape mode.
2.Have given the following permission in the manifest:
android:configChanges="orientation|keyboardHidden"
Is there anything else I have missed out?
I do not want the changes or recreation to occur whenever I change the orientation.
By default,Android activity is recreated when orientation is changed, and xml layout used by default is xml that you have in "layout" folder unless you have a separate xml in "layout-land" for landscape mode.
On the other hand if you want to handle Orientation or any other config changes by you self and to avoid calling OnCreate() then use "android:configChanges" in manifest file and Overide OnConfigChanges in your activity.
The reason that it is recreating your views is because the oncreate is being called. This will ideally reset all your views. If you override the method that handles the orientation changes, you will catch it a lot easier and preserve your views.
Ex:
http://jnastase.alner.net/archive/2010/10/27/handling-orientation-change-in-android.aspx

Android - detect orientation change, but have interface not respond

Is it possible in Android to capture screen rotate events but not actually have the activity respond to the rotate events?
To make things more difficult, I've got an activity backed by a TabActivity, and in one of the tabs I want to rotate some of the content area on screen - but leave the tab in portrait mode.
I've tried setting the activity orientation to portrait in the manifest, but then I no longer get the onConfigurationChanged(..) events. However, If I remove this line, the whole tab rotates as well.
Any help appreciated!
You might want to add "orientation" to activity's configChanges property in AndroidManifest.
android:configChanges="orientation"
http://androidappdocs.appspot.com/guide/topics/manifest/activity-element.html#config
Set the orientation to portrait mode in the manifest as you did, but listen directly to the accelerometer sensor (which is the sensor from which Android derives the orientation of the device) updates and parse them yourself to know when the orientation changed.
It's actually quite easy to do. See here for an example: http://www.anddev.org/accessing_the_accelerometer-t499.html

Android - Handle Screen Orientation

I'm currently working on an Android application that have different behaviors according to the screen orientation.
I'm using getWindowManager().getDefaultDisplay().getRotation() to get the screen orientation in the onCreate() method and manage from there.
Basically, my problem is that I need a control over the orientation. In landscape mode, when the user decides it, the application must switch to portrait mode, and stays like that, but only until the use goes to portrait mode, and then again to landscape.
I'm using setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) to change the orientation when in Landscape. The problem is, using this, I can not go back to landscape.
If I use setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) in onCreate(), the problem is that is switches back to landscape mode after a few seconds (since the screen is still in landscape).
Is there any other way to manage this? Basically, is there a way to catch the screen orientation change with a listner (or anywhere else than in onCreate, since it's not called when the views is locked in portrait mode)? I can't find anything about that...
Any idea is welcome!
Thanks!
Why not try creating two layouts, one in layout-land and one in layout-port. These will automatically be applied when the device orientation changes.
Inside each of these, create two distinct child layouts one for a portrait view, and one for a landscape view and change the visibility of these to reflect the user's preference.
So, you don't need to handle the rotation change at all, let Android manage that for you, and you just manage the user override.

Categories

Resources