Saturday, July 24, 2010
Android Tutorial 5
java.lang.Object -> android.view.View -> android.widget.TextView -> android.widget.Button -> android.widget.CompoundButton -> android.widget.RadioButton.
A radio button is a two-states button that can be either checked or unchecked. When the radio button is unchecked, the user can press or click it to check it. However, contrary to a CheckBox, a radio button cannot be unchecked by the user once checked.
Radio buttons are normally used together in a RadioGroup. When several radio buttons live inside a radio group, checking one radio button unchecks all the others.
4.2.6 Others
There are other componets like ListView, Gallery, Spinner, ViewFlipper, and the more special-purpose AutoCompleteTextView, ImageSwitcher, QuickContactBadge, date-time pickers, auto-complete input boxes, maps, tab sheets, and TextSwitcher.
I will make examples for those components later. Here, I will just summarize the each component.
ListView A view that shows items in a vertically scrolling list. The items come from the ListAdapter associated with this view.
Gallery A view that shows items in a center-locked, horizontally scrolling list. The default values for the Gallery assume you will be using Theme_galleryItemBackground as the background for each View given to the Gallery from the Adapter. If you are not doing this, you may need to adjust some Gallery properties, such as the spacing. Views given to the Gallery should use Gallery.LayoutParams as their layout parameters type.
Spinner A view that displays one child at a time and lets the user pick among them. The items in the Spinner come from the Adapter associated with this view.
ViewFlipper A View Group that lets us define a collection of Views as a horizontal row in which only one View is visible at a time, and in which transitions between visible views are animated.
AutoCompleteTextView An editable text view that shows completion suggestions automatically while the user is typing. The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box with. The drop down can be dismissed at any time by pressing the back key or, if no item is selected in the drop down, by pressing the enter/dpad center key. The list of suggestions is obtained from a data adapter and appears only after a given number of characters defined by the threshold.
ImageSwitcher
QuickContactBadge Displays a badge showing the image icon assigned to a contact we specify using a phone number, name, e-mail address, or URI. Clicking the image will display the quick contact bar, which provides shortcuts for contacting the selected contact - including calling, sending an SMS, e-mail, and IM.
TextSwitcher Specialized ViewSwitcher that contains only children of type TextView. A TextSwitcher is useful to animate a label on screen. Whenever setText(CharSequence) is called, TextSwitcher animates the current text out and animates the new text in.
4.3 UI Events
Once you've added some Views/widgets to the UI, you probably want to know about the user's interaction with them, so you can perform actions. To be informed of UI events, you need to do one of two things:
Define an event listener and register it with the View. More often than not, this is how you'll listen for events. The View class contains a collection of nested interfaces named OnListener, each with a callback method called On(). For example,
View.OnClickListener for handling "clicks" on a View.
View.OnTouchListener for handling touch screen events in a View.
View.OnKeyListener for handling device key presses within a View. So if you want your View to be notified when it is "clicked" (such as when a button is selected), implement OnClickListener and define its onClick() callback method (where you perform the action upon click), and register it to the View with setOnClickListener().
Override an existing callback method for the View. This is what you should do when you've implemented your own View class and want to listen for specific events that occur within it. Example events you can handle include:
onTouchEvent() Called when the touchscreen is pressed or released, or when it detects movement.
onTrackballEvent() Called when the device's trackball is moved.
onKeyDown() Called when any device key is pressed; includes the D-pad, keyboard, hang-up, call, back, and camera buttons.
onKeyUp() Called when a user releases a pressed key. This allows you to define the default behavior for each event inside your custom View and determine whether the event should be passed on to some other child View. Again, these are callbacks to the View class, so your only chance to define them is when you build a custom component.
4.4 Menus
Menus offer a way to expose application functions without sacrificing valuable screen space. Each Activity can specify its own menu that's displayed when the device's menu button is pressed.
Application menus are another important part of an application's UI. Menus offer a reliable interface that reveals application functions and settings. The most common application menu is revealed by pressing the MENU key on the device. However, you can also add Context Menus, which may be revealed when the user presses and holds down on an item.
Menus are also structured using a View hierarchy, but you don't define this structure yourself. Instead, you define the onCreateOptionsMenu() or onCreateContextMenu() callback methods for your Activity and declare the items that you want to include in your menu. At the appropriate time, Android will automatically create the necessary View hierarchy for the menu and draw each of your menu items in it.
Menus also handle their own events, so there's no need to register event listeners on the items in your menu. When an item in your menu is selected, the onOptionsItemSelected() or onContextItemSelected() method will be called by the framework.
And just like your application layout, you have the option to declare the items for you menu in an XML file.
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
http://www.bogotobogo.com/android.html
http://www.bogotobogo.com/Android/android25ActivityLifeCycle.html
http://www.bogotobogo.com/Android/android24Media.html
http://www.bogotobogo.com/Android/android23HTTP.html
http://www.bogotobogo.com/Android/android22Threads.html
http://www.bogotobogo.com/Android/android21ContentProvider.html
http://www.bogotobogo.com/Android/android20NotificationService.html
http://www.bogotobogo.com/Android/android19Animation.html
http://www.bogotobogo.com/Android/android18Manifest.html
http://www.bogotobogo.com/Android/android17MapView.html
http://www.bogotobogo.com/Android/android16Preferences.html
http://www.bogotobogo.com/Android/android15Rotation.html
http://www.bogotobogo.com/Android/android13Intent.html
http://www.bogotobogo.com/Android/android12ActivityTesting.html
http://www.bogotobogo.com/Android/android11JUnitTest.html
http://www.bogotobogo.com/Android/android10Menus.html
http://www.bogotobogo.com/Android/android9TabWidgetFlipper.html
http://www.bogotobogo.com/Android/android8DatePickerTimePickerClocks.html
http://www.bogotobogo.com/Android/android7dvancedListView.html
http://www.bogotobogo.com/Android/android6ListViewSpinnerGridViewGallery.html
http://www.bogotobogo.com/Android/android5Layouts.html
http://www.bogotobogo.com/Android/android4UserInterface.html
http://www.bogotobogo.com/Android/android3BackToHelloWorld.html
http://www.bogotobogo.com/Android/android2MyFirstApplication.html
http://www.bogotobogo.com/android.html
Android Tutorial 4
java.lang.Object -> android.view.View -> android.widget.TextView -> android.widget.Button -> android.widget.CompoundButton -> android.widget.CheckBox.
A checkbox is a specific type of two-states button that can be either checked or unchecked. It has following properties:
isChecked() Determines if the check box has been checked.
setChecked() Forces the check box into a checked or unchecked state.
toggle() Toggles the check box as if the user checked it.
We can register a listener object (in this case, an instance of OnCheckedChangeListener) to be notified when the state of the check box changes.
Let us look at the example, "MyCheckBox.java."package com.bogotobogo.MyCheckBox;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
public class MyCheckBox extends Activity
implements CompoundButton.OnCheckedChangeListener{
CheckBox cb;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cb = (CheckBox)findViewById(R.id.checkbox);
cb.setOnCheckedChangeListener(this);
}
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(isChecked){
cb.setText("state: checked");
}
else {
cb.setText("state: unchecked");
}
}
}
Note that the activity serves as its own listener for check box state changes, since it implements the OnCheckedChangeListener interface (via cb.setOnCheckedChangeListener(this)). The callback for the listener is onCheckedChanged(), which receives the check box whose state has changed. Then, we update the text of the check box to indicate what the actual box contains.
The layout file is:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_height="wrap_content"
android:text="state: unchecked"
/>
Run this application and the result should look like this:
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
Android Tutorial 3
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
Android Tutorial 2
While it is possible to create and attach widgets to our activity purely through Java code, the most common way to define your layout and express the view hierarchy is with an XML layout file. XML offers a human-readable structure for the layout, much like HTML. Each element in XML is either a View or ViewGroup object (or descendant thereof). View objects are leaves in the tree, ViewGroup objects are branches in the tree.
Dynamic instantiation of widgets, for example, using addView(View) method to insert new View or ViewGroup object, is reserved for more complicated scenarios, where the widgets are not known at compile time.
As the name indicates, an XML layout is a specification of widget's relationships to each other-and to their containers-encoded in XML format. Android considers XML layouts to be resources and layout files are stored in the res/layout directory inside Android project.
Android's SDK ships with a tool (aapt) that uses the layouts. The tool should be automatically invoked by Android toolchain (Eclipse or Ant's build.xml). The aapt generates the R.java source file within the project, allowing us to access layouts and widgets within those layouts directly from Java code.
Why use XML layouts? The biggest reason, perhaps, is to assist in the creation of tools for view definition, such as a GUI builder in an IDE like Eclipse. Such GUI builders could generate Java code instead of XML. But the challenge is reading in the UI definition to support edits, which is far simpler when the data is in a structured format like XML rather than in a programming language. Moreover, keep generated definitions separated from Java code can reduce the mistakes by the programmers.
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
Android Tutorial 1
4.0 User Interface
In an Android application, the user interface is built using View and ViewGroup objects. There are many types of views and view groups, each of which is a descendant of the View class.
When we look at the documentation, the View class is declared as:View
extend Object
implements Drawable.Callback KeyEvent.Callback
AccessibilityEventSource
And the ViewGroup is declared as:ViewGroup
extends View
implements ViewManager ViewParent
View objects are the basic units of user interface expression on the Android platform. The View class serves as the base for subclasses called "widgets," which offer fully implemented UI objects, like text fields and buttons. The ViewGroup class serves as the base for subclasses called "layouts," which offer different kinds of layout architecture, like linear, tabular and relative.
A View object is a data structure whose properties store the layout parameters and content for a specific rectangular area of the screen. A View object handles its own measurement, layout, drawing, focus change, scrolling, and key/gesture interactions for the rectangular area of the screen in which it resides. As an object in the user interface, a View is also a point of interaction for the user and the receiver of the interaction events.
On the Android platform, you define an Activity's UI using a hierarchy of View and ViewGroup nodes, as shown in the diagram above. This hierarchy tree can be as simple or complex as you need it to be, and you can build it up using Android's set of predefined widgets and layouts, or with custom Views that you create yourself.
In order to attach the view hierarchy tree to the screen for rendering, your Activity must call the setContentView() method and pass a reference to the root node object: to inflate within an Activity, usually within the onCreate() method, as setContentView() method accepts a resource identifier @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
or as in the following example, to assign a user interface to an Activity, we can call setContentView() method from the onCreate() method of our Activity. In the example TextView is used as the Activity's user interface: @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello, Android");
setContentView(tv);
}
The Android system receives this reference and uses it to invalidate, measure, and draw the tree. The root node of the hierarchy requests that its child nodes draw themselves � in turn, each view group node is responsible for calling upon each of its own child views to draw themselves. The children may request a size and location within the parent, but the parent object has the final decision on where how big each child can be. Android parses the elements of your layout in-order (from the top of the hierarchy tree), instantiating the Views and adding them to their parent(s). Because these are drawn in-order, if there are elements that overlap positions, the last one to be drawn will lie on top of others previously drawn to that space.
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
Thursday, July 22, 2010
Android Applications Tutorial 18. Manifest
18. Manifest
18.1 AndroidManifest.xml File
18.2 Structure of the AndroidManifest.xml File
18.3 File Conventions
18.4 File Features
18.1 AndroidManifest.xml File
The foundation for any Android application is the manifest file. Every application must have an AndroidManifest.xml file in its root directory. The manifest presents essential information about the application to the Android system, information the system must have before it can run any of the application's code. Among other things, the manifest does the following:
It names the Java package for the application. The package name serves as a unique identifier for the application.
It describes the components of the application � the activities, services, broadcast receivers, and content providers that the application is composed of. It names the classes that implement each of the components and publishes their capabilities (for example, which Intent messages they can handle). These declarations let the Android system know what the components are and under what conditions they can be launched.
It determines which processes will host application components.
It declares which permissions the application must have in order to access protected parts of the API and interact with other applications.
It also declares the permissions that others are required to have in order to interact with the application's components.
It lists the Instrumentation classes that provide profiling and other information as the application is running. These declarations are present in the manifest only while the application is being developed and tested; they're removed before the application is published.
It declares the minimum level of the Android API that the application requires.
It lists the libraries that the application must be linked against.
18.2 Structure of the AndroidManifest.xml File
The diagram below shows the general structure of the manifest file and every element that it can contain. Each element, along with all of its attributes, is documented in full in a separate file.
All the elements that can appear in the manifest file are listed below in alphabetical order. These are the only legal elements; you cannot add your own elements or attributes.
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
Wednesday, July 21, 2010
Android Tutorial
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