Android Implicit Intent Example

By | September 6, 2012
In the previous series we have seen the example of explicit intent , here in this article we shall look at implicit intent  

Implicit Intent: – Implicit intent do not directly specify the Android components which should be called. They specify the action which should be performed and optionally an URI which should be used for this action.

Some of the Pr-packaged Application Like

  •  Call a number .
  •  Browse the web for a given URL.
  •  Sending Email to a given email .
  • and many more …

Let’s see an Example of  implicit intent to launch a web browser and open a given URL.

1. XML Layout

       Create XML File
         1)implicit_intent.xml

file:-implicit_intent.xml

< 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:padding=”20dp” >

    < Button
        android:id=”@+id/browse_button_id”
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:layout_gravity=”center”
        android:layout_marginTop=”30dp”
        android:text=”Browse” / >

 < /LinearLayout >

2. Activity

     Create Activity Class
      1) Implicit_intent

Create a class implicit_intent and extend it to activity class and override the onCreate() method of activity class and set the content of the activity with the above defined xml file by calling setContentView() method and passing xml file name as a parameter to it

   setContentView(R.layout.implicit_intent); 

And inside the implicit_intent class set the onclick listener for button and create the implicit intent , and pass the intent object to startActivity method as a paramter to it.

file:- Implicit_intent.java

package com.example.implicit_intent;

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

public class Implicit_intent extends Activity {

    Button browse;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.implicit_intent);

        browse = (Button) findViewById(R.id.browse_button_id);

        // set listener for browse button

        browse.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                // TODO Auto-generated method stub

              Uri uriUrl = Uri.parse(“http://pavanhd.blogspot.in/”);

                Intent browse_intent = new Intent(Intent.ACTION_VIEW, uriUrl);

                startActivity(browse_intent);
            }
        });
    }
}

3. RUN

  Browser Activity Gets Launched

  • Anonymous

    Small thing, if you create a new folder in your res folder and name it "layout_land", you can create landscape layouts (with the same names as their portrait layout in res/layout) and as he will automatically switch between those two layouts without any additional effort in your java class.