Ex02 - Multiple Activities
Ex02 - Multiple Activities
Assumptions
Development Environment for Android (Java SDK, Eclipse, Android SDK) has been setup
successfully.
You are familiar with using Eclipse.
Android SDK 4.x is available and Android Virtual Devices are already created.
Start the Android Virtual Device to save time.
o Click on Window Android SDK and AVD Manager.
o Select an Android 4.x compatible AVD and click on Start
o Select Scale display to real size and provide a Screen Size (in) as 5 inches or any
other appropriate size for your development machine.
7.
8.
9.
10.
11.
12.
2. Define the Layout for MainActivity. This will consist of a two buttons which will launch the
second activity and third activity i.e. ViewSubjectsActivity and AboutAppActivity
Go to res/layout and open the activity_main.xml. Enter the following content in the
activity_main.xml file (You can simply copy this):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:layout_gravity="center_horizontal" />
<Button android:layout_height="wrap_content" android:id="@+id/btnViewSubjects"
android:text="@string/viewsubjects_label"
android:layout_width="match_parent"></Button>
<Button android:layout_height="wrap_content" android:id="@+id/btnAboutApp"
android:text="@string/aboutapp_label"
android:layout_width="match_parent"></Button>
</LinearLayout>
package com.mindstorm.multipleactivities;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
}
}
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is About App Activity"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/txtDataPassed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
}
}
On clicking the Subjects button, you should see the second activity screen come up as shown below:
Similarly, when you click on the About App button, the About Activity screen will come up.
Summary
This hands-on exercise demonstrated how you can have multiple activities (screens) in your Android
application. The pattern is simple. Define the Activity class and its layout. Ensure that the Activities
are defined in the AndroidManifest.xml file. And launch the activity by explicitly providing the
Activity class in the Intent and starting it with startActivity method.