Saturday, July 24, 2010

Android Tutorial 3

4.2 Widgets
A widget is a View object that serves as an interface for interaction with the user. Android provides a set of fully implemented widgets, like buttons, checkboxes, and text-entry fields, so we can quickly build our UI. Some widgets provided by Android are more complex, like a date picker, a clock, and zoom controls. But we're not limited to the kinds of widgets provided by the Android platform. If we want to do something more customized and create our own actionable elements, we can, by defining our own View object or by extending and combining existing widgets.
4.2.1 Labels
The simplest widget is the label. Labels is a text that cannot be edited directly by users. Typically, they are used to identify adjacent widgets.
We can create a label by creating a TextView instance. Or we can create labels in XML layout files by adding a TextView element to the layout, with an android:text property to set the value of the label itself. If we need to swap labels based on certain criteria, such as internationalization, we may wish to use a resource reference in the XML instead.
TextView has numerous other properties of relevance for labels:
android:typefaceSet the typeface to use for the label (e.g., monospace).
android:textStype Indicates that the typeface should be made bold, italic, or bold. and italic.
android:textColor Sets the color of the label's text, (e.g., #0000ff for blue).
4.2.2 Buttons
Android 1.6 adds a new feature for the declaration of the "on-click" listener for a Button. In addition to the classic approach of defining some object (such as the activity) as implementing the View.OnClickListener interface, we cannot take a somewhat simpler approach:
Define some method on you Activity that holds the button that takes a single View parameter, has a void return value, and is public.
In out layout XML, on the Buttton element, include the android:onClick attribute with the name of the method you defined in the previous step.
For example, we can modify the "layout.java" in Section. 4.1 and save it as "OnClick.java"package com.bogotobogo.OnClick;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class OnClick extends Activity {
//implements View.OnClickListener{

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void myClick(View view){
TextView tv =(TextView)findViewById(R.id.text);
tv.setText("button pressed");
}
}
We have a new method myClick() which was defined in "main.xlm" and we do not have to implement View.OnClickListener in our Activity.
Here is the "main.xml" file with an additional method, android:onClick="myClick".
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a TextView" />

No comments:

Post a Comment