Android’s GUI toolkit include all the common UI widgets like text labels, buttons, input fields, etc. While these widgets can be created and attached to our activities using Java code, in Android development it is more common to do it using XML-based layout files. An XML layout files is a specification of the widgets’ relationships to each other, and are stored in the res/layout directory inside an Android project tree.
Labels
The simplest UI element is probably the label, which is basically just text. In Android the label is referred to as a TextView. If you have created an application skeleton with Eclipse, the text that the default activity outputs to the screen is actually such a TextView. The TextView is defined in main.xml, and placed into the activity’s view via a setcontentView call:
setContentView(R.layout.main);
The definition of the TextView itself looks like this:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
Note that the text specified in the above TextView definition tells the widget to use the string resource with the identifier “hello” as the text, which is defined in res/values/strings.xml as:
<resources>
<string name="hello">Hello World, HelloWorld!</string>
</resources>
If we run the application, we will see that this label widget showing the text properly on the screen.

Buttons
The Android Button class is actually a subclass of TextView, to define a button we can try adding the following code to our layout xml file:
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button"
/>
When we run the activity, here is what we will see:

Since all we have done is adding the button to the layout, it will not do anything when we click on it. What we need to do is to define a method in our activity, then associate the button to this method by adding an onClick attribute to the button XML element. Note that when implementing this we need to import two additional packages to our code:
import android.view.View;
import android.widget.Button;
Now in the activity we can add the method:
public void onClick(View theButton) {
Button btn=(Button)findViewById(R.id.button);
btn.setText("Button Clicked!");
}
And the following to the xml layout file:
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="onClick"
/>
The above code changes the text of our button when we click on it. When running the activity on the emulator here is what you should see:

Note that this method is only supported by Android 1.6 and above. Although there is another possible method to achieve the same thing, which is to define the activity to implement the View.OnClickListener interface.
ImageView and ImageButton
As their names suggest, ImageView and ImageButton are the image based versions of TextView and button. To specify the image to be used, we use the android:src attribute in the XML element, which usually references a drawable resource in the project. For instance, when the Eclipse project wizard creates a project skeleton, it includes an image, icon.png in the drawable folder by default. We can try to show this image by adding the following code to the layout XML file:
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/icon"
/>
When we run the activity in the emulator here is what we will see:

ImageButton works the same way as the regular button, with which we can use an onClick attribute to attach a handler method to a click event.
Fields
Fields are input areas where the user can type in text. To use Android fields, use the EditText widget in the layout XML file as follows:
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
What will show up in our UI is this:

The EditText widget is also a subclass of the TextView, but it has many other attributes that can be specified to alter the behavior of the field. For instance: android:autoText, android:capitalize, android:digits and android:singleLine to name a few. The purposes of these attributes are self-evident.
Checkbox
To use Android checkboxes, use the CheckBox widget in the layout XML file as follows:
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a checkbox"
/>

In Java, we can use the methods isChecked(), setChecked(), toggle() to check or modify the state of our checkbox. Alternatively, we can implement the CompoundButton.OnCheckedChangedListener interface with our activity, and use setOnCheckedChangeListener() to attach a callback function to handle the check/uncheck events.
Radio Buttons
Android’s RadioButton widget inherits from the CompoundButton, which in turn inherits from TextView. Multiple RadioButtons can be put inside a RadioGroup, which means that only one button of the group can be selected at a time. The following example XML code place a group of RadioButtons in our activity’s layout:
<RadioGroup
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button 1" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button 2" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button 3" />
</RadioGroup>
When the activity is run in the emulator, this is what it will look like:
