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

Switching between activities


Often we will want to activate one activity from within another. Although this is not a difficult task, it will require more setting up than the previous two recipes as it will need two activities to be declared in the Manifest, a new Class to serve as our second activity, and a button along with a click listener to perform the switch.

Getting ready

This recipe can be started from scratch, so create a new Android project in Eclipse and call it ActivitySwitcher. Creating a project with the wizard automatically generates the first of our activities. This example can be built against any platform target and here we have used 2.2 (API level 8).

How to do it...

First we create a new public class that we will use to create the second activity:

  1. Create a new public class in the same location as the original activity subclass using the tool bar's New Java Class icon or the package's context menu from the Package Explorer, selecting New and then Class.

  2. Name the class MySubActivity or something similar and make sure to complete the Superclass field as seen in the following screenshot:

  3. Next, we need to declare our new activity in the manifest file. Open the AndroidManifest.xml file from the Package Explorer and select the Application tab.

  4. Under Application Nodes, click on the Add... button and then select Activity.

  5. In the panel on the right-hand side, fill in the Name field as .MySubActivity and the Label field as my sub activity.

  6. Open the AndroidManifest.xml tab and check whether these changes are reflected in the XML, which should look similar to the following snippet:

    <activity
      android:name=".MySubActivity"
      android:label="my sub activity">
    </activity>
  7. Next, we must add a button that the user can click on to switch activities. This is set up through the main.xml file which resides in the res/layout folder in the Package Explorer.

  8. Open the main.xml file and click on the XML tab at the bottom so that the code can be edited.

  9. Add the following <Button> element just after the <TextView> element that was generated automatically:

    <Button
      android:text="click to switch activities"
      android:id="@+id/main_activity_button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content">
    </Button>
  10. Now open the original Java activity class, ActivitySwitcher or whatever you called it.

  11. Add the following code to the onCreate() method after the setContentView(R.layout.main); statement, making sure to replace the package and class parameters in the setClassName() call with your own, as they will most likely be different:

    Button switchButton = (Button) findViewById(R.id.main_activity_button);
    
    switchButton.setOnClickListener(new OnClickListener() {
    
      @Override
      public void onClick(View v) {
        Intent intent = new Intent();
        String packageName =
          "com.packtpub.android.activityswitcher";
        String className =
          "com.packtpub.android.activityswitcher.MySubActivity";
        intent.setClassName(packageName, className);
        startActivity(intent);
      }
    
    });
  12. Run the application on a device or emulator. Clicking on the button will now start the sub activity.

How it works...

This new activity is not a very exciting application. Our activity does nothing but demonstrate how to switch from one activity to another, which of course will form a fundamental aspect of almost any application that we develop.

In most cases there would be a separate layout declaration alongside main.xml in the res/layout folder for each new activity. Also a button, or some other object, to return us to our original activity would be quite reasonable but these features have been omitted here simply to save us the extra typing, and of course, the user can always use the device's own Back button to achieve this.

We have seen how to create a new subclass for each new activity and how to declare these in the manifest. We have also seen how a UI element such as a button is declared in an XML file, main.xml, and then associated with a data member in Java with the findViewById() method.

Again we have made use of the intent object, not only to start the new activity but also to specify which activity class to run.

See also

To learn more about embedding widgets like the Button, see Chapter 3, Widgets.