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.
Related
orientation change is not working when I rotate a child activity to landscape. I added android:configChanges="orientation|keyboardHidden in all activity tags in my manifest.xml file.
I found that my app doesn't take layoyt file from layout_land folder. what is the solution for this problem.
By putting that in your manifest you are saying that you will manually handle all orientation changes. You'll have to setContentView again onConfigurationChanged OR remove it from your manifest and it will load the right layout.
Edit: found this answer afterwards which explains it
I have created two layout files (in res/layout and res/layout-land) for my application. But it is not displaying the proper layout file when i change the device orientation. It just displays the same layout file (res/layout) in both the modes. Why it is not picking up the landscpe mode layout file in landscape mode? Also i have added android:configChanges="keyboard|orientation" to my activity in manifest. What else i have to do? I'am a newbie to android app development.
If you are adding added android:configChanges="keyboard|orientation" in Manifest file it won't change the layout, because by adding this attribute your Activity is not re-created again so it displays the same layout as in portrait mode. So, remove android:configChanges="keyboard|orientation" from Manifest file to get it working.
If you want that your Activity should not be created again and also it should change the layout when your orientation changes you have to add code for changing the layout manually in onConfigurationChanged() check my answer here for getting it working.
You should not add this.
android:configChanges="keyboard|orientation"
in your manifest.. because it is not allowing the call for onConfigurationchanged method... which is responsible for handling the orientation changes... remove that line from you manifest and it should work...
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.
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.
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.