0

After some years of Android development, i took months wondering why should i use xml layouts to make the UI of my Android App.

I understand that part of MVC and real-time graphical interface design.

If im wrong please take me out of ignorance and forgive me but , I´m Unable to set a view´s position, exactly, 8% of screen width for margin and 70% of screen width for width, for example.

Creating the UI in that way and taking care of screen aspect ratio ensures the app will look proportionally exactly the same in all devices.

As this post sums it up, we are told to use dip units and a nice bunch of xml files for every layout of our app if we want to support different screens. (Why they call it density-independent if we have to make it different for each screen density?)

But, even doing that, as (again) the Google Doc says

The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion.

And in place of not necessarily they should say mostly never looking at the incredible number of different devices. Depending of your project, this may be a problem. (Or do you want your client looking at your "slightly-decomposed" UI on a who knows chinese device)

I could ask the same for the image resources, but that whould be another question, i use to put the highest resolution on -nodpi then downscale by code to screen-width-dependent desired size, instead of putting 10.000 images at all resolutions.

Community
  • 1
  • 1
Nanoc
  • 2,381
  • 1
  • 20
  • 35
  • This might help. http://imgur.com/0QcNkz7 – denvercoder9 Apr 30 '15 at 03:23
  • 2
    You are **not required** to use XML, feel free to build your UI at runtime. – ozbek Apr 30 '15 at 08:48
  • I know it is not required, but it is the main way google show us, im primary looking for differents point of view of why using xml layouts is the most common pattern design. – Nanoc Apr 30 '15 at 09:26
  • @RafiduzzamanSonnet, the system scales resources based on density buckets, you cant control exactly the size of the scaled image, and, in any case, i cant see any reason to put each image 8 times at different resolutions. – Nanoc Apr 30 '15 at 10:18

1 Answers1

3

Creating the UI in that way and taking care of screen aspect ratio ensures the app will look proportionally exactly the same in all devices.

This is the way Apple does it, and this why when you open a folder on a 9.7 inch iPad with beautiful retina display, it only displays 9 icons.

A tablet is not just a bigger phone. It's really not. Also, iOS devices have mostly the same aspect ratio, so that's how they took care of the aspect ratio.

On Android however, you can't just say that you "took care of the aspect ratio" problem and that this problem magically goes away. Your best bet is to use Android best practices to handle aspect ratios correctly.

That being said, don't over do it either. You don't need to use all 8 density buckets for instance. Look at the official dashboard to see what devices people are actually using, before you try to optimize for every scenario possible.

Also, you don't have to listen to Google all the time. For instance, I am sure that Google would love that you translate your application so that it's available in more then 68+ different languages worldwide, but what's good for Google is not necessarily good for you. Android actually lets you choose the tools you want to use to make your application. They don't force you to use any particular one.

Community
  • 1
  • 1
Stephan Branczyk
  • 9,363
  • 2
  • 33
  • 49
  • So main pro is that you can build different UI for tablet devices (in a simplier way), not needed for most of my projects. And about aspect ratio, it does not go away "magically" but you can take account of it and make your UI looks well on all aspect ratios. So do you think it is worth making (at least more than 1) xml layouts looking at is pro/cons? – Nanoc May 04 '15 at 13:46
  • It really depends what your app does. For instance, if it was a public transportation real-time tracking app, I just wouldn't bother with tablets because 99% of my audience would probably use their phone with it, not their tablet. But I also wouldn't bother with making a proportional looking table, I would just be using a ListView which expands and shrinks with screen size. So in that case, I would just be using one xml layout. So again, it all depends on your actual application and the kind of design that would be appropriate for it, which you haven't told us about yet. – Stephan Branczyk May 04 '15 at 19:53
  • In most of my projects priority Nº1 is a perfect rendered UI that must look equal for phone or tablet, of course most screens are much more complex than a simple listview. My clients would complain if they see any screen element that have, for example , a little more margin than it should, on any device. So for that project needs, i think is better creating the UI by code than using xml layouts. – Nanoc May 05 '15 at 13:59