Tuesday, February 28, 2012

Android GUI building

This post is an update to my previous post about xml vs programmatic GUI development.


After a bit more Android experience it seems to be true that the xml syntax is indeed simpler than the Java equivalent. The Java code turns out to be even more verbose than the xml is. Here is a simple example:


ScrollView rootView = new ScrollView(this);
setContentView(rootView,
  ScrollView.LayoutParams.FILL_PARENT,
  ScrollView.LayoutParams.FILL_PARENT));
LinearLayout linear = new LinearLayout(this);
rootView.addView(linear,
  new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.FILL_PARENT,
    LinearLayout.LayoutParams.FILL_PARENT);
Also, it is more difficult to find documentation for the programmatic method. You are expected to create the GUI with xml.


It is still true that debugging xml is impossible. However, it hasn't been necessary to debug this xml. The declarative programming style seems to work in this case.


All the above speaks for the xml approach. However, the xml is simpler not because of some inherent quality, but rather because the Java API is really bad.


For my next Android project I might try to wrap the view classes into user-friendlier versions. After all I can make my own API if I'm not happy with the one provided by Google. Perhaps like this:


MyScrollView rootView = new MyScrollView(this, LP.FILL_PARENTLP.FILL_PARENT);
setContentView(rootView);
MyLinearLayout linear = rootView.addLinearLayout(LP.FILL_PARENTLP.FILL_PARENT);
A third approach would be a visual GUI builder. The one in Xcode 4 is a good start, but far from really good.

Sunday, February 12, 2012

Debugging XML?

Let me quote the Hello World tutorial at developer.android.com:

"The general structure of an Android XML layout file is simple: it's a tree of XML elements. ... This structure makes it easy to quickly build up UIs, using a more simple structure and syntax than you would use in a programmatic layout."

Hmm, let's try that. First the xml version:

<TextView
  android:id="@+id/textview"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:text="Hello, Android"/>
And now the Java version:

TextView tv = new TextView(this);
tv.setText("Hello, Android");
Is the xml version simpler? Not sure. It's a little bit verbose but still readable.

What about editor support? In a good editor, like that of IntelliJ, I can Ctrl+Space Java code as well as Android layout xml. So, no difference.

But there is one difference:

You can step Java code, but you cannot step XML!

Case closed.





Archive