Jump to content

Two Ways to Create a User Interface for the Android

  chco's Photo
Posted May 12 2011 04:56 PM

There are two ways to create a user interface (UI) in Android. One is declarative, and the other one is programmatic. They are quite different but often are used together to get the job done. The following excerpt from Learning Android describes the two approaches.
Declarative User Interface

The declarative approach involves using XML to declare what the UI will look like, similar to creating a web page using HTML. You write tags and specify elements to appear on your screen. If you have ever handcoded an HTML page, you did pretty much the same work as creating an Android screen.

One advantage of the declarative approach is that you can use what-you-see-is-what-you-get (WYSIWYG) tools. Some of these tools ship with the Eclipse Android Development Tools (ADT) extension, and others come from third parties. Additionally, XML is fairly human-readable, and even people who are unfamiliar with the Android platform and framework can readily determine the intent of the user interface.

The disadvantage of a declarative UI approach is that you can get only so far with XML. XML is great for declaring the look and feel of your user interface, but it doesn’t provide a good way of handling user input. That’s where the programmatic approach comes in.

Programmatic User Interface

A programmatic user interface involves writing Java code to develop UI. If you have ever done any Java AWT or Java Swing development, Android is pretty much the same in that respect. It is similar to many UI toolkits in other languages as well.

Basically, if you want to create a button programmatically, you have to declare the button variable, create an instance of it, add it to a container and set any button properties that may make sense, such as color, text, text size, background, and so on. You probably also want to declare what the button does once it’s clicked, so that’s another piece of code. All in all, you end up writing quite a few lines of Java.

Everything you can do declaratively, you can also do programmatically. But Java also allows you to specify what happens when that button is actually clicked. This is the main advantage of a programmatic approach to the user interface.

The Best of Both Worlds

So which approach to use? The best practice is to use both. You would use a declarative (XML) approach to declare everything about the user interface that is static, such as the layout of the screen, all the widgets, etc. You would then switch to a programmatic (Java) approach to define what goes on when the user interacts with the various widgets in the user interface. In other words, you’d use XML to declare what the “button” looks like and Java to specify what it does.

Note: Note that there are two approaches to developing the actual user interface, but at the end of the day, all the XML is actually “inflated” into Java memory space as if you actually wrote Java code. So, it’s only Java code that runs.

Learning Android

Learn more about this topic from Learning Android.

If you're new to the Android mobile operating system, Learning Android is the perfect way to master the fundamentals. This gentle introduction shows you how to use Android's basic building blocks to develop user interfaces, store data, and more. You'll build an example application throughout the course of book, adding new features with each chapter. You'll also build your own toolbox of code patterns that will help you program any type of Android application with ease.

See what you'll learn

0 Subscribe

1 Reply

  james rogla's Photo
Posted Aug 03 2011 10:16 PM

Thanks for your informative information in this comments site like you said about thanks for the tips and ideas.
Carbon Fiber Vinyl