ADT XML layouts


So I’ve been fiddling around with ADT for a couple of hours. It’s possible to programmatically create layouts; i.e., an Android UI, but it’s not great. It’s way easier to define the layout in XML, and then bind whatever event handlers are required. Here we have a simple class:


import android.os.Bundle;
import android.view.*;
import android.widget.*;
import java.util.*;

public class HWA extends Activity implements View.OnClickListener {
  protected void onCreate(Bundle savedInstanceState) {

    Button b = (Button) findViewById(;

  public void onClick(View v) {
    switch (v.getId()) {
      case {
        Button b = (Button) v;
        b.setText(new Date().toString()); break;

As you can see, it’s quite bare. Load an XML layout R.layout.main, define a button, then bind a click listener to the button.

Then we have onClick() to set button text to the current String-ified date. Our code is really clean, and we do all the UI-related stuff in /res/layout/main.xml, and put all our conveniently l10n strings in /res/values/strings.xml.