I am in the process of creating layouts for a new application and I am interested to know how you are usually approach this process.
Obviously it's a very general question, but I think we can get a lot of ideas from different developers' perspectives. As creating perfect layouts in Android, in my opinion, is one of the most difficult tasks in the app development process, I think the answers can be of value.
The assumption is that we want to support all device sizes with the least number of XML files, without compromising the application design quality.
In my case the designer of the app designed it with Galaxy S3 dimensions in mind (720 x 1280px).
Considering the 3:4:6:8 ratio -
What will be the base device you'll do the design for, so one layout file will look good on all normal size devices?
How would you handle the difference in device dimensions, thinking in dp -
Eg.
480 X 800 hdpi = 320 X 533dp
320 X 480 mdpi = 320 X 480dp
720 x 1280 xhdpi = 360 X 640dp
How will you still use the extra dp, so there won't be to many empty spaces, without making the layout not appropriate for 320 X 480 device?
When the designer marks a margin of 20px, how would you interpret it in the xml?
Taking into consideration he designed it on a 720 x 1280px canvas.
Start with some screen size as your base target device for e.g. 4.7 inch (hdpi) and create a single layout for default screens in layout folder and declare dimensions in dimens.xml in values folder.
Next, let's say you want your layouts for a 7" device, create values-sw320dp folder for 7" in portrait orientation and declare separate dimensions in dimens.xml in this folder.
Next, let's say you want your layouts for a 10" device, create values-sw720dp folder for 10" in portrait orientation and declare separate dimensions in dimens.xml in this folder.
PS: For landscape orientation, add -land in the end of folder names.
Next, let's say you have new devices such as xhdpi and xxhdpi devices, you can create values-xhdpi and values-xxhdpi folders.
Since you have defined dimensions in values folder, when you run the App, most of the dimensions will be used from here only. And you can add/update the dimensions in respective folders for screen-size and resolutions on which there is any mismatch.
Also, if you have got separate drawables for various size like mdpi, hdpi, etc you can place them under drawable-mdpi, drawable-hdpi, etc.
The Android system will take care of loading correct resources at run-time.
Note: On Android versions < 3.2, the folder naming was little different, you can refer that here.
Hope this helps.
Android automatically scales layouts to fit the current device. One way to make the solution more elegant would be to create separate folders drawable-ldpi, drawable-mdpi, drawable-hdpi, drawable-xhdpi and drawable-xxhdpi to hold the drawable resources - this is to make sure the same resource does not get used across all the different resolutions where it might not be so clear.
Do not use px (pixels) for measurements in the layout file - use dip (or density independent pixels) which allows to scale according to device pixel density.
The other extreme would be to have different layouts for different resolutions though this is is not advisable.
Related
I am creating an android app and I want to be able to support as many android devices as possible that use Android 4.4 and above.
The screen sizes that I want to support are:
Normal
Large
xLarge
I have created different dimens.xml files for each size.
My first question is, the numbers that I put in each dimens.xml file should be based on the minimum dimension of each category?
For example: for normal I should design the layout in a device with dimension 470dp x 320dp, for large a device with dimensions 640dp x 480dp and for xlarge a device with dimensions 960dp x 720dp ?
My second questions is, let's say that we have our base dimension files for normal, large and xlarge, how many more dimension xml files should I create in order to support different densities too. Also do devices with Android 4.4 and above support all densities or are some excluded? (for example LDPI)
Now let's say that I exclude the LDPI density, should I make each density for each size or if I make one MDPI for each screen size and one HDPI for each screen size everything will "look" the way I want them, even in devices with XXXHDPI density?
One last thing, if you can recommend any devices to make my "base" layout that would be great.
hi you can handle size of different resolutions by this,
create different types of values folder.Create these folder inside res.
values-hdpi
values-xhdpi
values-xxhdpi
values-xxxhdpi
and dimens.xml create accordingly for different resolutions.Use all padding and margin from here.
This is way to handle different resolutions for different sizes.
for more info see the below .
for more details take a look from my project base structure
Thanks
I am having trouble managing my application layouts in three different resolutions; 720x1280, 1080x1920 and 1440x2560.
In drawable-xhdpi folder is the corresponding images to 720x1280 resolution.
The folder drawable-xxhdpi is the corresponding images to 1080x1920 resolution and in drawable-xxxhdpi to 1440x2560.
I began to adjust the screens in layout folders. The layout-sw360dp was setting screens for 720x1280 and the layout-sw480dp the 1080x1920.
When testing in the emulator 720x1280 all settings worked perfectly.
But to test the emulator 1080x1920, oddly taking this information in layout-sw360dp folder and not the layout-sw480dp.
In the case of adjusting each folder layout-sw360dp and layout-sw480dp, I'm using margin with values in 'dp' and emulator higher values (layout-sw480dp) are being dropped are being used and the values of the layout-sw360dp.
How can I manage three screen sizes correctly?
Well designed Android applications cater for varying screen sizes, the screen size being a function of both resolution and density. Using several layout-sw###dp folders allows you to vary the layout according to the width of the display, e.g. showing fewer elements and controls on a small screen and perhaps more detail on a large one.
The 'sw' in the layout folder name is the 'shortest width' a display must have in device independent pixels (dip). One dip = 1 real pixel on a 160 density screen. So on a 320 density screen, 2 real pixels make up one dip.
Your nexus 5 has 480/160 = 3 real pixels per dip. So with a width resolution of 1080, that is 360dip wide.
Your nexus 4 has 320/160 = 2 real pixels per dip. So with a width resolution of 768, that is 384dip wide.
Neither device is more than 480 dip wide so both use the sw360dp folder.
Both devices are physically very similar in size. The Nexus 5 (5.4inch screen) has more pixels than the Nexus 4 (4.7inch screen) but the pixels are physically smaller. So it is correct that the same layout is used for both. The UI should look the same on both devices, assuming you correctly specify the size and layout of your various UI elements in dip.
As a further example, I have an old tablet (10inch screen) with a resolution of 800x1280 and a low density of 149, hence is 859dip wide. You can comfortably display far more info on a screen that size than on a Nexus 4/5, hence you might consider creating a layout-sw720dp for that.
So you appear to be doing exactly the right thing already by designing different layouts for different screen sizes. Just remember that resolution is not the same thing as screen size. Screen size is a combination of resolution and density.
As for your drawables, you are also already doing the right thing by using drawable-xhdpi, drawable-xxhdpi etc with appropriate resolution images in each one. So for example a small device with an extremely high density would likely use the 1440x2560 images and the sw360dp layout. My low res tablet would use the 720x1280 images, unless you'd put something in drawable-mdpi which is where it would look first.
So firstly you'd create appropriate resolution images in the drawable folders so that they would look as good as possible on different resolution screens. Then create appropriate swxxxdp for your layouts so they take up the appropriate space depending on the physical screen size, i.e. make good use of available screen space on large devices and don't clutter up small ones. It's likely you would want to go further and create -land and -port versions of each as well.
It is worth noting that even if you only have one layout folder and one drawable folder, your application will still work on all devices. Android simply looks for the best choice and if there is only one, it'll use that. Adding in the various folders simply allows you to make your app look as good as possible on a range of devices.
Everything I have discussed here and more is explained in detail at http://developer.android.com/guide/practices/screens_support.html.
I made my first Android App. I read a lot about the images and made separate sizes for mdpi hdpi xhdpi and xxhpi.
Although i noticed i'm still having some issues on smaller screens 3.7" and 4.0" inches. What i noticed is that i'm using fixed values for things like buttons TextViews and Text sizes.
I came across and read about the dimens files but i'm still a little confused as to how should the folder structure should of the values.
To be more specific
drawable
drawable-hdpi
drawable-mdpi
drawable-xhdpi
drawable-xxhdpi
How is the values folder structure should be ?
I've read somewhere that it should look like values-sw600dp.
But what does the 600dp represents ?
600dpi and up ??
Can i use custom values there or are there some fixed ones??
Thank you
after version 3.2 sw600dp is used for multi-layout panes for devices whose dp is greater than 600, where dp is :
px = dp * (dpi / 160)
This is irrelevant to your requirement (unless you want to make apps compatible for tablets).
Just make sure you put proportional image sizes in xhdpi,xxhdpi,mdpi and ldpi folders and use wrap_content for images. You can also use constant sizes, just make sure that you use dp for buttons and sp for text size.
I have 3 custom made android devices.
First one has a 5 inch screen with 1280x800 res
Second has a 5 inch screen with 800x480 res
Third has 7 inch screen with 800x480 res
I tried giving sizes with dp, px and inches but it seems they cant support those screens properly with the same value (even inches seems to be not exact inches but translation to px eventually).
How can i use same code to properly adjust view sizes relatively to the screen size?
Well, as first, the correct measure unit to use is: dp for controls and sp for fonts.
These are normally for margin, paddings, widths, heights, and other attributes.
Also xml drawables can take advantage of pixel independency
Define your dimensions in a res/value/dimens.xml file (this is a PROPOSED standard name, you can call it whatever you like best), in order to have them referrable from all your code, instead of being hardcoded and often repeated in many files.
Then you must know that you should provide your graphics AT LEAST in mdpi resolution (160 dpi), which will be scaled up or down to match other resolutions.
Notice that I said at least.
For every resolution you are supporting, you should add a folder in your res path containing the graphics at the corresponding density for that resolution.
this means that you will have, let's say 3 resolutions mdpi, hdpi and xhdpi (today's favourites, excluding tablets - these ones deserve some folders on their own):
The graphics is going into:
res/drawable-mdpi
res/drawable-hdpi
res/drawable-xhdpi
Just put your graphics (with the same names) with (respectively) a dpi density of 160, 240, 320 in those directories.
Now your graphics is resolution compliant.
Now, I don't realize what the problem really is.
I mean, is it the background not fitting well? then the solution is to use a tiling or an "abstract enough" stretchable picture.Or you could use 9 patches, as well
If the problem is how the fonts and other objects interact with each other, you should always reference to an mdpi device (even emulated, if you don't own a physical one). When things scale well on a mdpi device, they are supposed to scale well on every device.
For tablets in particular, you are supposed to provide specific folders for values (where you put your dimens.xml file, containing the dimensions).
These folders normally have names like values-sw600dp or values-sw720dp-land. The suffix land indicates landscape mode, the particle sw###dp indicates the minimum dimension (width or height), so, I guess that in you case you could prepare some folders called values-sw480dp and values-sw480dp-land and there you would put your dimens.xml file, with the special dimensions for that particular device.
I guess that providing only the non-land folder would be enough.
I'm testing one of my layouts using the different devices defined in the graphical layout editor in Eclipse. Due to some of the devices' varying resolutions I had to define different dimens.xml files to define specific font sizes so the design would look correct.
I have a dimens.xml file in each of the following resource folders:
values
values-sw1280dp
values-sw640dp
values-sw800dp
values-sw800dp-large
For whatever reason the device 7in WSVGA (Tablet) uses the dimens.xml in the values folder. But according to the device configuration the resolution for this device is 1024 x 600, so shouldn't it be using the dimens.xml defined in values-sw800dp-large?
Anyone have any idea?
It won't use values-sw800dp-large because that qualifier means that the smallest dimension must be at least 800dp. Also note that these are dp values, not pixels. So if the device is hdpi, 800dp is 1200 pixels.
If you're doing this in an emulator, there is a weird interaction with device display density and scaling of the emulator display. So, for instance, if your device is supposed to be hdpi, it may actually be treated as an mdpi display unless you specify a scaling.