34

This question has been bugging me for some time. I've already developed a couple of apps on the Android platform and somehow always find myself resorting to Java code in order to construct the layouts. In a professional development environment, is this acceptable? Or should XML files be the go-to approach? I usually find XML a more tedious approach and often, these layouts don't look the same on all devices. I just don't get it. From a professional viewpoint, has anyone really been able to develop apps with complex views purely using XML files? This question is killing me because Google recommends using XML but the UI never looks the same on all devices unless done programmatically. Or am I doing something wrong?

Note that I'm referring to Android 2.2 and 2.3, which majority of the users use.

OckhamsRazor
  • 4,824
  • 13
  • 51
  • 88

5 Answers5

36

I use XML layouts on pretty much every fragment and activity of every app I write. I very rarely see any need to create Views dynamically, tho configuration of ListViews, showing/hiding views, etc needs doing in code. For me the advantages of XML are:

  • Ability to use layout editors (Eclipse)
  • Easier to preview layouts
  • Possible to benefit from auto-localisation of layouts
  • Easily maintain different parallel layouts for difference devices (screens)
  • Can get a sense of the layout by looking at it (easier than code)
  • Easy to break layouts down into pieces (fragments, includes, etc) to remove duplication
  • Keeps a separation between the visual design, and the functionality behind it

I can't think of any good reasons to put all my layouts into code - that sounds like hell.

I expect the reason your layouts don't look the same is because your XML is not defining the layouts correctly. Bear in mind the Android tools convert XML layouts into code, so there's no inherent problem with using XML layouts versus dynamic - both end up as code.

Ollie C
  • 28,313
  • 34
  • 134
  • 217
  • Thanks Ollie. I'm greatly encouraged by your XML-centric approach. Part of the reason is that I own a pretty weak development system and am unable to work with concurrent emulators. This is one of the main reasons I use code rather than XML, because I can be sure that my layouts will look the same on all devices. Otoh, if I use XML, I will have to work with the emulators so that I can test each layout and actually check out how the UI looks. – OckhamsRazor Mar 22 '12 at 18:36
  • Also, by any chance, do you have any open source android projects I could refer to? – OckhamsRazor Mar 22 '12 at 18:37
  • If you look at the Android app code samples in the SDK, or those with Mark Murphy's (Commonsware) books you'll see plenty of examples of use of XML. I use physical devices for testing - a handful of them provides high coverage for screen & OS types. I don't think it's a good idea to allow a slow dev machine to affect the architecture of your apps. – Ollie C Mar 22 '12 at 19:06
  • What About Heap Memory ? It Doesn't cause Heap Memory? Does it clear from Heap Memory After Scrolling out Of Screen Or When onDestroy event has called ? – Hossein Kurd May 26 '15 at 06:49
17

OckhamsRazor,

The answer very much depends on your needs, flexibility, and knowledge. The first thing to understand is that every Layout, whether created via XML or programmatically can be tweaked specifically or made to conform to many screens via properties.

... and somehow always find myself resorting to Java code in order to construct the layouts. In a professional development environment, is this acceptable?

Yes, it is. Android makes those available so you can do just that. However, the benefits of managing layouts via XML include standard MVC segregation, simpler debugging, and an easier time modifying the resource, if needed. Additionally, you may maintain multiple copies of Layouts depending on device configuration easily.

... has anyone really been able to develop apps with complex views purely using XML files?

Absolutely! There are some amazing programs that fully utilize XML rather than programmatic views. The key to them is how much information (that is non-standard view properties) is required from parental Views. Even in those cases there are ways to pass that information provided you know where and how to do so.

Or am I doing something wrong?

I don't think so. Honestly, I've run both ways depending on need. I'd say it really comes down to your lack of knowledge of the quirks. But the job is to get the job done. Here's an example: There are some times when I don't know how big everything needs to be until its run on the device, and there are times that I make the device conform to my layout's needs. Ultimately, I use the following chart to make my determinations.

  • Do I need information from parental Layouts that is aside from view properties
  • Do I need to dynamically size more than one element independently.
  • Is the View type pre-determined or will it change as well?

If the answer to 2 out of 3 of those is "yes", I will use some level of programmatic layout. If not, I will go pure XML. That being said, programming is one of those professions that encourages ingenuity (provided it is safe) and nearly anything can be accomplished in any number of ways. Ultimately, I'd say do whatever makes your job making quality apps easier.

Google makes its recommendations based on their own knowledge of software programmers and their general practices. They also created the platform, so they know which things are optimized in which ways. Its all about experience and we all have our own. If you have trouble utilizing XML, its worth taking the time to figure out the quirks simply so that it is another tool to utilize. Also, it will give you the information you need to answer this question for yourself.

To sum things up: I could say chocolate is better, but if you like vanilla, you'll disagree. Be aware of the drawbacks and benefits of each and take the time to learn how to accomplish the same tasks with both methods. It will make you a better programmer and give you a better sense of when to use which technique.

Hope this helps,

FuzzicalLogic

Fuzzical Logic
  • 12,947
  • 2
  • 30
  • 58
  • Thanks for your writeup - I still have a question on creating views programmatically, does that not take up more heap memory as you need to define everything in java code and hence wait for the GC to run to clean up memory? In some layouts, not all elements are interactive and therefore there is no need to create handlers for them. If you create these layouts on code, there will need to be handlers. If you create it on xml, you don't need handlers. – Simon Sep 15 '15 at 13:17
  • I don't happen to have the answer to that one. In theory, it depends heavily upon your handlers. The fact is that even XML generated layouts utilize prerender, render and postrender handlers, but these are mostly automatically handled. Some significant tests would have to be performed to come to any conclusion on this. What I can say is that simple handlers are sometimes more performant, but this may be indicative of either case. – Fuzzical Logic Oct 20 '15 at 13:19
7

I typically do a lot of work with highly customizable UIs, where large portions of it need to be done in code. That being said, wherever possible I try to use layout fragments and inflate them, so as UI sections are added, removed, or rearranged I'm still just doing some of the layout, not all of it.

That being said, it's not that hard doing layout via code. The big advantage to it is compile-time checking. I'll find issues that way faster than using the preview pane. The preview pane can be nice for initial layout, but I use the Hierarchy Viewer for figuring out why my layouts don't look right.

Hounshell
  • 5,321
  • 4
  • 34
  • 51
4

It really depends on what type of project it is, or piece of a project, and what type of programmer you are. Some people just prefer pure code, while others like leaning as much on other tools for design as possible.

XML definitely has some benefits, like being able to switch between interface designs quickly. For specific design themes that are repetitive, is definitely useful for most programmers.

I personally prefer doing everything programmatically, and it is quicker for me to develop than writing XML, with the libraries and classes I have created. XML is quicker straight out of the box.

As for performance, there really isn't a difference worth mentioning unless you are using the same view so repetitively, at the same time, to the point that it no longer fits on the screen many fold. I did a test of how many text views Android could render on a Moto X - Android 4.4, and it couldn't get much over 5000, but there is never a purpose for that. If you are at that point, you are either need to dynamically load and unload data or are just doing something very wrong to begin with.

So learn both sides of it, definitely get to know the pros and cons with your style of programming, because there is no right answer for everyone, and let loose and have fun.

Nido
  • 89
  • 1
  • 8
0

It is much better to separate the layout and put it in the xml file. I occasionally have to adjust the layout in code, but it is always an exception and only when I determine that it cannot be done in the layout .xml. If you use the layout views correctly, the application should look very similar on all devices.

Kevin Westwood
  • 7,739
  • 8
  • 38
  • 52