Android intent filter for a particular file extension?


Introduction

Android Intent Filters are an essential part of the Android operating system. They are used to allow applications to respond to certain system events, such as launching an activity, or receiving a broadcast. In addition to being able to launch activities, Intent Filters can also be used to filter for particular file types.

An Intent filter for a particular file type is a filter that will cause an application to respond to the user's request to open a file with a specific extension. For example, when a user clicks on a .txt file in their file manager, the Android operating system will query the list of installed applications to see if any of them have an intent filter that matches the requested file type. If so, it will launch the application that has the intent filter. In this article we will take a look on How to create an Android intent filter for a particular file extension.

Implementation

We will be creating a simple application in which we will be displaying a simple text view in which we are displaying the heading of our application. Then we are creating two buttons. One button to view and another to send on clicking on this button we will be opening an intent with custom parameters which we are passing through intent filter.

Step 1 − Creating a new project in Android Studio.

Navigate to Android studio as shown in below screen. In the below screen click on New Project to create a new Android Studio Project.

After clicking on New Project you will get to see the below screen.

Inside this screen we have to simply select Empty Activity and click on Next. After clicking on next you will get to see the screen below.

Inside this screen we have to simply specify the project name. Then the package name will be generated automatically.

Note − Make sure to select the Language as Java.

After specifying all the details click on Finish to create a new Android studio project.

Once our project has been created we will get to see 2 files which are open i.e activity_main.xml and MainActivity.java file.

Step 3 − Working with activity_main.xml.

Navigate to activity_main.xml. If this file is not visible. To open this file. In the left pane navigate to app>res>layout>activity_main.xml to open this file. After opening this file. Add the below code to it. Comments are added in the code to get to know in detail.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/idRLLayout"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   tools:context=".MainActivity">

   <!-- text view for displaying  heading of the application -->
   <TextView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_above="@id/idBtnSendMsg"
      android:layout_centerInParent="true"
      android:padding="5dp"
      android:text="Intent Filter in Android"
      android:textAlignment="center"
      android:textAllCaps="false"
      android:textColor="@color/black"
      android:textSize="18sp"
      android:textStyle="bold" />

   <!-- on below line we are creating a button to send message -->
   <Button
      android:id="@+id/idBtnSendMsg"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_centerInParent="true"
      android:layout_margin="20dp"
      android:text="Send Message"
      android:textAllCaps="false" />

   <!-- on below line we are creating a button for view action -->
   <Button
      android:id="@+id/idBtnView"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_below="@id/idBtnSendMsg"
      android:layout_centerInParent="true"
      android:layout_margin="20dp"
      android:text="View Action"
      android:textAllCaps="false" />
</RelativeLayout>

Explanation − In the above code we are creating a Relative layout as a root layout and inside that we are creating a simple text view in which we are displaying a heading of our application. After that we are creating two buttons one button is used for send action and another button is used for view action.

Step 4 − Working with AndroidManifest.xml file.

Navigate to AndroidManifest.xml file and add below code to it. Comments are added in the code to get to know in detail.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools">

   <application
      android:allowBackup="true"
      android:dataExtractionRules="@xml/data_extraction_rules"
      android:fullBackupContent="@xml/backup_rules"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:preserveLegacyExternalStorage="true"
      android:requestLegacyExternalStorage="true"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/Theme.AndroidJAVAApp"
      android:usesCleartextTraffic="true"
      tools:targetApi="31">


      <activity
         android:name=".MainActivity"
         android:exported="true">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

         <!-- on below line creating an intent filter for send action -->
         <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="text/plain" />
         </intent-filter>

         <!-- on below line creating an intent filter for view action -->
         <intent-filter>
            <action android:name="android.intent.action.VIEW" />

               <category android:name="android.intent.category.DEFAULT" />
               <category android:name="android.intent.category.BROWSABLE" />

            <data android:scheme="http" />
         </intent-filter>
      </activity>
   </application>

</manifest>

Step 5 − Working with MainActivity.java file.

Navigate to MainActivity.java. If this file is not visible. To open this file. In the left pane navigate to app>res>layout>MainActivity.java to open this file. After opening this file. Add the below code to it. Comments are added in the code to get to know in detail.

package com.example.androidjavaapp;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

   // on below line creating a variable for button.
   private Button sendBtn, viewBtn;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      // on below line we are initializing variables.
      sendBtn = findViewById(R.id.idBtnSendMsg);
      viewBtn = findViewById(R.id.idBtnView);
      
      // on below line we are adding click listener for send button.
      sendBtn.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            
            // on below line we are creating an intent for send action.
            Intent i = new Intent(Intent.ACTION_SEND);
            
            // on below line we are setting intent type and starting our activity.
            i.setType("text/plain");
            i.putExtra(Intent.EXTRA_EMAIL, "niranjantest@gmail.com");
            i.putExtra(Intent.EXTRA_SUBJECT, "This is a dummy message");
            i.putExtra(Intent.EXTRA_TEXT, "Dummy test message");
            startActivity(i);
         }
      });

      viewBtn.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            
            // on below line we are creating an intent for view action.
            Intent i = new Intent(Intent.ACTION_VIEW);
            startActivity(i);
         }
      });
   }
}

Explanation − In the above code we are firstly creating a variable for the button. After that we will get to see an onCreate method. Inside this method we will get to see a set content view method. This method will load the UI from the activity_main.xml file. After that we are initializing the variable for our button from the id which we have specified inside our activity_main.xml file. After that we are adding a click listener for our button. Inside the click listener for our button we are creating an intent by specifying two actions for each button and then opening this intent by calling a start activity method.

After adding the above code now we have to simply click on the green icon in the top bar to run our application on a mobile device.

Note − Make sure you are connected to your real device or emulator.

Output

Conclusion

In the above article we have taken a look on WHat are intent filters in android and how we can use these intent filters within our android application to open a specific intent.

Updated on: 30-Mar-2023

706 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements