Chapter 4. Designing Your User Interface Using Views

WHAT YOU WILL LEARN IN THIS CHAPTER

  • How to use the basic views in Android to design your user interface

  • How to use the picker views to display lists of items

  • How to use the list views to display lists of items

In the previous chapter, you learned about the various layouts that you can use to position your views in an activity. You also learned about the techniques you can use to adapt to different screen resolutions and sizes. In this chapter, you will take a look at the various views that you can use to design the user interface for your applications.

In particular, you will learn about the following view groups:

  • Basic views — Commonly used views such as the TextView, EditText, and Button views

  • Picker views — Views that enable users to select from a list, such as the TimePicker and DatePicker views

  • List views — Views that display a long list of items, such as the ListView and the SpinnerView views

Subsequent chapters will cover the other views not covered in this chapter, such as the date and time picker views and other views for displaying graphics, etc.

BASIC VIEWS

To get started, let's explore some of the basic views that you can use to design the UI of your Android applications:

  • TextView

  • EditText

  • Button

  • ImageButton

  • CheckBox

  • ToggleButton

  • RadioButton

  • RadioGroup

These basic views enable you to display text information, as well as perform some basic selection. The following sections explore all these views in more detail.

TextView View

When you create a new Android project, Eclipse always creates the main.xml file (located in the res/layout folder), which contains a <TextView> element:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
</LinearLayout>

The TextView view is used to display text to the user. This is the most basic view and one that you will frequently use when you develop Android applications. If you need to allow users to edit the text displayed, you should use the subclass of TextView, EditText, which is discussed in the next section.

Note

In some other platforms, the TextView is commonly known as the label view. Its sole purpose is to display text on the screen.

Button, ImageButton, EditText, CheckBox, ToggleButton, RadioButton, and RadioGroup Views

Besides the TextView view, which you will likely use the most often, there are some other basic controls that you will find yourself frequently using: Button, ImageButton, EditText, CheckBox, ToggleButton, RadioButton, and RadioGroup:

  • Button — Represents a push-button widget

  • ImageButton — Similar to the Button view, except that it also displays an image

  • EditText — A subclass of the TextView view, except that it allows users to edit its text content

  • CheckBox — A special type of button that has two states: checked or unchecked

  • RadioGroup and RadioButton — The RadioButton has two states: either checked or unchecked. Once a RadioButton is checked, it cannot be unchecked. A RadioGroup is used to group together one or more RadioButton views, thereby allowing only one RadioButton to be checked within the RadioGroup.

  • ToggleButton — Displays checked/unchecked states using a light indicator

The following Try It Out provides details about how these views work.

Now that you have seen how the various views look for an activity, the following Try It Out demonstrates how you can programmatically control them

ProgressBar View

The ProgressBar view provides visual feedback of some ongoing tasks, such as when you are performing a task in the background. For example, you might be downloading some data from the Web and need to update the user about the status of the download. In this case, the ProgressBar view is a good choice for this task.

The next Try It Out shows how you can change the look of the ProgressBar.

AutoCompleteTextView View

The AutoCompleteTextView is a view that is similar to EditText (in fact it is a subclass of EditText), except that it shows a list of completion suggestions automatically while the user is typing. The following Try It Out shows how to use the AutoCompleteTextView to automatically help users complete the text entry.

PICKER VIEWS

Selecting the date and time is one of the common tasks you need to perform in a mobile application. Android supports this functionality through the TimePicker and DatePicker views. The following sections show how to make use of these views in your activity.

TimePicker View

The TimePicker view enables users to select a time of the day, in either 24-hour mode or AM/PM mode. The following Try It Out shows you how to use it.

Displaying the TimePicker in a Dialog Window

While you can display the TimePicker in an activity, a better way is to display it in a dialog window, so that once the time is set, it disappears and doesn't take up any space in an activity. The following Try It Out shows how to do just that.

DatePicker View

Another view that is similar to the TimePicker is the DatePicker. Using the DatePicker, you can enable users to select a particular date on the activity. The following Try It Out shows you how to use the DatePicker.

Displaying the DatePicker View in a Dialog Window

Like the TimePicker, you can also display the DatePicker in a dialog window. The following Try It Out shows you how.

LIST VIEWS

List views are views that enable you to display a long list of items. In Android, there are two types of list views: ListView and SpinnerView. Both are useful for displaying long lists of items. The following Try It Outs show them in action.

ListView View

The ListView displays a list of items in a vertically scrolling list. The following Try It Out demonstrates how to display a list of items using the ListView.

Customizing the ListView

The ListView is a versatile control that you can further customize. The following Try It Out shows how you can allow multiple items in the ListView to be selected and how you can enable filtering support.

While this example shows that the list of presidents' names is stored in an array, in a real-life application it is recommended that you either retrieve them from a database or at least store them in the strings.xml file. The following Try It Out shows you how.

Using the Spinner View

The ListView displays a long list of items in an activity, but sometimes you may want your user interface to display other views, and hence you do not have the additional space for a full-screen view like the ListView. In such cases, you should use the SpinnerView. The SpinnerView displays one item at a time from a list and enables users to choose among them.

The following Try It Out shows how you can use the SpinnerView in your activity.

SUMMARY

This chapter provided a brief look at some of the commonly used views in an Android application. While it is not possible to exhaustively examine each view in detail, the views you learned about here should provide a good foundation for designing your Android application's user interface, regardless of its requirements.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.243.15