Android XML layout declaration (portrait, landscape modes) - android

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

Related

Difference between activity_main.xml and activity_main.xml(land)? How to change them at run time?

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

Get id of landscape layout

[EDIT]I will try to simplify my question:
Is it possible to get a reference to a layout in res/layout-land/ when there is the same named layout in any other res/layout.../ folder?
I have a layout /res/layout/main.xml and I have a layout /res/layout-land/main.xml.
In certain cases I would like to set the layout manually to the landscape layout.
How can I access the landscape layout via R.layout, so that I could set the landscape layout manually:
setContentView(R.layout.<<main(landscape)>>);
without renaming it to a distinct name.
For the downvoter, I tried the solution from here force layout but this does not help with old Nexus 7 which gets recognized as phone in some cases.
[EDIT] This has nothing to do with my question, but for all the down voters who don't understand my question, currently I use this code:
if(getResources().getBoolean(R.bool.is_phone)){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
else{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
setContentView(R.layout.activity_fullscreen);
but I would like to use code like this:
if(getResources().getBoolean(R.bool.is_phone)){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_fullscreen);
}
else{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.activity_fullscreen_land);
}
(No it's not an error here with the layout name having a _land suffix, I want to choose the tablet layout myself not let Android decide which layout to choose for tablets).
My question was, can I reference the layout res/layout-land/activity_fullscreen, without renaming it. OK, I give up.
You set same name to both protrait and landscape layout.
Android recognizes in which mode it is and based on that it selects
the required layout.
If you put main.xml in both layout/res/layout-land/main.xml and layout/res/main.xml you can set your layout as setContentView(R.layout.R.layout.main);, android will recognize devices mode and select the correct one
If you want to set it manually to landscape mode:
You can do it from AndroidManifest by putting android:orientation="landscape" to activities you want to show in landscape mode
To set it for different tablet sizes you can create also different folders based on that:
If tablet layout requires at least 600dp of screen width, you should place it in layout-sw600dp/
Check this link for more information

Button self "rearranging"

Wondering how I do build a layout with self rearranging button, when the user turns his device from portrait to landscape, using relativelayout ?
I do not want to use scrolling on my layout and I have a lot of buttons
You can provide two different main.xml (or any layout xml file) Place one in res/layout/ and one in res/layout-land/ When the device switches orientation the system will handle the "magic" of switching to the appropriate file.
Here is some documentation about supporting these type of behaviors...
If I'm understanding your question correctly what you are looking to do is have one layout displayed for portrait mode and another for landscape mode. This is actually fairly easy to accomplish.
Here is a link to a quick tutorial:
How to change your layout in Android landscape / portrait mode

how to load different xml file in orientation change in android

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.

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