I have two layout files, one for landscape (layout-land) and one for portrait (layout-port). When I run my application the correct layout is grabbed initially dependent upon how I am holding my phone. If I am holding landscape, landscape is grabbed and visa versa. BUT, once I initially launch my application, if I change orientation the first layout grabbed at launch is used when switching orientation.
How do I get Android to changed layout xml per orientation change after initial launch of applicaiton?
In my manifest I have android:configChanges="orientation/keyboardHidden"
When you specify configChanges in the manifest for an activity, the activity has to handle the events by itself. Either do the required layout change in the activity or remove the configChanges in the manifest.
http://developer.android.com/guide/topics/manifest/activity-element.html#config
Related
I have an editText and some buttons within an activity. Every time I tilted the phone to landscape mode I reset it to the standard value. To change this, I added this line to the activity manifest:
android:configChanges="orientation|keyboardHidden|screenSize"
And with this I got the same values in it changing phone orientation. But my layout was perfect in portrait mode and awful in landscape mode(buttons were off screen). So I got two xml files: layout and layout(land). In this way, if the phone is tilted when this activity is called, layout(land) is called. Otherwise, the portrait layout is called. But every time I tilt my phone after the activity was called the layout used keeps the same. I would like to know how to dynamically change layout during activity call and after it.
You are preventing the activity from reloading by specifying android:configChanges in the manifest.
Removing this will allow the activity to reload on rotation change and therefore handle the layout correctly. You will then need to handle the life cycle events of the activity to save and restore transient data during the rotation.
https://developer.android.com/training/basics/activity-lifecycle/recreating.html#SaveState
Normally the layout file in /res/layout are applied to both portrait and landscape.
For example,
/res/layout/activity_main.xml
you can add a new folder
/res/layout-land, copy activity_main.xml into this folder and do necessary adjustments
Make Sure the name of the folder should be layout-land for landscape mode.
I am facing a small problem.In my application i am trying to have portrait and landscape view for audio streaming app in which i am using custom view to show visualizer on layout.
I have created two folder one is layout and another is layout-land and i put xml into that with same name but change in code for size,width.
Also i have added `android:configChanges="orientation|keyboardHidden"
But when i rotate my handset , orientation change take place to landscape but xml is displayed from default layout only.. it doesn't take layout-land xml to show landscape mode.
as i am using custom view to display visualizer on my both land and port xml so my mediaplayer object is connected to XML and when i change orientation than media player object get recreated and start playing music
Please refer developer.android.com
It specifies:
android:configChanges
Lists configuration changes that the activity will handle itself. When a configuration change occurs at runtime, the activity is shut down and restarted by default, but declaring a configuration with this attribute will prevent the activity from being restarted. Instead, the activity remains running and its onConfigurationChanged() method is called.
Since you have specified android:configChanges="orientation", it means that your activity itself will handle orientation changes from onConfigurationChanged().
Remove android:configChanges from manifest. Your problem will be solved.
When you write,
android:configChanges="orientation|keyboardHidden"
your activity is not recreated, so its not loading the xml from the layout-land folder. If you want to load xml from layout-land folder you have to manually change the overriding onConfigurationChanged() and handling the config changes yourself. Check out my answer here.
I have created two folders layout and layout-land with two xml files, one for portrait and the other for landscape. Both of the xmls work but here is the problem.
My first screen is a login screen and my second screen is a main screen. If I login in portrait and then turn my phone landscape at the main screen. The layout will landscape turn but it uses the portrait xml for the main screen.
The same error occurs if I start in landscape and try to move to portrait later on.
It seems like whatever layout I do for my main then that's the layout that will be used for the rest of the app. Is there anyway to go around this?
Also. I'm already using android:configChanges="orientation" in my manifest for the activities.
If you are using android:configChanges="orientation", then you can override onConfigurationChanged to inflate the new layout after a configuration change.
#Override
protected void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(...);
}
Make sure you have a good reason for preventing the Activity from being recreated on an orientation change... and most importantly don't just do it because orientation changes are crashing your app. 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 when you must avoid restarts due to a configuration change and is not recommended for most applications.
Using android:configChanges="orientation" means you will be handling the orientation change in code. If you want it to automatically switch layouts, you shouldn't have that there.
Make sure both xml files present in two different folders have same name.
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
I have an activity group say mainActivity which contain 4 activity groups. say one, two, three and four.
All activities except fourth one has orientation portrait. The fourth activity group's orientation varies it may be portrait or landscape.
when i change fourth once orientation from portrait to landscape whole application restarts.
Is it possible to do such kind of things. that mainActivity group has orientation portrait and its child orientation is landscape. And also to allow to change child's orientation without restarting application.
thanks
You could prevent the activity from restarting by adding android:configChanges="orientation" to your activity in AndroidManifest file. But it's generally not advisable to do that as stated in the documentation:
Using this attribute should be avoided
and used only as a last-resort. Please
read Handling Runtime Changes for more
information about how to properly
handle a restart due to a
configuration change.
Source: http://developer.android.com/guide/topics/manifest/activity-element.html#config