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_height="fill_parent"
android:orientation="vertical" >
android:layout_height="wrap_content"
android:text="Hello, I am a TextView" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a Button"
android:onClick="myClick"/>
The result of the run should be the same as in Section 4.1.
4.2.3 EditText
EditText is a subclass of TextView and EditText is a thin veneer over TextView that configures itself to be editable.
Along with the standard TextView properties, EditText has many other properties including:
android:autoText Controls if the field should provide automatic spelling assistance.
android:capitalize Controls if the field should automatically capitalize the first letter of entered text.
android:digits Configures the field to accept only certain digits.
android:singleLine Controls if the field is for single-line input or multiple-line input.
Let us look at the example: "EditField.java" and "main.xml"package com.bogotobogo.EditField;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
public class EditField extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText et = (EditText)findViewById(R.id.edtext);
et.setText("Android Market is an online software store " +
"developed by Google for Android devices. " +
"An application program (\"app\") called \"Market\" " +
"is preinstalled on some Android devices and " +
"allows users to browse and download apps published " +
"by third-party developers, hosted on Android Market.");
}
}
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_height="wrap_content"
android:singleLine="false"
/>
Full List of Android Tutorials
25. Activity Life Cycle
24. Media
23. HTTP Internet Connection
22. Threads
21. Content Provider
20. Notification and Service
19. Animation - Frame By Frame, Layout, and View
18. Manifest
17. Map View
16. Preferences
15. Configuring Rotation
14. On Notepad Examples
13. Intent
12. Activity Testing
11. Android JUnit Test
10. Menus
9. TabWidget, Flipper, and SlidingDrawer
8. DatePicker, TimePicker, and Clocks
7. Advanced ListView Widget
6. ListView, Spinner
5. Layouts
4. User Interface
3. Back to Hello World Again
2. Hello World
1. Introduction
No comments:
Post a Comment