In The Beginning There Was the Root And It Was Good

The root of all manifest files is, not surprisingly, a manifest element < manifest Note the namespace declaration. Curiously, the generated manifests only apply it on the attributes, not the elements (e.g., it's manifest, not android manifest). However, that pattern works, so unless Android changes, stick with their pattern. The biggest piece of information you need to supply on the manifest element is the package attribute (also curiously not-namespaced). Here, you can provide the name of...

Menus of Options

Rather than building your activity's options menu during onCreate(), the way you wire up the rest of your UI, you instead need to implement onCreateOptionsMenu(). This callback receives an instance of Menu. The first thing you should do is chain upward to the superclass (super.onCreateOptionsMenu(menu)), so the Android framework can add in any menu choices it feels are necessary. Then, you can go about adding your own options, described below. If you will need to adjust the menu during your...

Drawing the Overlay

Overlay subclasses need to implement a draw() method to actually put their material onto their layer for superposition over the map surface. The draw() method takes three parameters 1. A Canvas, used as the drawing surface 2. A PixelCalculator, to help you convert between pixels for your drawing and real-world dimensions on the map 3. A boolean indicating whether this is the shadow call or not The draw() method is called twice in succession once with shadow true, indicating that if your layer...

Moving Pictures

Video clips get their own widget, the videoview. Put it in a layout, feed it an MP4 video clip, and you get playback Right now, playback seems a bit rocky in the emulator, but that will likely clear itself up in future releases - videoview was only made available in the SDK release prior to publication of this book. Since videoview is a widget, you can put it in a layout, such as this one from the VideoDemo sample project < xml version 1.0 encoding utf-8 > < Button android id +id show...

Types of Pestering

A service, running in the background, needs a way to users know something of interest has occurred, such as when email has been received. Moreover, the service may need some way to steer the user to an activity where they can act upon the event - reading a received message, for example. For this, Android supplies status bar icons, flashing lights, and other indicators collectively known as notifications. Your current phone may well have such icons, to indicate battery life, signal strength,...

Welcome to the Warescription

We hope you enjoy this ebook and its updates - keep tabs on the Warescription feed off the CommonsWare site to learn when new editions of this book, or other books in your Warescription, are available. All editions of CommonsWare titles, print and ebook, follow a softwarestyle numbering system. Major releases 1.0, 2.0, etc. are available in both print and ebook minor releases 0.1, 0.9, etc. are available in ebook form for Warescription subscribers only. Releases ending in .9 are release...

Making Noise

The crux of playing back audio comes in the form of the MediaPlayer class. With it, you can feed it an audio clip, start stop pause playback, and get notified on key events, such as when the clip is ready to be played or is done playing. You have three ways to set up a MediaPlayer and tell it what audio clip to play 1. If the clip is a raw resource, use MediaPlayer.create and provide the resource ID of the clip 2. If you have a Uri to the clip, use the Uri-flavored version of MediaPlayer.create...

Get Your Media On

In Android, you have five different places you can pull media clips from -one of these will hopefully fit your needs 1. You can package media clips as raw resources res raw in your project , so they are bundled with your application. The benefit is that you're guaranteed the clips will be there the downside is that they cannot be replaced without upgrading the application. 2. You can package media clips as assets assets in your project and reference them via file android_asset URLs in a Uri....

Introspection

Sometimes, you know just what you want to do, such as display one of your other activities. Sometimes, you have a pretty good idea of what you want to do, such as view the content represented by a Uri, or have the user pick a piece of content of some MIME type. Sometimes, you're lost. All you have is a content Uri, and you don't really know what you can do with it. For example, suppose you were creating a common tagging subsystem for Android, where users could tag pieces of content - contacts,...

Example

Let's look at an example Linear that shows LinearLayout properties set both in the XML layout file and at runtime. lt xml version 1.0 encoding utf-8 gt lt LinearLayout gt lt RadioGroup android id id orientation android padding 5px gt lt RadioButton android text horizontal gt lt RadioButton android id id vertical android text vertical gt lt RadioGroup gt lt RadioGroup android id id gravity android padding 5px gt lt RadioButton android id id left android text left gt lt RadioButton android id id...

Craft the Search Activity

The first thing you are going to want to do if you want to support query-style search in your application is to create a search activity. While it might be possible to have a single activity be both opened from the launcher and opened from a search, that might prove somewhat confusing to users. Certainly, for the purposes of learning the techniques, having a separate activity is cleaner. The search activity can have any look you want. In fact, other than watching for queries, a search activity...

Spin Control

In Android, the Spinner is the equivalent of the drop-down selector you might find in other toolkits e.g., JComboBox in Java Swing . Pressing the left and right buttons on the D-pad iterates over children. Pressing the center button on the D-pad displays, by default, a small list akin to a ListView appears to show a few items at a time, instead of the one-item-at-a-time perspective the unexpanded Spinner itself provides. As with ListView, you provide the adapter for data and child views via...

Pick and Choose

With limited-input devices like phones, having widgets and dialogs that are aware of the type of stuff somebody is supposed to be entering is very helpful. It minimizes keystrokes and screen taps, plus reduces the chance of making some sort of error e.g., entering a letter someplace where only numbers are expected . As shown previously, Editview has content-aware flavors for entering in numbers, phone numbers, etc. Android also supports widgets DatePicker, TimePicker and dialogs...

Manifest Destiny

Finally, you need to add the service to your AndroidManifest.xml file, for it to be recognized as an available service for use. That is simply a matter of adding a service element as a child of the application element, providing android name to reference your service class. For example, here is the AndroidManifest.xml file for MailBuzz lt activity android name .MailBuzz android label MailBuzz gt lt intent-filter gt lt action gt lt category gt lt intent-filter gt lt activity gt lt service gt lt...