Changes

Jump to: navigation, search

Android Explicit Intents – An Android Studio Example

91 bytes added, 18:17, 19 January 2015
no edit summary
Launch Android Studio and create a new project, entering ExplicitIntent into the Application name field and ebookfrenzy.com as the Company Domain setting before clicking on the Next button.
On the form factors screen, enable the Phone and Tablet option and set the minimum SDK setting to API 8: Android 2.2 (Froyo). Continue to proceed through the screens, requesting the creation of a blank activity named ActivityA with a corresponding layout named activity_aand a menu resource file named menu_activity_a.
Click Finish to create the new project.
== Designing the User Interface Layout for ActivityA ==
The user interface for ActivityA will consist of a RelativeLayout view containing EditText (Plain Text), TextView and Button views named editText1, textView1 and button1 respectively. Using the Project tool window, locate the activity_a.xml resource file for ActivityA (located under ExplicitIntent -> app -> src -> main -> res -> layout) and double click on it to load it into the Android Studio Designer tool. Either design the layout in Design mode, or switch to Text mode and enter the following XML. Note that the “Ask Question” text displayed on the button view has been extracted to a string resource named ask_text.
If designing the user interface using the Designer tool in Design mode, be sure to edit the XML afterwards to add the onClick handler property for the button1 view, and note the the layout width property of the EditView component has been set to 200dp:
[[Image:android_studio_explicit_intent_activity_aandroid_studio_explicit_intent_activity_a2.png|The Android Studio UI layout for the first activity in an explicit Intent example]]
Figure 32-1
== Creating the Second Activity Class ==
When the “Ask Question” button is touched by the user, an intent will be issued requesting that a second activity be launched into which an answer can be entered by the user. The next step, therefore, is to create the second activity. Within the Project tool window, right-click on the com.ebookfrenzy.explicitintent package name located in ExplicitIntent -> app -> src -> main -> java and select the New -> Activity -> Blank Activity menu option to display the New Blank Activity dialog as shown in Figure 32-2:
[[Image:android_studio_create_new_activity2android_studio_create_new_activity3.png|Adding a new Activity to an Android Studio project]]
Figure 32-2
Enter ActivityB into the Activity Name and Title fields and name the layout and menu files activity_band menu_activity_b. Since this activity will not be started when the application is launched (it will instead be launched via an intent by ActivityA when the button is pressed), it is important to make sure that the Launcher Activity option is disabled before clicking on the Finish button.
== Designing the User Interface Layout for ActivityB ==
[[Image:android_studio_explicit_intent_activity_b2android_studio_explicit_intent_activity_b3.png|The Android Studio UI layout for the second activity in an explicit Intent example]]
Figure 32-3
== Reviewing the Application Manifest File ==
In order for ActivityA to be able to launch ActivityB using an intent, it is necessary that an entry for ActivityB be present in the AndroidManifest.xml file. Locate this file within the Project tool window(app -> manifests), double click on it to load it into the editor and verify that Android Studio has automatically added an entry for the activity:
<pre>
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent;
import android.view.View;

Navigation menu