Android Activities

Almost all the Android applications need multiple screens. This tutorial explains the way to create multiple activities(screens) in Android Studio. An activity is a combination of a Java class and an XML design. This tutorial uses the same project which is used in the last article. Download the template from this link and continue the development. The given template has the complete GUI and partially completed MainActivity.java.

In the last tutorial, we showed a Toast message during the button click event. In this article we are going to create a new activity and display the order in that new activity.

Step 1:
In the imported sample project, right click on the app folder and select New → Activity → Blank Activity.

Android Activities

Step 2:
Modify the activity name as “OrderActivity” and leave the other fields with the default values.

Android Activities

Step 3:
Modify the activity_order.xml as shown below. This layout has four TextViews, among them two text views are used to display the title and other two are used to display the actual message.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.javahelps.sample.activity.OrderActivity">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Order number"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/txtOrderNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginStart="10dp"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Order"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/txtOrder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginStart="10dp"
        android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>

Step 4:
Modify the showMessage method of MainActivity as shown below.
/**
 * Display the message to user.
 *
 * @param number The order number
 * @param order  The descriptive order to display
 */
private void showMessage(String number, String order) {
    // Create an Intent
    Intent intent = new Intent(MainActivity.this, OrderActivity.class);
    // Start the activity
    startActivity(intent);
}

An Intent is an abstract description of an operation to be performed. In this case an Intent is used to start a new activity. For the intent the context and the target activity is provided using the constructor. This type of intents are called explicit intents since the target activity is explicitly provided. startActivity method is used to execute the intent.

Step 5:
Run and check the output. When you click the “Order” button, the new activity will be displayed.

Step 6:
However, we need to display a message in the OrderActivity. Intent provides a facility to pack any necessary information inside the Intent itself and send it to the target activity. Now modify the showMessage method as given below.
/**
     * Display the message to user.
     *
     * @param number The order number
     * @param order  The descriptive order to display
     */
private void showMessage(String number, String order) {
    // Create an Intent
    Intent intent = new Intent(MainActivity.this, OrderActivity.class);
    // Attach the parameters
    intent.putExtra("ORDER_NUMBER", number);
    intent.putExtra("ORDER_MESSAGE", order);
    // Start the activity
    startActivity(intent);
}

The putExtra method of Intent class has several overloaded versions with different parameters. The putExtra method needs a String key and a value related to that key. All the values passed to the putExtra method must be serializable objects or any primitive values.

Step 7:
Sending information is not enough. In the OrderActivity we need to receive the information and display it properly. Create the following instance variables in OrderActivity class to represent runtime objects.
private TextView txtOrderNumber;
private TextView txtOrder;

Step 8:
Find the GUI components using findViewById method as shown here.
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_order);

    // Find the GUI components
    this.txtOrder = (TextView) findViewById(R.id.txtOrder);
    this.txtOrderNumber = (TextView) findViewById(R.id.txtOrderNumber);
}

Step 9:
Get the Intent which is used to start the OrderActivity and extract the passed messages from the Intent. Intent has a sub container Bundle which stores all the provided contents. Using the keys provided in the MainActivity, the stored values can be retrieved. Activity.getIntent() method is used to get the Intent which is used to start the current activity. 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_order);

    // Find the GUI components
    this.txtOrder = (TextView) findViewById(R.id.txtOrder);
    this.txtOrderNumber = (TextView) findViewById(R.id.txtOrderNumber);

    // Get the intent used to start this activity
    Intent intent = getIntent();
    // Get the bundle object from the Intent
    Bundle bundle = intent.getExtras();
    String number;
    String order;
    // Get all the stored parameters
    if (bundle != null) {
        number = bundle.getString("ORDER_NUMBER");
        order = bundle.getString("ORDER_MESSAGE");
    } else {
        number = "Not available";
        order = "Not available.";
    }

    txtOrderNumber.setText(number);
    txtOrder.setText(order);
}

Step 10:
Now run and test the application.

Android Activities

For the completed project click here.

We have created an application with two activities. The first activity starts the second activity with some parameters. Second activity receives those parameters and display them using some TextViews. To check your understanding, try to complete the sample problem.

Sample Problem
A template of login activity is provided here. Download the template and start your development using this template. The GUI and the logic of MainActivity is implemented. You need to create a new activity with a name “DashboardActivity” and display that activity with a welcome message during a successful login. Remember that the username displayed in the Dahsboard Activity must be passed from the MainActivity through an intent. MainActivity's onLoginClicked method has a // TODO: comment; replace it with a suitable implementation.
The final application must look like this:

Android Activities


Find the projects at Git Hub.
Previous
Next Post »

Contact Form

Name

Email *

Message *