Which XML files is to be used for different screens - android

I am working on android app and I have created different screens for different layouts.There are 3 types of Android XML files, Android Layout XML and Android values XML.Which one is to be select. Please suggest

Use only Layout XML for layout and for landscape create another folder layout-land inside res folder and create layout of same name with different design suitable in landscape mode. And use different layouts with same name and same Id of all the child view.then android automatically pick when orientation changes
use this for reference
http://developer.android.com/guide/practices/screens_support.html#range

Related

Android portrait and landscape layouts

I have the following layout:
I need the following layout in portrait mode:
But instead of that I obtain the following one that I don't know how to modify it:
How can I do that?
Fastest way for Android Studio 3.x.x
1.Go to the design tab of the activity layout
2.At the top you should press on the orientation for preview button, there is a option to create a landscape layout (check image), a new folder will be created as your xml layout file for that particular orientation
Create a layout-land folder in res folder and create xml with the same name in this folder.
You could create a seperate Layout for Landscape mode.Just name it the name as your current layout but make sure to add it in the folder layout-land.
Right click on res - layout and then Add New and for the folder write layout-land. Name should be - as said before - the same as your current layout.
When now changing the orientation to landscape, Android will use this layout.
Just create layout-land folder near layout folder and create xml with the same name.
No need to create layout-land. Google has an open sourced FlexBox Layout that works adaptive on screen size /orientation changes like CSS.
Have a look at google's developer site for examples.

Android cannot find declaration of RelativeLayout

I am creating different layouts to support different screen sizes.So I created folders like layout-small,layout-large and so on.But I am stuck with the error cannot find the declaration of element RelativeLayout. How do I solve this?
#jobin , android till now does not allow to create seperate layout folders/packages , so please create all your layouts in the layout folder only, u can use prefix or postfix tags to differentiate between layouts.
for ex
activity_main_small ,
activity_main_big ,
activity_main_verybig
but do remember to keep all layout Xmls under the single layout folder provided .

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.

solving orientation issue in android

I have designed 2 xml files, one portrait and another for landscape for a single activity. I have called one xml file using setContentView(R.layout.portrait); in my onCreateclass. How should I call xml file for landscape.
You should have the same file name for both layout resource files. The only difference is that the landscape layout folder will be called "layout-land" and inside you'll have the layout xml file in landscape mode. When the app is running, Android knows to take the correct xml file and set the layout according to the current mode so you don't have to worry yourself about selecting the proper xml layout file.

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