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.
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 have an activity with a videoview and a button, I have two different layout, one for portrait mode and second for landscape.
So, I create layout-land for second layout, and all works fine.
The problem is when rotation occours. Videoview restart becouse Activity renew.
Setting in manifest orientation|screensize video not restart, but I lose landscape layout.
How to do both tasks ?
Keep the configChanges in your AndroidManifest.xml, but also make use of onSaveInstanceState and onRestoreInstanceState to put the activity back in to the state you want it to be in after the orientation has changed.
Look here for more details
I have an activity with two layout files, one each for portrait and landscape modes. I see that the onCreate method is called each time I change orientation. I tried using
android:configChanges = "orientation"
in the manifest file, but it only reads the portrait layout. Is there a way I can set the layout to the landscape mode without calling onCreate?
If you use android:configChanges = "orientation", you must override the onConfigurationChanged(...) method of your Activity. Check the newConfig parameter for the new orientation then manually set the layout you want.
See the following links:
onConfigurationChanged(Configuration newConfig)
Handling the Configuration Change Yourself
You could create two layout folders. One for portrait mode and one for landscape.
layout and layout-land.
This is a fact of life with android. You need to develop your activity around the fact it can be destroyed at any minute due to a configuration change like the screen's orientation changing.
Save all the the information used to build the screen in on pause and recreate it in onCreate.
If you want your activity be only in landscape mode, you set this attribute for the activity in the AndroidManifest.xml:
android:screenOrientation="landscape"
You can put your layout.xml files in such folders for different screen orientations:
res\layout-land
res\layout-port
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 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