android switch layout on rotate - android

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

Related

How to use layout and layout(land) xml files correctly?

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.

activity restart when view change to landscape for custom view

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.

Drawable Landscape isn't loaded on Orientation Change

I want to prevent my activity from restarting when I change the orientation from portrait to landscape, so I call this on the corresponding activity on manifest:
android:configChanges="orientation|screenSize"
My activity isn't restarted anymore but unfortunately the drawables and layout I have for the activity on landscape orientation isn't loaded.
When I removed the line above, the drawables and layout are loaded correctly but then the activity restarted.
is there any way to prevent the activity from restarting while the landscape resources are still loaded correctly?
The best solution is save the state before rotation happens, let the system reload normally your activity and then repopulate it with the previously saved state. You can do that overriding onSaveInstanceState(Bundle savedInstanceState) method.
But if you want to manage the orientation change by yourself, you can read this similar question: How to refresh an Android RelativeLayout when orientation changes without restarting the Activity?

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

Orientation issue with landscape mode

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.

Categories

Resources