Android : Simple example of Intent


In this post we will see how to write a simple android application to show usage of Intent.

Steps to be performed :

  • Add a new project
  • Open main.xml and drag and drop three Button controls and then rename their ids.
    Or, Clear all the contents and paste the code provided below.

    
    <?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" >
    
     <Button
     android:id="@+id/btnGallery"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="Show My Gallery" />
    
     <Button
     android:id="@+id/btnCallLog"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="Show My Call Log" />
    
     <Button
     android:id="@+id/btnContact"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="Show My Contacts" />
    
     <Button
     android:id="@+id/btnSearch"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="Show My Search" />
    
    </LinearLayout>
    
  • Now lets code for these 4 buttons.
  • Open your Activity file.
  • Now code for each button like the code provided below.
    </pre>
    package com.suvendu.tutorial;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    public class IntentDemoActivity extends Activity {
     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
    
     Button myGalleryBtn=(Button)findViewById(R.id.btnGallery);
     Button myCallLogBtn=(Button)findViewById(R.id.btnCallLog);
     Button myContactBtn=(Button)findViewById(R.id.btnContact);
     Button mySearchBtn=(Button)findViewById(R.id.btnSearch);
    
     myGalleryBtn.setOnClickListener(new Button.OnClickListener(){
    
    @Override
     public void onClick(View v) {
    
     Intent myIntent=new Intent();
     myIntent.setAction(Intent.ACTION_VIEW);
     myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
     startActivity(myIntent);
    
     }});
    
     myCallLogBtn.setOnClickListener(new Button.OnClickListener(){
    
    @Override
     public void onClick(View v) {
    
     Intent myIntent=new Intent();
     myIntent.setAction(Intent.ACTION_CALL_BUTTON);
     startActivity(myIntent);
    
     }});
     myContactBtn.setOnClickListener(new Button.OnClickListener(){
    
    @Override
     public void onClick(View v) {
    
     Intent myIntent=new Intent();
     myIntent.setAction(Intent.ACTION_VIEW);
     myIntent.setData(android.provider.Contacts.People.CONTENT_URI);
     startActivity(myIntent);
    
     }});
     mySearchBtn.setOnClickListener(new Button.OnClickListener(){
    
    @Override
     public void onClick(View v) {
    
     Intent myIntent=new Intent();
     myIntent.setAction(Intent.ACTION_SEARCH);
     startActivity(myIntent);
    
     }});
    
     }
    }
    

    [Note: You have to change the package name with your original package name, you provided at the time of creating the project. Similarly, you have to change the class name too.]

Result :

Clicking on the ‘Show My Gallery’ button should open your phone’s gallery.

Clicking on ‘Show My CallLog’ button will open you call log.

Clicking on ‘Show My Contact’ will open your Contact Book.

Clicking on ‘Show My Search’ will open action window to perform search using different options.

Downloads :

If you want to download the code, you can download it here Download

If you want see it quickly you can download it here Download

References :

If you are a beginner then you may also like following articles-