Creating an Activity

Extend Activity to create a new Activity class. Within this new class you must define the user interface and implement your functionality. The basic skeleton code for a new Activity is shown in Listing 3-9.

LISTING 3-9: Activity skeleton code

Available for downloadon package com.paad.myapplication; Wrox.com import android.app.Activity; import android.os.Bundle;

public class MyActivity extends Activity {

/** Called when the activity is first created. */ ©Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

The base Activity class presents an empty screen that encapsulates the window display handling. An empty Activity isn't particularly useful, so the first thing you'll want to do is create the user interface with Views and layouts.

Views are the user interface controls that display data and provide user interaction. Android provides several layout classes, called View Groups, that can contain multiple Views to help you design your user interfaces.

Chapter 4 examines Views and View Groups in detail, examining what's available, how to use them, and how to create your own Views and layouts.

To assign a user interface to an Activity, call setContentView from the onCreate method of your Activity. In this first snippet, an instance of a Textview is used as the Activity's user interface:

©Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView textView = new TextView(this); setContentView(textView);

Usually you'll want to use a more complex UI design. You can create a layout in code using layout View Groups, or you can use the standard Android convention of passing a resource ID for a layout defined in an external resource, as shown in the following snippet:

©Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

In order to use an Activity in your application you need to register it in the manifest. Add new <activity> tags within the <application> node of the manifest; the <activity> tag includes attributes for metadata such as the label, icon, required permissions, and themes used by the Activity. An Activity without a corresponding <activity> tag can't be displayed.

The XML in Listing 3-10 shows how to add a node for the MyActivity class created in Listing 3-9.

LISTING 3-10: Activity layout in XML Available for downloadon <activity android:label="@string/app_name" Wrox.com android:name=" .MyActivity">

</activity>

Within the <activity> tag you can add <intent-filter> nodes that specify the Intents your Activity will listen for and react to. Each Intent Filter defines one or more actions and categories that your Activity supports. Intents and Intent Filters are covered in depth in Chapter 5, but it's worth noting that for an Activity to be available from the main application launcher it must include an Intent Filter listening for the main action and the launcher category, as highlighted in Listing 3-11.

LISTING 3-11: Main application Activity definition Available for downloadon <activity android:label="@string/app_name" Wrox.com android:name=".MyActivity">

<intent-filter>

<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


Post a comment