Get id of landscape layout - android

[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

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.

Android XML layout declaration (portrait, landscape modes)

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

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 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.

Categories

Resources