Android Intent Example

An intent is an abstract description of an operation to be performed. Its most significant use is the launching of activities, where it can be thought of as the glue between activities.
How it works?
1. Drag 3 .png files to “res/layout/drawable-hdpi” (ex. facebook.png, phone_blink2.png, phone.png)

2. Open “res/layout/main.xml” file, add a three ImageView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
       
        android:text="@string/hello" />
 <LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:gravity="center"
     android:layout_gravity="center"
     android:orientation="horizontal">
    <ImageView
       android:layout_width="60dp"
       android:layout_height="60dp"
       android:src="@drawable/facebook"
       android:id="@+id/imgfb"/>
    <ImageView
       android:layout_width="60dp"
       android:layout_height="60dp"
       android:src="@drawable/phone"
       android:id="@+id/imgcall"/>
    <ImageView
       android:layout_width="60dp"
       android:layout_height="60dp"
       android:src="@drawable/phone_blink2"
       android:id="@+id/contact”/>
</LinearLayout> 
</LinearLayout>

3. Inside activity “onCreate()” method, attach a listener on imageview. Read the code’s comment, it should be self-explanatory.

package fb.jer.facebook;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class MyFacebookActivity extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
   
    ImageView ifb,icall,icontact;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        ifb=(ImageView) findViewById(R.id.imgfb);
        icall=(ImageView)findViewById(R.id.imgcall);
        icontact=(ImageView)findViewById(R.id.contact);
       
        ifb.setOnClickListener(this);
        icall.setOnClickListener(this);
        icontact.setOnClickListener(this);
    }
      
    public void onClick(View v) {
      Intent myIntent;
      switch(v.getId())
    {
        case R.id.imgfb:
        //Following Intent uses built-in task (ACTION_VIEW) to explore a web page
            myIntent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://wwww.facebook.com"));
            startActivity(myIntent);
            break;
        case R.id.imgcall:
        //Following Intent uses built-in task (ACTION_VIEW) to make a phone call
            myIntent = new Intent(Intent.ACTION_VIEW,Uri.parse("tel:09999909394"));
            startActivity(myIntent);
            break;
        case R.id.contact:
        //Following fragments calls an Intent whose job is to invoke a built_in
        //task(ACTION_VIEW) and explore the Contacts available in the phone.
            myIntent = new Intent(Intent.ACTION_VIEW,Uri.parse("content://contacts/people"));
            startActivity(myIntent);
            break;
            }
         }    
}
4. Run the application.
Screenshots
Programming Language: Java + XML
Programming IDE: Eclipse