Why Should We Use Xml Layouts

Why should we use xml layouts?


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.

What is the advantage of using xml for layout?


What are the maintainability benefits of using XML for this?

  1. Separation of logic from presentation. This does help on larger projects when you need to refactor some code. If it is tightly coupled to the UI this job can become time consuming very easily if much of the presentation logic is in code.
  2. The structure of XML correlates nicely to the structure of a user interface, i.e., a tree like structure.

What are the performance benefits of using XML for this?

That all depends on the implementation I suppose. I doubt there are any appreciable performance differences, but I honestly don't know enough to say definitively what they may be.

Why we use xml?

XML files are strongly encouraged because it makes your app more closely follow the Model-View-Controller programming strategy. Having each part as separate from each other as possible makes it easier to develop and maintain your overall program. The performance difference seems to be minimal, although others have said that XML is preprocessed.

Why is XML used for the creation of UI layouts in Android?

Unlike what everyone said about the XML being easy and efficient. Here is what I read in Hello Android by Ed Brunnette (p. 49) which made sense.

Android is optimized for mobile devices with limited memory and
horsepower, so you may find it strange that it uses XML so
pervasively. After all, XML is a verbose, human-readable format not
known for its brevity or efficiency, right?

Although you see XML when writing your program, the Eclipse plug-in
invokes the Android resource compiler, aapt, to preprocess the XML
into a compressed binary format.**It is this format, not the original
XML text, that is stored on the device.

This was the kind of answer that i was looking for.(sorry if my question meant otherwise).

The reason that XML was chosen is mainly because of its familiarity and the number of IDE tools that natively support it. The developers could have chosen JSON for example and still compiled that to binary.The auto-generated R.java file is a helper for the IDE so that you can get the benefit of autocomplete when you want to access a resource.

Should I use Java or XML for Android layouts?

An advantage of XML layouts are better is that they permit to define multiple layouts with smaller code

for multi-language application u can define French and english UI and load the desired one at application startup

they permit to support many screen resolutions and orientation (define main.xml file for each profile)

also u can easily add and modify layout without touching u'r code

so, better to USE IT

What is the advantage of using xml for layout?


What are the maintainability benefits of using XML for this?

  1. Separation of logic from presentation. This does help on larger projects when you need to refactor some code. If it is tightly coupled to the UI this job can become time consuming very easily if much of the presentation logic is in code.
  2. The structure of XML correlates nicely to the structure of a user interface, i.e., a tree like structure.

What are the performance benefits of using XML for this?

That all depends on the implementation I suppose. I doubt there are any appreciable performance differences, but I honestly don't know enough to say definitively what they may be.

What is better to use for creating layouts. Java or XML?

Most of the time I prefer layout XML resource files, but it varies according to the need.

  1. First, the resource sets (e.g., res/layout-land/ in addition to res/layout/) allow you to define multiple UIs to be used in different circumstances.

    • layout-land -> for landscape

    • layout-port -> for portrait

    • layout-v15 -> for android version >= 15

    • layout-sw600dp -> for screens with a certain width

  2. Second, there are tools that can help you create those layout resources successfully. drag-and-drop GUI building of Eclipse is one of them.

  3. Third, it tends to be more terse, so if you're typing this stuff by hand, the XML will be less typing.

  4. In Java code you have to compile & run the code to see how the layout looks like, while in XML (if you are using eclipse) you can see it directly with the tools that eclipse provides.

For everything static I use XML, because it is easy to find in the structure of your project.

But in some cases, you want to create dynamic layouts and you have no other choice then to use Java Code. Be smart, in this, so if you have to add several Views that look the same do this

From my perspective, xml layouts are good if you want android to
handle the layouts automatically. This would be for things like
data-entry type applications (like banking applications). Java layouts
would be better for applications that need tight control of the UI
(like angry birds)

Why using XML to create GUI is a good practice in Android

Using XML layouts has many advantages over Java code. You get easy references to strings, drawables, dimensions, themes, etc. But the biggest advantage is automatic support for multiple configurations. Without changing your code you can have different layouts for landscape and portrait by just having an XML layout in layout-land/ and layout-port/. And you can do the same to adapt the layout to different resolutions, languages, etc.

Why is XML used for the creation of UI layouts in Android?

Unlike what everyone said about the XML being easy and efficient. Here is what I read in Hello Android by Ed Brunnette (p. 49) which made sense.

Android is optimized for mobile devices with limited memory and
horsepower, so you may find it strange that it uses XML so
pervasively. After all, XML is a verbose, human-readable format not
known for its brevity or efficiency, right?

Although you see XML when writing your program, the Eclipse plug-in
invokes the Android resource compiler, aapt, to preprocess the XML
into a compressed binary format.**It is this format, not the original
XML text, that is stored on the device.

This was the kind of answer that i was looking for.(sorry if my question meant otherwise).

The reason that XML was chosen is mainly because of its familiarity and the number of IDE tools that natively support it. The developers could have chosen JSON for example and still compiled that to binary.The auto-generated R.java file is a helper for the IDE so that you can get the benefit of autocomplete when you want to access a resource.

Best practices: Layouts on Android (Programmatic vs XML)

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.



Related Topics



Leave a reply



Submit