How Do I Handle Layout Changes When Orientation Changes? - android

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.

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

How to set different layouts for different orientations in a fragment

I have an activity which contain 3 fragments. 2 of them have data in a listview therefore when user rotate the screen everything is ok. but on my third fragment which contain buttons, when the user rotate the screen to landscape orientation,everything goes astray!
my third fragment in protrait orientation
my third fragment in landscape!
I have even tried to use scrollview but it's not working because when in landscape i don't want that big space at the middle. i wan't 3 x 2 buttons arrangment(i have 6 buttons.) when in landscape. and i think i can only achieve that by using 2 xml layouts for my fragment.
Question is how can i do that?
Make another layout folder layout-land under res and create landscape orientation layout file under this folder
After great help from sai Phani(see his comments above). i finally achieved my objective which was to create a landscape layout for my fragment by doing the following.
Create a folder under src\main\res and call it layout-land
copy your fragment layout xml (e.g peoplefragment.xml) which is in src\main\res\layout and paste it into src\main\res\layout-land folder. Don't change the file name!
Once you have pasted it, you can change the views alignment the way you would like them t appear on landscape. for my sutuation, i wanted them to appear like this in landscape
You may design your layouts that looks good for both portrait and landscape but you may customize for both orientation.
Look at SO link on the same issue # Android: alternate layout xml for landscape mode. The idea is simply to create folder names the correct way specified in Google documentation.
A good Google webpage is # Supporting Multiple Screens, search for "Using configuration qualifiers". There is a table stating Orientation and the folder names to use.

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

How does the ADT select layout XMLs depending on device size and orientation?

I have a complex project with many layouts (approx 40) and to reduce duplication, I am using a lot of includes.
I have an activity layout in layout-xlarge-land which includes another layout (layout B) which itself is just a set of more includes in a LinearLayout.
My emulator is set to XLARGE and landscape and sure enough, it picks up the activity layout. I've hard coded the activity title in the layout so I can confirm which one is being used.
The problem is that if I put layout B in layout-xlarge-land, the ADT will not preview my layout and gives a "cannot render" error. if I move layout B to res/layout (i.e. with no qualifiers) it works.
My understand is that Android will look for a layout in the qualified folder first then, if not found, use the one in the unqualified folder.
It's a problem because layout B should be different for the various resolutions and orientations.
Am I misunderstanding something or is this a quirk of the ADT/Android? If a quirk, any workarounds?
res
layout
layout-xlarge-land
activity_layout.xml <---- correctly loaded
layoutb.xml <---- "cannot render"
res
layout
layoutb.xml <---- renders OK
layout-xlarge-land
activity_layout.xml <---- correctly loaded
[EDIT] I'm using IDEA but don't see any relevance to the IDE.
[EDIT]
From the IDEA log
s.android.uipreview.RenderUtil - InflateException: You must specifiy a valid layout reference. The layout ID #layout/data_panel_all_views is not valid.
This confirms that ADT is looking in res/layout.
Taking a look at How Android Finds the Best-matching Resource, it tells that you can provide multiple qualifiers and how the best layout is chosen, so in the case you are sorting qualifiers well (which looks like you're doing, as the activity_layout is correctly loaded), try applying less qualifiers and seeing if those includes are working. Otherwise, you could think about the dirtiest solution: creating different layoutb's for each resolution and storing them in the same folder. That would mean each activity_layout would load it's correct file.
Good luck!
This was weird. One of the things I did was to restart IDEA which didn't resolve anything.
I've just rebooted my PC to install some updates and since starting IDEA, it's working as expected.
I've seen some issues with layouts which were fixed by restarting Eclipse. The common element is ADT so I suspect some flakiness in there.

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.

Categories

Resources