Select layout xml files most appropriate, depending on screen size - android

I would like to know if You Could select a layaout xml Differently Depending on the size of the phone screen.
I try to be more clear, I make more layout xml file, and I would like my app handles this statement
setContentView (R.layout.main)
first verifying on that size of the display is performed, and Therefore You Set the most appropriate layout xml

What you could actually do is the following:
Create a new folder for each desired configuration and name that folder with qualifiers.
If you want your application to have a different main.xml layout for different screen sizes you should make the following folders in your project.
/res/layout-small
/res/layout-normal
/res/layout-large
/res/layout-xlarge
In each of those folders have your different main.xml file layout (or whatever resource you want). Android will pick the one it matches with the device is running your application.
You can even add different resources for different configurations for drawables and even for language.
More info: http://developer.android.com/guide/topics/resources/providing-resources.html

Have you looked at this page yet?
You can query the size of the device screen, and then select which xml layout you want to use for that size screen...

Related

Single layout for multiple screen without different layout folders in android

I have a doubt. If i have one single xml file let's say res folder--> layout folder--> my.xml. i want to display this my.xml to all multiple screen size without creating layout-mdpi, layout-hdpi etc.., May i know what is the simple and best way to achieve this concept ??
Thanks in advance
Place the file at res\layout\my.xml
Every single device will get that layout by default unless you create an alternative.
If you have a single layout file in the default location (res/layout/my.xml), then it will be used on all the devices that your app can be installed on.
However, that's usually not what you want because it will look very different on a device with a different size than what you developed on. To handle this, you can start by creating another layout file in another directory (res/layout-sw600dp/my.xml). This layout would be used for devices where the smallest width (sw) is 600dp, ie, tablets.

android app development compatible to multiple screen sizes

consider a requirement of an app which need to be run on different screen sizes. lets say app has text fields ediboxes list view etc... and in each screen size the width/height of each ui component has to vary. how to desing an UI for this kind of requirement. please suggest.
Use Width and Height as fill_Parent or wrap_content..
if you are using images make different sizes images and put it in drawable-hdpi,drawable-ldpi,xhdpi,xxhdpi.The Sizes for those imagaes are in developer.android.com
In the res folder,there exists folder named layout,that the default folder which the system find the layout defination.But people also can make other folder like
layout-480x800
layout-1024x768
and so on ,and put the layout files in it.
When the app run on a device which have the certain size,the app will auto find the layout defination in the same folder,if cant it will use the default defination.

targeting different android devices

I know you can set specific layouts for a device like this:
if(android.os.Build.MODEL.equals("Galaxy S II")){
setContentView(R.layout.resultsGS2);
}
else if(android.os.Build.MODEL.equals("Nexus 7")){
setContentView(R.layout.resultsN7);
}
else{
setContentView(R.layout.results);
}
But what I want is to make a layout for all devices that have similar screen size/dimensions. I have been looking at this: http://developer.android.com/guide/topics/resources/providing-resources.html and I tried renaming the layout folder many different things and whenever I try to edit an xml file in the new layout folder I made and I change the device preview it keeps changing back to the default layout. I am using android studio, and basing the need to make a new layout off of the device previews that it shows. Is there is an easier way to do this besides making a layout for each specific device?
This can be done by having different layout folders and calling them layout- + screen size name and then edit the layouts inside these folders to match the screen size you are targeting.
So for example if you wanted to target something such as the galaxy nexus (320dpi) you would create a folder called layout-xhdpi and all the device specific layouts will be placed in here. And then if you were to run this in an emulator or a physical device you would see the layout that you made specifically for that screen size.

how to design one xml layout for all devices without creating folders or different sized images.

I want to design app in which i am using one xml layout,I want to use that layout for every device but i dont want to create new folders for small,large or extra large images in drawable.
how we can design one layout which is fit to all devices without creating images for ldpi,mdpi,hdpi?
Thanks in advance!
just put that layout in the 'layout' folder and your image in 'drawable' folder, one place no more :)
Android system will auto scale resource for you. But the UI will look bad.

Android creating calculator app

I am trying to create a calculator app and I've been having problems with the xml layout.
Due to the amount of button even though I use dp's to determine the size of each button from screen to screen the result varies...
Am i supposed to create the layout in java so I can get the screen dimensions?? if so can I have an example?
You can design the layout file and then make a few versions of it with different sized buttons. put them in res/layout-small/, res/layout-large/, and res/layout-normal/. The system will pull the proper xml file according to what size display the device has.
Another option is to try to design the layout with specifying a size in dp, instead use android:layout_weight and android:weightSum to achieve the proportions you are wanting. Then it should scale itself based on the device without needing any different xml files.

Categories

Resources