Book Image

Android 3.0 Application Development Cookbook

By : Kyle Merrifield Mew
Book Image

Android 3.0 Application Development Cookbook

By: Kyle Merrifield Mew

Overview of this book

<p>Android is a mobile operating system that runs on a staggering number of smartphones and tablets. Android offers developers the ability to build extremely rich and innovative applications written using the Java programming language. Among the number of books that have been published on the topic, what&rsquo;s missing is a thoroughly practical, hands-on book that takes you straight to getting your job done without boring you with too much theory.<br /><br />Android 3.0 Application Development Cookbook will take you straight to the information you need to get your applications up and running. This book is written to provide you with the shortest possible route between an idea and a working application. <br /><br />Work through the book from start to finish to become an Android expert, or use it as a reference book by applying recipes directly to your project.<br /><br />This book covers every aspect of mobile app development, starting with major application components and screen layout and design, before moving on to how to manage sensors such as internal gyroscopes and near field communications. Towards the end, it delves into smartphone multimedia capabilities as well as graphics and animation, web access, and GPS. <br /><br />Whether you are writing your first app or your hundredth, this is a book that you will come back to time and time again, with its many tips and tricks on the rich features of Android 3.</p>
Table of Contents (18 chapters)
Android 3.0 Application Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Declaring an activity


Activities and other application components, such as services, are declared in the AndroidManifest XML file. Declaring an activity is how we tell Android about how the activity can be requested, and what code to run when it is requested. For example, an application will usually indicate that at least one activity should be visible as a desktop icon and serve as the main entry point to the application.

Getting ready

As with most recipes, we will be using the Eclipse IDE. If you have not done so already, start up Eclipse and ensure that you have installed the ADT Plugin.

Android projects are built against a target platform or API level. Here we have used API level 8, which corresponds to the Android 2.2 platform (FroYo). It is quite possible to use any level for this task but if you intend to make use of the 'holographic' UI you will need to look at the recipe about optimizing for 3.0 in Chapter 2, Layouts.

How to do it...

The Eclipse Android project wizard is as good a place to start building an application as any and it will automatically generate a manifest file that includes a basic activity declaration:

  1. Run the project wizard. From the Eclipse File menu select New and then Android Project.

  2. Enter the details of your project as you can see in the next screenshot and click on Finish:

  3. Open up the manifest file from the Package Explorer, and then click on the AndroidManifest.xml tab at the bottom to display the code that the IDE has produced.

  4. Within the <activity> element, find the following attributes:

    android:name=".DeclaringAnActivity"
    android:label="@string/app_name"
  5. Edit the code so that it matches the following snippet:

    <activity
      android:name=".DeclaringAnActivity"
      android:label="Welcome to the Android 3.0 Cookbook"
      android:screenOrientation="portrait">
      ...
    </activity>
  6. Run the application on a device or emulator. The title bar and screen orientation now reflect the changes that we have made. If you have not done this before, instructions can be found at http://developer.android.com/guide/developing/building/building-eclipse.html.

Note

Note that the use of string literals, as in "Welcome to the Android 3.0 Cookbook", is not considered good practice, as it makes translation next to impossible. String constants should be defined in a separate XML file; a literal is used here (and elsewhere in the book) only to simplify examples.

How it works...

An activity represents a single task that the user can perform, such as editing some text or selecting a media file from a list. Each of our activities must be declared in the AndroidManifest XML file, which resides in the root directory of the project.

The project wizard provides us with two basic attributes:

  • The name DeclaringAnActivity refers to the Java subclass that will contain our activity's methods and fields.

  • The label app_name acts as a title for our application. It is displayed on the title bar of the device at runtime and also as the text under the application icon.

We also added an attribute of our own, screenOrientation, which does exactly what you might expect it to.

The manifest is used to control an activity's start-up state and to apply features such as themes, or as just demonstrated, screen orientation. As we will see later though, most attributes can be set and changed dynamically through Java code as well.

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.