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"
Related
I have a DialogFragment in my Android app, and I need to set android:configChanges="orientation|screenSize, I want to load different layout for my DialogFragment in portrait or landscape, but now Android will not automatically do this for me, I think I need to update the layout in onConfigurationChanged , but I don't know how to do it, any sample code will be better, thank you!
Another most common solution to dealing with orientation changes by setting the android:configChanges flag on your Activity in AndroidManifest.xml. Using this attribute your Activities won’t be recreated and all your views and data will still be there after orientation change.
try this code and changed your configuration :
<activity
android:name="com.example.test.activity.MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden"/>
This attribute informs to Android system that you are going to handle orientation and screenSize changes for this Activity. So instead of destroying and recreating your Activity, Android will just rotate the screen and invoke one of the lifecycle callback method which is onConfigurationChanged(Configuration).
try this
I finnally find my solution, but I think it maybe only work for my situation, my situation is I want to show a imageView in portrait and not show in landscape, so I set the visibility as visiable or gone in function onConfigurationChanged' and it works
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.
I am sorry, this question has been asked probably thousand times over, but I do not find the correct words how to ask this question.
I have an activity in android. When I turn the screen at 90 degrees, it looks like the current activity is completely destroyed and a new activity is initiated. Is that correct?
But if I have showed some data in one orientation, I want to show the exact same data in the other orientation? Is there some SIMPLE way to achieve this? That the activity just remains, but shows the layout with the SAME data from the SAME activity in the other orientation?
there is an easy way to save your time, if your project api level is upper than Android 3.2 (API level 13) add this code into your activity in manifest:
android:configChanges="keyboardHidden|orientation"
Update: this also can help:
android:configChanges="keyboardHidden|orientation|screenSize"
Yes whenever an activity changes its rotation it is destroyed and recreated. and the same android uses same layout and roughly shows it into landscape mode.
You can make a new folder layout folders name layout-land.
layout-land may contain the same layout as in layout folder and the views can be tweeked according to the landscape orientation requirments
Another way would be to store all your information in savedInstanceState and restore it from there in onCreate(). Or use a Fragment with setRetainInstance(true) to store your information in.
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.
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