Change layout without calling onCreate - android

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

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.

setRequestedOrientation for landscape loads portrait Layout

I am developing an app that needs to run strictly in portrait mode in smart phones and strictly in landscape mode in tablets. I am using the following code to set the orientation manually in onCreate method of my login activity:
if (getResources().getBoolean(R.bool.portrait_only)) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}
Where the bool value comes false in Tablets. However the orientation is changed to landscape; still the layout file is picked from "layout_large" folder instead of "layout_large_land" folder. Hence my view looks stretched.
I have tried this link.
But it doesn't help.!
Any help is appreciated.
Thanks in Advance.
I had the same problem. Make sure you're NOT setting android:configChanges="orientation" for your activity in AndroidManifest.
I'm guessing the problem with that is that setRequestedOrientation potentially triggers an orientation config change, HOWEVER by using android:configChanges="orientation" this config change is not registered by the system.
Alternatively, if you want to keep the Manifest setting, you would have to implement your own recreation mechanic for this particular configuration change.
It may look something like this:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(getResources().getBoolean(R.bool.portrait_only))
recreate();
}
I have used a folder like this for Tab & mobile
layout folder is for mobile and layout-sw720dp for Tabs. 720 dp is width which tells 720+ dp will use that folder for layout.
Being late in this thread but will help in the future SO. In my case I was programmatically changing the orientation of the screen by calling
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
OR
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
depending on the user's action. I have also created a separate layout for portrait and landscape but every time on changing the screen from portrait to landscape, it always loading the portrait screen in landscape.
In AndroidManifest.xml, I've set this
android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"
So, To solve this problem, Need to remove the orientation from android:configChanges then it works. This is what android documentation Link mentioned
The "orientation" value prevents restarts when the screen orientation
changes.

Landscape and portrait android app

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.

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

handeling orientation change in android

I wanted to have a differnet look to one of my activities when the orientation changes.
I created the layout-land folder and added activity1.xml there but when I switch the orientation I still get the regular activity.
Is there anything else I need to do to make it work?
Thanks.
Do you have android:configChanges attribute defined in your manifest?
There are two ways the orientation change can be handled:
You define attribute android:configChanges="orientation" in your app's mainfest and implement onConfigurationChanged() in your activity. This method will then be called when orientation changes.
You do NOT define android:configChanges attribute. The you activity will be restarted (a new activity crated) and will go through a lifecycle process (onCreate, onStart,
etc), every time orientation of device changes.
Read here about configuration changes. You can override onConfgiratationChanged() method of your activity and set a content view to the activity1 layout.

Categories

Resources