Device Orientation Change doesnt change layout - android

I have alternative layouts in my layout-normal-land and layout-normal-port folders and they are correctly invoked by the system according to if I hold the device in land or port at start. My problem is, when I rotate the device AFTER I have launched the app, it tries to somehow adapt the already displayed view to the new situation, creating a mess.
How can I tell the system it should switch to the alternate layout during execution?
Have been experimenting around and found that when I dont have android:configChanges="keyboardHidden|orientation" , oncreate gets called again, which gives the correct layout, but its not what I want to have. I dont think this is normal, is it? Maybe a question of bug in Android (2.3.3.)?

By including
android:configChanges="orientation"
in your manifest you are saying you want to handle orientation changes yourself. You should remove it if you want the system to handle it for you.
The automatic handling works extremely well. You should only override it if you have a specific reason for doing so.
In normal operation (without the above manifest entry), an orientation switch causes the current activity to be closed and then re-opened in its new orientation reloading all resources and layouts from the currently active resource folders. The process follows what is known as the "Activity Lifecycle".
If you include the above manifest entry, you are saying, "I will handle all changes myself. Do not close my activity" so it is then your responsibility to remove all unwanted layouts from the activity and replace them with the layouts you now require for the current orientation.

Do you already use an OrientationListener?
If no:
http://developer.android.com/reference/android/view/OrientationEventListener.html#onOrientationChanged(int)
the listener should detect orientation changes.
Then you call setContentView (R.layout.name_of_layout) in your Activity class.
Hope this helps.

do you have android:configChanges="orientation" in your manifest on that activity? That will prevent android from automatically changing your layout.

May be obvious, but:
Within the res folder make sure you have the folders labelled "layout" and "layout-land".
Portrait and landscape .xml files must have the same filename.

I have run into the same problem, and I did not have android:configChanges="orientation" in my manifest.
However I did have [Activity(ConfigurationChanges = ConfigChanges.Orientation)] in my mainactivity. That seemed to be another way to override the automatic orientation handling.

Related

My app is not working properly when screen is rotated

I am creating an app on android studio. The app is working fine but whenever I rotate the screen, the layout is changing, the items on the screen are getting overlapped and sometimes the app restarts.
What are the possible solutions? Is preventing the screen from rotation a good option? Please help. Thanks in advance.
You need to set orientation in AndroidMenifest.xml.Use keyboardHidden attribute if you are getting any issue in keyboard.
You can set you activity's screen orientation to portrait, by this your activity will never rotate, but if you want to support landscape version then you have to create a landscape layout as well and put the layout in layout-landscape folder, but you have to take care of your data also.
Choice is yours
You can prevent your activity to recreate when orientation changes. Just add this in your activity tag in Manifest file.
<activity
android:name=".YOUR_ACTIVITY_NAME"
android:configChanges="keyboardHidden|orientation|screenSize" />
Android Activites start from the beginning when you rotate a device. So your Activity Lifecycle methods like
onCreate(), onStart(), onResume()
will be called again.
There are two possible solutions, the first one is by Rob here.
<activity
android:name=".ACTIVITY"
android:configChanges="keyboardHidden|orientation|screenSize" />
This will handle the orientation changes itself.
Another solution is to create different layout types in different layout folders. The folders will be
layout-land, layout-sw600dp-land(for 7inch tablets) and layout-sw720dp-land(for 10inch tablets)
So when you rotate a device, the layouts in this folders will be automatically inflated by the system. There will be cases like when you want to preserve the current state of the activity, for this refer to this link
No, preventing the orientation to be landscape is not a good solution. Many tablets are landscape-only, and your app won't display correctly on those.
The correct thing to do is to save your state in onSaveInstanceState() and then read it in onCreate() when the activity is re-created (the savedInstanceState variable will not be null). For this to work, the data that you are saving must be Serializable or Parcelable.
If you want to save non-serializable data or you have something running in the background, you can use a Fragment with setRetainInstance(true); and no layout. See this link.
The view will be created again, yes, but you will set the saved values so that it will look as it was before the rotation.

Change Layout in Landscape with out destroying and recreating Activity

Hi Everyone iam new to android and stuck with the orientation problem i need to display separate layout in landscape and portrait which i designed separately and placed in layout-large and layout-large-land folders now i need to change layout when device is rotated to landscape with out destroying and recreating the Activity
please help me get out of this problem
Thanks in Advance
my advice as a long time Android programmer is:
Don't do it!
Let the activity be destroyed and re-built with the correct layout.
Just search and research on all the several methods of keeping the data during orientation changes and apply them to your specific case. Below a few to illustrate:
the onCreate(Bundle) receives that bundle that contains information saved during onSavedInstances(Bundle);
User a fragment without a UI (do not call onCreateView) and set it to be retained across rotation with setRetainInstance(true) and use it to remember the data
use the Loader pattern to automatically receive the data it was generated on the previous activity
Replace
layout-large-land
with
layout-land-large
Prevent activity from recreating/destroying
Add attribute android:configChanges="orientation" to the activity declaration in the AndroidManifest.xml file.
The purpose of the android:configChanges attribute is to prevent an activity from being recreated when it's really required.
Let me know if it works for you..
Try this,
Add this code in your mainfest.xml each and every activity.
android:ConfigChanges="keyboardHidden|orientation"

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 Lock Orientation and Double Create

I want to make my application displayed only in portrait orientation, so I have put android:screenOrientation="portrait" in the Activity tag in AndroidManifest.xml, and have put setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); in the Activity's onCreate method.
This works to lock the orientation to portrait, however, when the Activity starts, it shows itself once, then shows itself again, so you see a sort of flash. I can confirm that onCreate is being called twice as well.
This flash is causing further force closes later in my application.
How can I eliminate this flash?
Edit
I had a splash screen displaying before the activity I had described. It was being run twice, and therefore ran this activity twice (via an Intent) twice. I fixed this issue by checking if this activity had already been stared using an intent in the splash screen class, and it had, not to run it again. The fix was more of a workaround than a fix, but I hope it helps people.
I believe that using the Activity's configChanges attribute (in the manifest) should solve your problem. ConfigChanges attribute documentation
However, you are just going around another problem. What you really should address is why is that provoking FC? There is absolutely no reason for that to happen so you also should solve that problem. More info in handling runtime changes.
Please note: using the first approach is acceptable to address the performance/UX issues though.
Just in case you wonder why onCreate is called twice, once I got into this problem and I think that it was related to having the orientation fixed and then having another activity being created but in a different orientation. Before the second activity was started, the former activity changed into the orientation of the latter. And remember that keyguard is also an Activity! I'm not sure if this happens for this reason though.
don't put : "setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);".
Just let the AndroidManifest do his job. I thing what you did is :
Tell your App to go only in Portrait mode
Tell your Activity to be in Portrait mode, wherever it was already like this or not (The flash have to come from this).
I could be wrong.

Suspend orientation change

Documentation says: "a configuration change (such as a change in screen orientation, language, input devices, etc) will cause your current activity to be destroyed, going through the normal activity lifecycle process of onPause(), onStop(), and onDestroy()."
I would like to suspend the orientation change, since it crashes my app if it was done in the middle of a a loop (of reading a file).
How can I do this? Also - looking for some kind of "onOrientationChnage" function :)
First, why not solve the initial problem instead?
90% of the times, the problem that you are facing (when changing orientation) is solved in this question:
Background task, progress dialog, orientation change - is there any 100% working solution?
Read it, correct your code and see if the problems are gone!
If you still want to do it anyway:
Quoting the documentation:
Note: Handling the configuration
change yourself can make it much more
difficult to use alternative
resources, because the system does not
automatically apply them for you. This
technique should be considered a last
resort and is not recommended for most
applications.
Still want to do it? Read the following link (where I also found the quotation):
http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange
If you rely on android system to use different layouts when you are on portrait/landscape mode, you'll need to start to handle these differences by yourself. If you don't use these features, you may get away without doing nothing fancy :)
If you always want a particular orientation, you can add android:screenOrientation="portrait" (or ="landscape" as an attribute to your tag in the manifest.
At run time, you can call setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

Categories

Resources