Android layouts - one layout for all screen sizes? - android

We have a confusion about the best practices of Android layout design. Up to now we designed layouts either for each screen sizes (hdpi, XHDPI, xxhdpi.... ) or for width/height concept (sw, sh etc folder structure). Any away we had multiple layout to support multiple screen sizes. Sometimes most of they are just a copy of hdpi as Android managed to scale things properly.
Now we hired a senior Android developer with 5. 5 Years of experience and he prefers to write only one layout to all screen sizes. He argues that this is the latest way. He only write one layout and when it comes to images he add them to different drawable folders (hdpi drawable folder etc) with different sizes to match across all screens
At the moment I am also confused thinking whether I really missed an industry standard, because this new guy's past UIs also seems pretty good.
Any advice on this please?

Up to now we designed layouts either for each screen sizes (hdpi, XHDPI, xxhdpi.... )
Those are not sizes. Those are densities.
or for width/height concept (sw, sh etc folder structure)
Those are sizes.
He only write one layout and when it comes to images he add them to drawable folders (hdpi drawable folder etc) with different sizes to match across all screens... whether I really missed an industry standard
Having layouts based upon densities has been a code smell since late 2009, when the concept of screen densities was introduced. Having drawables based upon densities has been a best practice over the same timeframe.
In terms of the "one layout" portion, that may not be practical in all cases:
Different orientations (portrait vs. landscape) may require different layouts (e.g., to get forms to fit without scrolling)
Substantially different screen sizes (e.g., phones versus tablets) may require different layouts
Substantially different UI approaches (e.g., watches vs. handheld mobile devices vs. TVs) usually require different layouts

you should fix your height and width of the view in dimension file for various resolution example:
values folder:
90dp
75dp
values-hdpi folder:
<dimen name="width">100dp</dimen>
<dimen name="height">85dp</dimen>
like this do for mdpi,,xhdpi,xxhdpi and put this in your view.
android:layout_width="#dimen/width"
android:layout_height="#dimen/height"

Related

What is difference between layout-small and layout-sw320dp

I know how to use the following folders but for example I do not know what is difference between layout-small and layout-sw320dp?
Also, Suggest for me that which the folders are important to optimization.I do not like my program be seen for users as irregular.I want a final answer from you.
Based on which the folders I design my layouts? Because I have not multiple android devices to test the layout in different screen size.
I know how to use the folders and I have not problem about it.but my question is which the folders for continuous use and for all devices?
layout-sw320dp
layout-sw480dp
layout-sw600dp
layout-sw720dp
layout-small,
layout-large etc...
Both are qualifiers to define layouts for specific screen sizes.
Before Honeycomb - 3.0 (I guess), the screen's diagonal size was measured based in 4 groups: small, normal, large, and extra-large.
After that layout resources are defined for screen sizes with the smallest width (sw stands for smallest width).
Resuming, layout-small is deprecated and layout-swXXXdp qualifier should be used instead.
You can get more info about these qualifiers in Supporting Multiple Screen's Android official doc page.
The difference is that the qualifiers -small, -normal, -large had been marked as deprecated in API 13. Now the recommended way to go is using the -swdp folder structure. (see http://developer.android.com/guide/practices/screens_support.html#support )
The number of resources folders in your project depends basically on the type of devices you're targeting at (small phones, phablets, tablets, ...) and the number of different layouts you want to maintain. In most cases implementing a layout for landscape/portrait modes for phones and for tablets should be enough. Though you can create another layout for a specific screen size if you really need it.
Hope this helps.

Supporting multiple screen size - Android

I am going to develop new application in Android. This application should only work in portrait (even for tablet). Also the UI and layout design should be similar on phones and tablet. We can't change the layout design for tablet as it has huge area to use. We have to stretch all the images to match phones. We can use nine patch. But I am little bit confused of using images in multiple drawables.
As per my analysis (may be wrong.. : ) ) the screens are divided into density and sizes. We can use the scaling ratio of 3:4:6:8. But this ratio is based on the density. But in my case I have to stretch the entire UI to fill the Tablet screen.
So what are the drawables that can be used for a app like this which can support multiple devices. And what are the screen sizes for which we have to design.
And this application needs nearly 100 layouts. So I am planning to maintain single layout and designing the layout using weight for each layout instead of using dimension.
Also if I used multiple APKs to support different screen size what are the drawables used to support
1. Small and Normal
2. Large
3. Xlarge
I just did something very similar. To stretch the app without creating new layouts I used dimensions set in XML
res/values/dimensions.xml
res/values-sw600dp/dimensions.xml -> 7+ inches
res/values-sw720dp/dimensions.xml -> 10+ inches
Dimensions are resources files:
<dimen name="default_padding">11dp</dimen>
You can increase the dimensions by about 30% in the 600 and 720 file.
Then simply used #dimen/default_padding in your layout and it will be scaled
Regarding images, either you make sure you have all your assets in all densities, or you set fixed size to you ImageView's and appropriate scaleType
Firstly, you do NOT want to create multiple APKs to support multiple screen densities. Android provides all of the framework you need to support everything within one build, you just need to create the right resource hierarchy drawables with your desired densities.
So what exactly do you need... based on your question the following:
portrait mode: you can specify this in each Activity declared in your AndroidManifest file using the following:
<activity android:name=".MainActivity"
android:label="#string/app_name"
android:configChanges="orientation" >
...
</activity>
NOTE: Per the Android docs, if you're targeting API >= 13, and you use the android:configChanges attribute you should also use the android:screenSize attribute to help specify size changes.
dimension sizes for various screens: as touched upon, this can also be handled in resources. This is your best way to use one common layout file but configure the layout for use on numerous devices. See http://developer.android.com/guide/topics/resources/more-resources.html#Dimension for how to use dimensions if you're unfamiliar
drawables: it sounds like this is the crux of your question. As you mentioned, using nine-patches will help you reduce your app footprint and fill in extra space (see here and here for more on nine-patches). The sizes you should support and the densities needed for those sizes are discussed in great detail in Android design docs, so much detail I could not even do it justice rehashing it here. I've provided links below to as many places as I could remember that this is discussed.
Good luck!
Here are links to Android design docs that you will find useful (some of which have been mentioned):
http://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources
http://developer.android.com/training/multiscreen/index.html
http://developer.android.com/guide/practices/screens_support.html
http://developer.android.com/design/style/devices-displays.html
In addition to the pixel density specific folders, you can specify screen-size specific folders
drawable/
drawable-large/
drawable-xlarge/
drawable-hdpi/
drawable-large-hdpi/
drawable-xlarge-hdpi/
drawable-xhdpi/
drawable-large-xhdpi/
drawable-xlarge-xhdpi/
So you could design scale appropriate graphics for the various screen sizes and densities. Please note that a give screen size category (e.g. "large") will only give you a rough idea as to the actual device pixel dimensions of the device, but you'll get good guidelines for min/max dp ranges.
For example, you might have a 100x100 image you want to display on phones (screen size "normal"), you'd create image assets at 100x100, 150x150, 200x200 for drawable, drawable-hdpi, drawable-xhdpi folders respectively. But on 7" tablets, i.e. "large" screen size devices, you might display this same image at 200x200, so your "drawable-large" folder assets would be 200x200, 300x300, 400x400, and on 10" tablets, i.e. "xlarge" screens, you might display the same image at 300x300, 450x450, 600x600, so these go in "drawable-xlarge-*" folders.
All the details are here:
http://developer.android.com/guide/practices/screens_support.html
First you need all the possible screen layouts
drawable
drawable-ldpi
drawable-mdpi
drawable-hdpi
drawable-xhdpi
drawable-xxhdpi // phones like s4
drawable-xlarge
drawable-tvdpi // nexus 7 etc
drawable-xlarge-xhdpi //tablet like nexus 10
layout : for phone
layout-sw600dp
layout-sw720dp
then you need to put all use 9- patch images for buttons etc ... you can also make your custom drawable it would be easy and handy to work on ..Also you can take dpi for each screen by using switch and scale it the layout accordingly.
As, in one of my project I had used this technique for showing respective thing to each resolution device .
DisplayMetrics metrics = getResources().getDisplayMetrics();
int densityDpi = (int) metrics.density ;
switch (densityDpi) {
case (int) 1.5:
break;
case (int) 2: //1.75 will be 2 in INT.
break;
default:
break;
}
also keep all the values you are going to used for margin padding etc values-sw600dp for tvdpi tablet ,value-sw720dp for tablets
Last but not least keep all thing generic as much as you can and put it in drawable ..
I have seen some ppl who used background patterns of different dpi's and put it in respective drawable .. if there is such thing like pattern make your custom drawble and repeat it accordingly
that will save your time .. hope it may help you
In order to stretch all the images to match phones you can specify the image size using the sdp size unit. This size unit is relative to the screen size so it can fulfill your requirement.

Density-specific layouts vs. size-specific layouts in Android

For quite a long time, I have been declaring resource folders for 4 different densities:
drawable-ldpi
drawable-mdpi
drawable-hdpi
drawable-xhdpi
In the layout's XML, I have been using fixed widths (while still density-independent) such as 128dp for those graphics.
However, when more and more large-screen phones, and especially tablets, were introduced, that approach did not work anymore. Although you provide density-independent resources this way, the layout will not look good on large screens.
This is why I think I need to add Dimension resources that depend on the screen size, for use in the XML layouts, e.g.:
values
values-w600dp
values-w720dp
values-w1024dp
But does that mean that I should drop supporting those 4 density containers? Or do I need to provide 16 resource folders, i.e. one for every combination of density and size?
I can't find any good help in the Android documentation as to this topic.
drawables and layouts are different. To answer your question, should you stop support those densities. Yes, but you should still support xdpi and hdpi. Romain Guy recently said that modern devices like the Nexus 7 (at a tvpi) can scale the assets properly enough that mdpi isn't really needed. And nobody uses ldpi anymore. Last I looked is was less than 2% of the market.
About layouts. A Nexus 7 (1280x800 tvdpi) would use something from the values-w1024dp but still get assets from the drawable-hdpi folder. Those two aren't mutually exclusive. Something like a S3 would also pull from the values-w1024dp but use drawable-xdpi. You only need to provide an alternative layout if your use-case calls for it.
So do you need 16 different things? No. You do need xdpi & hdpi (if not mdpi). You may want to include alternative layouts for different sizes. You can be as specific as you want or as generic. Unless you're doing a hybrid app for both phone & tablet (7 & 10 in) you probably don't need a lot of xxxx-sizexxx folders.
In the layout's XML, I have been using fixed widths (while still density-independent) such as 128dp for those graphics.
This is probably a source of your issues. Your layouts should be as fluid as possible using wrap_content and match_parent. Fixed sizes should be reserved for padding around the sides and image where you know the size ahead of time. If you do this, your layout should look decent at any size from a small 320 x 200 to a GTV size.
1) Regarding dimensions in your layouts (values/dimens.xml):
values values-w600dp values-w720dp values-w1024dp
But does that mean that I should drop supporting those 4 density
containers? Or do I need to provide 16 resource folders, i.e. one for
every combination of density and size?
No, you don't need to provide different dimensions per dpi bucket (hdpi/xhdpi), because the dimensions are already being scaled up or down based on the device (if you're using dp instead of px). So for dimensions, you only need to provide values for devices with different sizes (hence the name, values-smallest possible width-600-dp). Because you don't want 16dp padding on a phone AND 16dp on a 10" tablet as well. You'd want 64dp instead. And no, it doesn't matter what density the device has. It still needs to have the same padding on the respective device width. So for dimens, you only need to think about the device's actual physical dimensions.
2) Regarding drawables scaling for different resolutions (drawables/xdpi):
The system scales them appropriately for the device. You don't need to worry about this. Also, you don't need to add any other buckets here. Just use mdpi/hdpi/xhdpi and maybe xxhdpi because many new devices are going to use the new density in the future.
Conclusion: there are 2 different UI building components that vary according to 2 different rules: drawables based on screen density and dimensions based on screen size. Do not mistake one for the other and think you need tens of buckets in the values folder, because that's not only wrong, it's just mind boggling.

images don't adapt to screen type

I am working on an Android game.
I finally got my app on two devices besides the emulator and realized that even though I have different sized images in the "drawable-hdpi", "...-mdpi", etc folders, they're always the same size. The same goes for my widgets (Buttons, ImageViews).
I was wondering if there's another way to adapt to the different screen sizes? My game is just loading them as Bitmaps when the images are being used and unloading them when they're done.
Also, why aren't my widgets resizing even though I used "dip" in the layout xmls?
When you qualify your images with -hdpi, -mdpi, -ldpi, you support different densities.
If you want to distinguish between screen sizes, you must qualify your drawables with -small, -normal, -large and -xlarge.
At Supporting Multiple Screens is an article about this issue, describing screen size, screen density and resolution.

Supporting multiple screens across different dpi and screen sizes

I have a two circles in a background image and I want to put two buttons in them. I am using relative layout. I have created several layout folders and images using guideline (layout, layout-large etc). But still the buttons are somewhat up or down in different actual devices. I am assuming this is because of Android default screen density bucket. Because if I provide only one layout for 320 to 479 dpi screen, It is possible that there could be a lot of devices in between. And my only layout for this range is surely going to be distorted? How do you design for supporting multiple devices? Thanks for you help in advance!
Add all the listed dimens.xml in vlaues folder for your project and manage them accordingly
values-sw320dp
values-sw360dp
values-sw480dp
values-sw600dp
values-sw720dp
This will definitely solve your problem of
supporting multiple devices.

Categories

Resources