Cheok Yan Cheng
Cheok Yan Cheng

Reputation: 42690

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

I am from Java Swing background. May I know why using XML to create GUI is a good practice in Android? For instance, instead of writing the code in (Which makes me feel more comfortable with it as I use to Swing desktop application)

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class HelloAndroid extends Activity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       TextView tv = new TextView(this);
       tv.setText("Hello, Android");
       setContentView(tv);
   }
}

We write the code in XML way.

import android.app.Activity;
import android.os.Bundle;

public class HelloAndroid extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

In tutorial, it states that,

This structure makes it very easy to quickly build up UIs, using a more simple structure and syntax than you would use in a programmatic layout.

However, I don't really buy the idea, as I feel that creating a separate XML file is more cumbersome.

Can anyone give a real world example (in the sense of Android), why using XML to build GUI is more superior than bare Java code?

If GUI programming through XML is really a good stuff, why it still hasn't become a common practice among GUI desktop application developers?

Upvotes: 14

Views: 10015

Answers (8)

behrad
behrad

Reputation: 1278

One thing that you should consider is that performance of creating your UI dynamically in your code has better performance in android. Telegram has so good performance also uses dynamic UI. Also, you can check article about dynamic and XML in performance in android that dynamic always has better performance. But it's obvious that dynamic UI is much harder than XML.

Upvotes: 0

Adil Mehmood
Adil Mehmood

Reputation: 462

Here are my thoughts:

  • a nice way to take care of my GUI plumbing.
  • whenever I need to experiment with GUI dimensional values, code doesn't need recompilation
  • XML Layouts are usually very easy to understand - initial GUI development becomes simplistic

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006644

If GUI programming through XML is really a good stuff, why it still hasn't become a common practice among GUI desktop application developers?

  • .NET development uses XAML, which is XML
  • Flex development, for Web and desktop via AIR, which is XML
  • XUL development is XML
  • There are ~20 XML bindings for Swing and SWT

Considering that .NET development alone probably exceeds all other desktop GUI development combined, I would argue that it has "become a common practice among GUI desktop application developers".

Moreover, using XML for GUIs is one facet of the larger "declarative UI" trend, which has many non-XML forms, such as the JSON-ish QML for Qt. AFAICT, all major GUI development is moving to incorporate such declarative markup in one form or fashion.

Upvotes: 8

Sarstime
Sarstime

Reputation: 184

I have to say, either ways can work well if developer is skilled. However, I think we have to consider how this work in a team. I agree with tangentstorm. In this way, we can easy to decouple UI developer and app developer. In a team, ppl can only focus what they are doing.

Upvotes: 0

Vikas Patidar
Vikas Patidar

Reputation: 43349

As i think you are from the Java Swing background so may be you consider the programmatically creating layout easy task. But there are many reasons that you can think of XML strategy:

1) XML layout design is easy to understand as well keep application user interface seperate from the application code.

2) There ary many devices in the market having diffirent-diffrent display sizes and Android provides an easier task to create UI for all using XML and keeping them seperate.

3) Also if you want to develop an multilingual application then XML is the best one.

Bottom line:

Only use the Java code for layout if you really need otherwise you have still best option XML.

Upvotes: 1

Adeel Ansari
Adeel Ansari

Reputation: 39897

Think of creating a UI using HTML/CSS. I always find that handy as compared to Swing. And I believe, its not just me, but other folks too think in the same way i.e. Jelly Swing.

Upvotes: 1

Romain Guy
Romain Guy

Reputation: 98501

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.

Upvotes: 24

tangentstorm
tangentstorm

Reputation: 7295

Well, for one thing, it allows you to use GUI builders (like the eclipse-powered builder Google provides).

It also helps you separate your business logic from your display logic.

Probably, though, you're just really good at Swing code, and you've forgotten how long it took to build up those skills. :)

Upvotes: 0

Related Questions