can anybody tell how to load different xml file whenever user changes device orientation in android?
For example, I am using a profile screen in my application. In portrait mode, user gives 'user name' value, after that I change orientation to landscape, now I have to load different xml, so that user gives value in landsacpe mode.
Is it possible?
Thanks
Editor's Note: English doesn't seem to be his first language.
create two types of layout directories to handle orientation . layout-land layout-port
put the xml with the same name in both the directory. if i have main.xml file then i have to put it in both directory.
Thanks
Provide a different layout in layout-land\
See http://developer.android.com/guide/topics/resources/providing-resources.html
yes it is possible. If the user has changed the orientation then you can set different XML on setContentView(R.id.xml)
You need to implement OnConfigChange Method so when ever orientation change at that time this method will call autmatically and setContentView(R..id.nameofxml) from layout-land folder.
Related
I have two files activity_main.xml and activity_main.xml(land).
If my phone is in portrait mode, I want two run activity_main.xml.
If my phone is in landscape mode, I want to run activity_main.xml(land).
What should I do ?
Difference is activity_main.xml(land) is for landscape mode. If everything is there make sure your screen rotation is turned on!
Or else if you need to know how to do that,Create a new directory layout-land, then create xml file with same name in layout-land as it was layout directory and align there your content for Landscape mode.
Note that id of content in both xml is same.
Or you can do that in this way,
Now the job is done!
But you have a single Activity and two views depend on the orientation. View A might not have something in view B(if you have the same set of views in both xmls then you don't need this). If you don't have same set you need to initialize your views in the correct way!
When you initialize your views you can do this by:
For Lanscape
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
//init views in landscape
}
For Portrait
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
//init views in portrait
}
delete the file activity_main.xml(land) and refer the activity_main.xml to the same java file!
Android SDK provides different ways to manage resources of android application.
Device supports two orientation mode:
Portrait
If you want to add any specific layout for portrait mode only, You should name layout file as 'xyz-port.xml'.
Landscape
If you want to add any specific layout for portrait mode only, You should name layout file as 'xyz-land.xml'.
Android system will decide runtime which layout to choose.
For your query below,
I have two files activity_main.xml and activity_main.xml(land)
If you do not specify any like, activity_main.xml then, It will be used for both.
For more information check here.
Thanks.
please check the following link i hope you get the solution.
google developer
link 2
I want to show two extra textfields when my application is shown in landscape. I know I can do this by copying my original layout to folder layout-land and just add 2 fields, but that way I need to maintain 2 versions of my layout file, the original one and the one copied to folder layout-land. What I actually want is just add two textfields without the need for maintaining 2 copies of a layout file. Can you do that ?
thanks, Steef
The idea which I've thought is may or may not be correct. My idea is to check the device orientation in class file and when the layout is landscape, just manipulate your layout or simply mean just add two text field at that time. Hopefully! it will work.
Happy Coding!!!
Im beginner but i might have some idea. If you dont want to have 2 layouts file, there is second options- make styles.xml for portrait and land and you can write style which set visibility. Try something like that.
when we have two XML layout files for an activity one for portrait and one for landscape mode, is it necessary for their root views to have the same ID or they may have different?
IF you have separate layout files (i.e.: for different orientations), they can be completely different.
however it depends how you would like to use them in your code.
Update:
To check orientation in code use:
getResources().getConfiguration().orientation
It is either ORIENTATION_LANDSCAPE or ORIENTATION_PORTRAIT.
http://developer.android.com/reference/android/content/res/Configuration.html#orientation
If you do not need to use the view through a findViewById, then it won't be a problem to have different names. Generally, the layout changes depending on the orientation, but it contains the same views so you should ask yourself: Why do I want different name for my root view? If it's only to check the orientation, then you should not use this solution. See the answer on how to check orientation: Check orientation on Android phone
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 worked in a tabactivity, I set the gravity for tabs as bottom. If I change the screen from portrait to landscape, I can't see the tabs.
How can I do it? I need an example please.
Thanks.
Assuming you do not have a separate XML file dedicated to landscape format, you might need to create one and specify the landscape-layout look.
Create another main.xml file with the specifics of how your program should look in landscape mode. The file shoud reside inside your res/layout-land/ folder.
When that is all said and done, in potrait orientation, Android will use main.xml found in res/layout and in landscape main.xml found in res/layout-land.