Android - Simple User Interface - Activity, Intent, Extension and Extras

Google provides a good tutorial called 'Building a Simple User Interface' for beginning Android development, the first part of which is intuitive and informative. In this post I look at the extension of Google's code to incorporate an activity.

The activity involves sending and displaying the string entered in the text field used in the tutorial, and is split across three files, which are all contained in the src folder of the app:

  1. src->com->example->simpleuserinterface->MainActivity.java
  2. src->com->example->simpleuserinterface->DisplayMessageActivity.java
  3. src->com->example->simpleuserinterface->AndroidManifest.xml

And here they are in order with comments added. The first (ActivityMain) is the heart of the app.

package com.example.simpleuserinterface;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;


public class MainActivity extends Activity {
  // Public constant attached for the purposes of the later putExtra() method
// "static" means that it belongs to the class not an instance and "final" means it cannot be subclassed and thereby changed
  
 public final static String EXTRA_MESSAGE = "com.example.simpleuserinterface.MESSAGE";
    
 // Regular system code automatically created begins
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
   // Regular system code automatically created ends
    
    /** Called when the user clicks the Send button */
    public void sendMessage(View view) {

        // A new Intent is created, the method for which will be declared in DisplayMessageActivity - created as a new Java file in src->com->example->simpleuserinterface 
     Intent intent = new Intent(this, DisplayMessageActivity.class);

        // Create a pointer to the EditText field and typecast the view
     EditText editText = (EditText) findViewById(R.id.edit_message);

     // Create a string called message, the contents of which is the editText text converted to a string
        String message = editText.getText().toString();
        
// putExtra is a way of sending a string to another area of the app through an Intent (The name must include a package prefix, for example the app com.android.contacts would use names like "com.android.contacts.ShowAll")
        intent.putExtra(EXTRA_MESSAGE, message);

        // startActivity triggers the Intent that has been set up
        startActivity(intent);
    }    
}

The second (DisplayMessageActivity) is the one that the Intent, binding the intended class and content detail of our activity, is sent to:

package com.example.simpleuserinterface;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class DisplayMessageActivity extends Activity {
 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);

     // Get the Intent that started this activity
     Intent intent = getIntent();
  
   // Retrieve the extra part of the EXTRA_MESSAGE string that was attached with putExtra() in the MainActivity.java file
     String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

     // Create the text view
     TextView textView = new TextView(this);

      // Set the size of the text
     textView.setTextSize(40);

      // Set the text of the message
     textView.setText(message);

     // Set the text view as the activity layout
     setContentView(textView);

 }}
And finally we have the xml manifest file that records the existence of the activity and gives it a label in the form of a string:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.simpleuserinterface"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <activity
        android:name=".DisplayMessageActivity"
        android:label="@string/title_activity_display_message" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.SimpleUserInterface.MainActivity" />
    </activity>
    </application>

</manifest>


I've commented the first two of these files and also included all the necessary imports listed. In the third file you are just looking at the final "activity" listed. And note that wherever a string is declared it must also be recorded in the res->values->strings.xml file.

The purpose of this post is to act as a reminder of how each part of the app relates to the other, and to demonstrate how things are linked to one another.

Comments