14.01.2013 Views

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 4: Creating User Interfaces<br />

78<br />

}<br />

TextView myTextView = (TextView)findViewById(R.id.myTextView);<br />

If you prefer the more traditional approach, you can specify the User Interface in code. The following<br />

snippet shows how to assign a new TextView as the User Interface:<br />

@Override<br />

public void onCreate(Bundle icicle) {<br />

super.onCreate(icicle);<br />

}<br />

TextView myTextView = new TextView(this);<br />

setContentView(myTextView);<br />

myTextView.setText(“Hello, Android”);<br />

The setContentView method accepts a single View instance; as a result, you have to group multiple<br />

controls to ensure that you can reference a layout using a single View or View Group.<br />

The Android Widget Toolbox<br />

Android supplies a toolbox of standard Views to help you create simple interfaces. By using these controls<br />

(and modifying or extending them as necessary), you can simplify your development and provide<br />

consistency between applications.<br />

The following list highlights some of the more familiar toolbox controls:<br />

❑ TextView A standard read only text label. It supports multiline display, string formatting,<br />

and automatic word wrapping.<br />

❑ EditText An editable text entry box. It accepts multiline entry and word wrapping.<br />

❑ ListView A View Group that creates and manages a group of Views used to display the items<br />

in a List. The standard ListView displays the string value of an array of objects using a Text<br />

View for each item.<br />

❑ Spinner Composite control that displays a TextView and an associated ListView that lets you<br />

select an item from a list to display in the textbox. It’s made from a Text View displaying the<br />

current selection, combined with a button that displays a selection dialog when pressed.<br />

❑ Button Standard push-button<br />

❑ CheckBox Two-state button represented with a checked or unchecked box<br />

❑ RadioButton Two-state grouped buttons. Presents the user with a number of binary options<br />

of which only one can be selected at a time.<br />

This is only a selection of the widgets available. Android also supports several more advanced View<br />

implementations including date-time pickers, auto-complete input boxes, maps, galleries, and tab<br />

sheets. For a more comprehensive list of the available widgets, head to<br />

http://code.google.com/android/reference/view-gallery.html.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!