Best way to define Android layout files for multiple devices? - android

What is the standard/best way to define sizing for elements in Android layout files to account for Android devices with all types of screens?
For example: I've been using a LinearLayout to arrange multiple child elements (TextViews, Buttons) one after the other horizontally or vertically for a ListView. However, on a smaller device, I may want to scale the elements down or shrink some elements but keep the others fixed.
I'd appreciate input. Thanks

There's an entry in the android dev guide for that: http://developer.android.com/guide/practices/screens_support.html
You can use drawables in drawable-hdpi, drawable-mdpi and drawable-ldpi. Test those in different screens in the emulator and tweak the sizes appropriately

Please read supporting multiple screens.
In a nutshell: It's very easy. You can create subfolders of each folder in the resources (like layout-small, layout-large, layout-hdpi), and add different versions of your layout. The system will always fall back to the layout folder if none of your specializations (hdpi, large, etc) match.

Related

Using layout in android xml

Which is the layout to use which doesn't have a difference in different screen sizes. Linear layout or relative. I had a relative layout and when I run the app on a 4' phone, the icons were way closer. In a 5' phone the icons were a bit away. What's the best way to have a constant XML which doesn't change by screen size.
For example.
I had this app in linear layout which contained 3 images in horizontal orientation. In the design view the icons were perfectly same and equidistant. But on the device, the third icon was smaller. Why
You can't achieve perfect layout for every device with just single layout file. For best results you should make different layout files for different folders used with images designed accordingly. For more information read Android Developers' tutorial from the following link
Supporting Multiple Screens
Over and all, you need to create different layouts/drawables for supporting all the screens. It depends on your requirement, say for example, if you would want to include 2 buttons in small screen and 4 buttons on large screens then obvious you have to create different layouts.
If you would want to display same number of buttons in all screens then I would suggest you to prepare different set of images/drawables and include them in your project. Here you don't need to do anything other than placing images in particular drawable folders.
One more thing, If its a plain background then you can create 9-patch image so that it can stretch with whatever size you want or your device supports.
More study:
http://developer.android.com/guide/practices/screens_support.html
http://developer.android.com/training/multiscreen/screensizes.html
http://developer.android.com/training/multiscreen/screendensities.html
http://developer.android.com/training/multiscreen/adaptui.html

How can I fix android tablet's width and height

I am making android application in tablet using fragments so that I arranged 8 buttons vertically in 10.1 tablet it is showing fine but in 7.1 tablet only 5 icons it is showing.
So How can I manage height and width properly in all tablets ... please help
Remember to write different xml layouts (with same name) and place to different folders: layout-large, layout-normal, layout-small and layout-xlarge. Same is with drawables (different size images) and values (different type padding and other values). These folders are for different phone/tablet types with various resolution and screen size. If you do it, then you can easily manage layout for small device and not touch big screen devices. Device is picking xml file automatically so it's good practice to place xml files in different folders for various phones.
More information you can find in http://developer.android.com/guide/practices/screens_support.html
You can use different layout for different screen sizes. You can also add different buttons and other drawable also. Please see the link
Supporting Multiple Screens
You can write different xml layouts using different folders:
For example:
res/layout
res/layout-sw600dp //used by 7''
res/layout-sw720dp //used by 10''
Also you can use the same layout with the same component but with different dimensions.
You can use #dimen/my_dimen notation, and then put your my_dimen in different folders like:
res/values/dimens.xml
res/values-sw600dp/dimens.xml
and so on..
Official doc: http://developer.android.com/guide/practices/screens_support.html

problems with screen sizing for app

So I'm going through the main layout for my app and I have one layout folder "layout" the activity for my app has way to many text views and buttons on it to be able to use a relative layout with moving 1 thing and messing up the entire layout. I've been trying to figure out a way to just force the app into screen compatibility mode with my current layout (frame layout with just using margins) and it's displaying all messed up and nasty.
Does anyone know of a better way to support different screens?
Also, the answer to your first question is:Yes. I have gone through the Supporting multiple screens and screen compatibility mode in the documentation.
Use the nested layouts.For 10 inch tablet you have to specify layout-xlarge
For 9 inch tablet specify layout-sw720dp
see the below link for more details
http://developer.android.com/guide/practices/screens_support.html
my suggestion is to use nested linear layout and for different screen sizes you have to create different layout folders in your app such as layout_small,layout_large,layout_xlarge,layout_land then copy your xml files into these folders and make changes based on that
Hi williams you can create different layouts for screen sizes such as
layout-large,
layout-small,
layout-xlarge ,
So u can manage the screen sizes by creating different layout folders. And then copy your layout XML files to different layouts and you can modify the text fields , buttons ..etc as the screen size .

Android: support multiple screens

I am trying to make a application in Android. I want that it should be able to run on multiple phones of different screen sizes, so i studied support multiple screen on developers and according to that i have to create 3 different xml files for supporting three different screen sizes and also 3 different types of images for each type of xml file. But on a blog i get the idea of doing this by using current screen size method. So i am confused what i should do. means which is optimized and performance increasing way. And which one will be more perfect for supporting all types of screen(except extra large screens)
Defining height, width and other parameters in the XML file is the better option rather than on run time.
Because XML files works as metadata (data carrier) to the activity and avoids alot of confusion when onCreate mothode in called.
Plus, create different folders for image quality (hdpi,xhdpi,ndpi,ldpi)
7 inch device use mhpi
10 inch devices use hdpi and xhdpi
While NEXUS tabs use hdpi and xhdpi irrespective of their size.
Mobiles use ldpi and ndpi.
Beauty lies here is that android device automatically pick-p the suitable content when found, i.e layout and image.
If not found it would first search other Layout folders,e.g a layout not found in x-large folder then it will search in large,then medium, small, which one of them suits the best ,(if a layout is not found in its respective folder).
Nexus will create alot of trouble for you.
To check how your layout would look on different devices, try using the options, which tells you how it would look on that device with those height width, present in the Graphical (view of a ) layout.
You can use three different layouts for different screen sizes ,and android will pick the suitable layout , but Using three different layouts for each type of screen format will not be a good idea , because it will cause problem in handling all layout , if screens are less then its fine but if number of screen increases it will get difficult . Like if you forget to add change in one of the screen size it shall crash with any exception .
What you can do is keep images of different size in different folders and practice layout to make standard in one layout by using layout weights , and margins in in dp .
See my this answer
Table Layout spacing issues
and check this layout will look similar for all screen sizes.
If your design is same for all screens sizes you can use dp and have only one xml for all screens.
But you should support icons for all screens.
I think it's less confusing David Ohanyan way, but forgot to say something...
Whenever you can, use styles in your xx_layout, images, etc, so you'll have 1 layout.xml and 3 styles files inside folders: values, values-small, layout-large.
At least for me, it's less confusing than opening 30 different layout files.

android - How to build complex views that stretch?

I'm at the planning stages of my first proper android app. Part of the brief is to have a very "good looking" interface (aka, plenty of graphics). As I'm a web developer, and I'm used to the CSS way of doing things, I'm struggling to understand how I will cater for all the different resolutions when building my layouts. To cut it short, my question is this: how do I build complex, image heavy views that can be seen in apps such as SoundHound (example view here, for those not familiar with this app: http://getandroidstuff.com/wp-content/uploads/2010/09/SoundHound-Android2.jpg)? Is there a way I can tile images in a similar way to the way I'd tile in CSS?
It is important that you know how all Layouts Views work before you start designing your main layout. Android OS by very design supports stretching of views and GroupViews. You need to use dp (density independent pixel) units.
In the example you provided, there are some nice nine patch PNGs. Check C:\android-sdk-windows\tools\draw9patch.bat if on PC. These pngs stretch properly as backgrounds for any resolution.
Linear layout have weight which allows you determine how much space will each child occupy, similar to the old column width for tables in %. Just like in old hml, to make a rich layout you use nested GroupViews (e.g. LinearLayout, RelativeLayout etc) and populate them with children views like ImageView, TextView and son on.
It is possible also to have text styled with <b>,<i> tags and have
<img src="DrawableName">
see an example: Html.ImageGetter
Opposite to a nested layout would be to use the RelativeLayout and determine the position of each child view by its top and left margin anywhere on the screen in relation to the top-left corner of their parent RelativeLayout.
Android has some conventions, that help you out when it comes to different screen sizes, screen densities, screen ratios ...
For example there is a directory called "drawable" where you can put the image resources for your application. However, if you want to ship your application for devices with different resolutions, you can create additional folders named "drawable-ldpi", "drawable-mdpi", "drawable-hdpi". The ldpi folder contains image resources for low density screens, mdpi is meant for medium density and hdpi meant for high density. The folder names are part of the Android conventions. When loading an image resource at runtime e.g. on a high density device, the system is looking for the image resource in the drawable-hdpi folder. It there is no image with that name, the system will look in the drawable folder.
This concept also applies to the xml layouts you define in your "layout" folder. Maybe you say that the layout homeScreen.xml needs to look different when the user switches from portrait to landscape mode. Then you can create a folder "layout-land". You create another homeScreen.xml in there and change its apperance to your needs. At runtime the system detects the current mode. If in landscape, it will first check if there ist a folder "layout-land" and if it contains the layout file with the correct name. If not, or when the user switches back in portrait mode and there is no folder "layout-port", the system looks for the layout file in the "layout" folder.
This is only a small part of it, but I hope, you got a first glance and everything was understandable. Further information can be found in the official refrence

Categories

Resources