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.

Post a Comment

Archive