Published using Google Docs
105-Programmr: Android Intent and PutExtra
Updated automatically every 5 minutes

105-Programmr: Android Intent and PutExtra

An Intent is a messaging object that that you can use to request an action from another App Component (Activities, Services, Content Providers and Broadcast Receivers).

An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.

Further Reading:

http://developer.android.com/reference/android/content/Intent.html 

http://developer.android.com/guide/components/intents-filters.html 

http://www.tutorialspoint.com/android/android_intents_filters.htm 

0) Starting Up

Continue from the previous tutorial,http://android-steps.blogspot.my/2015/01/104-programmr-events-and-actions-in.html  .

The required files are as follows. In case you have difficulties to create the files, you can download source Codes here as well.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

      package="com.example.MyAndroid"

      android:versionCode="1"

      android:versionName="1.0">

    <application android:label="MyAndroid" android:icon="@drawable/ic_launcher">

        <activity android:name="MyAndroid"

                  android:label="MyAndroid">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

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

</manifest>

MyAndroid.java

package com.example.MyAndroid;      

       

import android.app.Activity;      

import android.os.Bundle;      

import android.view.View;

import android.widget.Toast;

       

public class MyAndroid extends Activity      

{      

    /** Called when the activity is first created. */      

    @Override      

    public void onCreate(Bundle savedInstanceState)      

    {      

        super.onCreate(savedInstanceState);      

        setContentView(R.layout.main);

    }      

    public void showAbout(View v){

        Toast.makeText(getApplicationContext(), "About", Toast.LENGTH_LONG).show();

    }

   

}        

res/layout/main.java

<?xml version="1.0" encoding="utf-8"?>    

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    

    android:orientation="vertical"      

    android:layout_width="match_parent"    

    android:layout_height="match_parent"

    android:gravity="center_horizontal"

    android:background="@drawable/bgcloud"

    >    

<ImageView

    android:layout_width="wrap_content"    

    android:layout_height="wrap_content"  

    android:src="@drawable/smiley"

    />

<TextView    

    android:layout_width="wrap_content"    

    android:layout_height="wrap_content"  

    android:text="MyAndroid"    

    />

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    

    android:orientation="vertical"      

    android:layout_width="match_parent"    

    android:layout_height="match_parent"

    android:layout_marginLeft="20dp"

    android:layout_marginRight="20dp"    

    >    

<Button    

    android:layout_width="match_parent"    

    android:layout_height="wrap_content"  

    android:text="About"    

    android:onClick="showAbout"

    />        

<Button    

    android:layout_width="match_parent"    

    android:layout_height="wrap_content"  

    android:text="Settings"    

    />            

</LinearLayout>        

</LinearLayout>    

res/drawable

bgcloud.png

ic_launcher/smiley.png

1) Create New Activity

We are going to add two pages ie About and Settings.

In order to add a page, we have to do the following:

1) Create New Activity Layout File

2) Create Java Activity Controller File

3) Update Main Layout

4) Update Main Java Activity Controller File

5) Update Android Manifest

1.1) Create New Activity Layout File (About)

res/layout/activity_about.xml

<?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"    

    >    

<TextView    

    android:layout_width="fill_parent"    

    android:layout_height="wrap_content"    

    android:text="About"    

    />    

</LinearLayout>    

1.2) Create Java Activity Controller File

About.java

package com.example.MyAndroid;      

       

import android.app.Activity;      

import android.os.Bundle;      

       

public class About extends Activity      

{      

    /** Called when the activity is first created. */      

    @Override      

    public void onCreate(Bundle savedInstanceState)      

    {      

        super.onCreate(savedInstanceState);      

        setContentView(R.layout.activity_about);

    }      

}    

1.3) Update Main Layout

res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>    

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    

    android:orientation="vertical"      

    android:layout_width="match_parent"    

    android:layout_height="match_parent"

    android:gravity="center_horizontal"

    android:background="@drawable/bgcloud"

    >    

<ImageView

    android:layout_width="wrap_content"    

    android:layout_height="wrap_content"  

    android:src="@drawable/smiley"

    />

<TextView    

    android:layout_width="wrap_content"    

    android:layout_height="wrap_content"  

    android:text="MyAndroid"    

    />

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    

    android:orientation="vertical"      

    android:layout_width="match_parent"    

    android:layout_height="match_parent"

    android:layout_marginLeft="20dp"

    android:layout_marginRight="20dp"    

    >    

<Button    

    android:layout_width="match_parent"    

    android:layout_height="wrap_content"  

    android:text="About"    

    android:onClick="showAbout"

    />        

<Button    

    android:layout_width="match_parent"    

    android:layout_height="wrap_content"  

    android:text="Settings"    

    />            

</LinearLayout>        

</LinearLayout>    

1.4) Update Main Java Activity Controller File

MyAndroid.java

package com.example.MyAndroid;      

       

import android.app.Activity;      

import android.os.Bundle;      

import android.view.View;

import android.widget.Toast;

import android.content.Intent;

       

public class MyAndroid extends Activity      

{      

    /** Called when the activity is first created. */      

    @Override      

    public void onCreate(Bundle savedInstanceState)      

    {      

        super.onCreate(savedInstanceState);      

        setContentView(R.layout.main);

    }      

    public void showAbout(View v){

        //Toast.makeText(getApplicationContext(), "About", Toast.LENGTH_LONG).show();

        startActivity(new Intent(this, About.class));

    }   

}

The Toast statement is commented as it is not needed anymore.

However the code stays there in case in the future it might be used again.

1.5) Update Android Manifest

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

      package="com.example.MyAndroid"

      android:versionCode="1"

      android:versionName="1.0">

    <application android:label="MyAndroid" android:icon="@drawable/ic_launcher">

        <activity android:name="MyAndroid"

                  android:label="MyAndroid">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

        <activity android:name="About"

                  android:label="About">        

        </activity>

    </application>

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

</manifest>

OUTCOME.

2) Sending data via Intent

Besides calling an activity, we can also use Intent object to pass data from the calling activity.

This is done by adding putExtra method.

We will add a second page, Settings and send a string data to the activity.

Repeat the steps in Step 1 above to add Settings activity.

2.1) Create New Activity Layout File (Settings)

res/layout/activity_settings.xml

<?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"    

    >    

<TextView  

    android:is="@+id/tvw_textview"    

    android:layout_width="fill_parent"    

    android:layout_height="wrap_content"    

    android:text="Settings"    

    />    

</LinearLayout>    

An id is given to the TextView to allow the Controller (Java) File to find and access its text property.

2.2) Create Java Activity Controller File

Settings.java

package com.example.MyAndroid;      

       

import android.app.Activity;      

import android.os.Bundle;      

import android.widget.TextView;

import android.content.Intent;

       

public class Settings extends Activity      

{      

    /** Called when the activity is first created. */      

    @Override      

    public void onCreate(Bundle savedInstanceState)      

    {      

        super.onCreate(savedInstanceState);      

        setContentView(R.layout.activity_settings);

        TextView tvwTextView = (TextView) findViewById(R.id.tvw_textview);

        tvwTextView.setText(getIntent().getStringExtra("com.example.MyAndroid.EXTRA_MESSAGE"));

    }      

}    

In order to work with View Objects, we have to create a TextView object in Java File and bind it to the TextView in the Layout File.

We retrieve the string stored into the Intent object in the previous activity by searching for its key ie com.example.MyAndroid.EXTRA_MESSAGE.

2.3) Update Main Layout

res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>    

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    

    android:orientation="vertical"      

    android:layout_width="match_parent"    

    android:layout_height="match_parent"

    android:gravity="center_horizontal"

    android:background="@drawable/bgcloud"

    >    

<ImageView

    android:layout_width="wrap_content"    

    android:layout_height="wrap_content"  

    android:src="@drawable/smiley"

    />

<TextView    

    android:layout_width="wrap_content"    

    android:layout_height="wrap_content"  

    android:text="MyAndroid"    

    />

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    

    android:orientation="vertical"      

    android:layout_width="match_parent"    

    android:layout_height="match_parent"

    android:layout_marginLeft="20dp"

    android:layout_marginRight="20dp"    

    >    

<Button    

    android:layout_width="match_parent"    

    android:layout_height="wrap_content"  

    android:text="About"    

    android:onClick="showAbout"

    />        

<Button    

    android:layout_width="match_parent"    

    android:layout_height="wrap_content"  

    android:text="Settings"  

    android:onClick="showSettings"  

    />            

</LinearLayout>        

</LinearLayout>    

2.4) Update Main Java Activity Controller File

MyAndroid.java

package com.example.MyAndroid;      

       

import android.app.Activity;      

import android.os.Bundle;      

import android.view.View;

import android.widget.Toast;

import android.content.Intent;

       

public class MyAndroid extends Activity      

{      

    /** Called when the activity is first created. */      

    @Override      

    public void onCreate(Bundle savedInstanceState)      

    {      

        super.onCreate(savedInstanceState);      

        setContentView(R.layout.main);

    }      

    public void showAbout(View v){

        //Toast.makeText(getApplicationContext(), "About", Toast.LENGTH_LONG).show();

        startActivity(new Intent(this, About.class));

    }   

    public void showSettings(View v){

    Intent iSettings = new Intent(this, Settings.class);

    iSettings.putExtra("com.example.MyAndroid.EXTRA_MESSAGE", "My Settings");

    startActivity(iSettings);

    }      

}

2.5) Update Android Manifest

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

      package="com.example.MyAndroid"

      android:versionCode="1"

      android:versionName="1.0">

    <application android:label="MyAndroid" android:icon="@drawable/ic_launcher">

        <activity android:name="MyAndroid"

                  android:label="MyAndroid">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

        <activity android:name="About"

                  android:label="About">        

        </activity>

        <activity android:name="Settings"

                  android:label="Settings">        

        </activity>

    </application>

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

</manifest>

OUTCOME.

Take note of the following:

1) Compare Step 1.4 and 2.4. We write the codes in different ways but they achieve the same objective, ie to start an activity.

2) In Step 2.4, we use putExtra() method to add a string into the Intent object. We also use the package name+”EXTRA_MESSAGE” as an identifier to the string so that the key is unique and searchable by the next activity.

3) In Step 2.2, we retrieve the string value by using getIntent().getStringExtra(...)

DOWNLOAD

Completed Codes