android layout-land and onConfigurationChanged - android

I am a starter on Android and trying to figure out how do use different layout files (main.xml) for portrait and landscape screens.
I tried to put main.xml in both res/layout and res/layout-land, then rebuild the project. However, this doesn't work for me.
I then added onConfigurationChanged method in the code and setContextView to R.layout.main. This time I got different screen layout in different orientation, but all functionality were lost. Some posting also said using onConfigurationChanged method is not really the best idea.
So can anyone help me with this issue?
Thanks,

Go to your AndroidManifest.xml and add this line to your activity tag:
android:configChanges="orientation"
This tells Android that you wish to control the state of your activity when the orientation changes. Normally, when there's an orientation change Android will actually destroy your activity and recreate it by mean of onCreate() again, so any data that was obtained will be lost unless the state is saved in the onConfigurationChanged() method.

Android will automatically use the layout that represents your devices current state. For your specific project, the layout-land xml file should be used when the device rotates. When you override the onConfigurationChanged method and modify your manifest, it tells Android you will handle the configuration change yourself which I do not recommend, especially if you want a new layout to be loaded.
Here's what you need to do (to test for the different layouts). Put an xml file called main in your layouts folder. For the layout, just use a LinearLayout that fills the parent with a blue background. Now create an xml layout file called main but put it under the layout-land folder. In this layout, use a red background.
Start the app, rotate the device, and you should see the background change colors. This is because the activity is being destroyed and restarted with the new layout.

Try to use onRetainNonConfigurationInstance() and getLastNonConfigurationInstance() methods to solve your problem.
Also you can use above sloution given by jrobinson3k1.
Thanx.

Related

how to fix orientation issues on android

I wanted to ask about android orientation;
I made 2 different folders for layout: layout and layout-land
when the orientation changes while the application is running i don't want to restart the running activity so i fixed that problem by adding the following line of the manifest file
android:configChanges="keyboardHidden|orientation|screenSize"/>
the problem then is that the app is not switching to the appropriate layout when i rotate the phone
does someone have any idea about the best way to handle the orientation changes of an android application ( i want to keep the state of the layout when i switch )
Simple: Let Android do the hard work.
Saying configChanges="orientation" overrides the default orientation behavior especially if you're using multiple layout files.
Change this:
android:configChanges="keyboardHidden|orientation|screenSize"/>
To this
android:configChanges="keyboardHidden|screenSize"/>

layout-land is not working?

I did some researches about this question but I couldn't find my answer. So, yes I did create a folder under res called "layout-land" and I put a separate main.xml in it. I intentionally deleted some images in the main.xml in the layout-land and I noticed that these images were still there when I rotated the android phone to horizontal.
Many questions similar to mine are caused by adding onConfigurationChanged to manifest. But I did NOT add anything to manifest as explained by this link
layout-land xml files does not work with onConfigurationChanged call back
First thing you must check is whether you already have in your Manifest.xml android:configChanges="orientation" that means that you wish to control the state of your activity when the orientation changes. When there's an orientation change Android will destroy your activity and recreate new, so any data will be lost.
So you should use onConfigurationChanged() method or onRetainNonConfigurationInstance() method.
I recommend to you read some tutorial.

How Do I Handle Layout Changes When Orientation Changes?

I am trying to simply alter the setlayout when I rotate my device so that I can have a layout of Views for a particular activity that is suited to the current orientation of the Android device but I am confused about the best way to achieve this.
I have referred to the following android doc:
Handling Runtime Changes
I do not need to save any data from my Activity so don't think I really need to use the onRetainNonConfigurationInstance() method. I tried handling the orientation change myself through the onConfigurationChanged() method, where I find the current orientation then set the layout as required but this results in views that no longer work. Is there something else I need to do in onConfigurationchanged()?
Thanks
To get a different view for landscape as opposed to portrait, you would place your layout XML file in both of the following resource folders:
/res/layout - Portrait
/res/layout-land - Landscape
This is, of course, if you have the same views within both, otherwise you may get some NullPointerExceptions.
You can create a new directory under res called "res\layout-land", create an .xml layout file in both "res\layout" and "res\layout-land" that have the same name. For example: "myLayout.xml". Android will automatically use the layout from the -land directory when in landsacpe orientation and the other when in portrait.
in your projects res folder you should have a layout folder. Create a new folder in the res and call it layout-land. Now create your second set of layout.xml files that are specific to landscape oriented devices. Save them in this layout-land folder. The system will handle the rest for you.
check out this page and scroll down to "Providing Alernative Resources" for more detail about different qualifiers you can use on your res folders.
Edit: What device are you using? I created a quick test project that is nothing but hello world but displays different text from a layout stored in res/layout-land folder.
I tried it once with and once without configChanges="orientation" in the manifest. When I run the app and switch orientations the layouts behave as expected. The layout from layout-land is displayed when device is landscape and layout from plain layout folder is shown when device is in portrait.
The device I tested on is Sidekick 4g. Download the test project and report back how it works on your device if you like.

Android Widget forcing RemoteView to clear cache?

I'm developing a widget where the portrait and landscape layouts are saved in /layout and /layout-land respectively. each layout file is named the same, but has been modified for formatting changes based on their orientation differences.
When I start a widget in one of the orientations and keep it there, it works fine...but the moment i switch orientations, the whole layout goes haywire.
Now, after searching the web I have found out that the Android system loads the latest RemoteView from cache. Aka, if i started the widget it in landscape, the landscape layout gets cached and used when changing oritentation.
I've tried everything I can think of to stop this from happening, but to no avail.
Does anyone know if it is possible to clear the Android RemoteView cache and FORCE it load the corrected layout??
Thanks
EDIT:
I have found a solution, albeit a bit hacky...
I created a "wrapper" layout that only has a LinearLayout in it with an ID, and then created sub-layouts where the landscape versions have a _land at the end of the name.
in the onUpdate method and the service that updates the widget, i check the orientation of the screen, then programatically select either the _land or non-_land version, and add it to the wrapper layout.
I can't believe Google didn't make this easier to do, and its incredibly frustrating that I have to force such programatic behaviour out of something that should seem natural to do using XML configurations.
You might need to use layout-land and layout-port as the directories (as opposed to layout and layout-land) to make it work correctly.

screen orientation

Hi
M new to android.I faced a problem i.e in landscape mode i need a layout view which doesnot have some feilds which are present in the portrait mode.I have created a layout for landscape view in such a way.In the activity i have given the conditions like if portrait display some feilds some hide.its working properly actually...first when i go from portrait to landscape it is giving what i want...but when i go to portrait mode its stopping the application.please help me.Thanks in advance
You can make two different layouts and put them in layout-port and layout-land respectively.
And make sure that you have mentioned android:configuration = "orientation" in your manifest for that activity. Try it if i have understood you correctly.
I think that the advice by Kantesh may be backwards. As explained in the docs, if you include android:configChanges="orientation" in the manifest, then the correct resource (from layout-port or layout-land) won't be automatically loaded. Instead, leave out mention of orientation from the manifest. Then, you do not need to worry about onConfigurationChanged() (don't override it). The system will automatically shut down the activity and then restart it, binding the appropriate version of configuration-dependent resources. Handling configuration changes yourself is (again, according to the docs) only a last resort to deal with performance issues that cannot be handled in other ways.

Categories

Resources