Saturday, November 16, 2024
Google search engine
HomeLanguagesJavaDeep Linking in Android with Example

Deep Linking in Android with Example

Deep Linking is one of the most important features that is used by various apps to gather data inside their apps in the form of a URL link. So it becomes helpful for the users from other apps to easily share the data with different apps. In this article, we will take a look at the implementation of deep links in our Android App. 

What is a Deep Link?

A Deep Link is a URL link that is generated, when anyone clicks on that link our app will be open with a specific activity or a screen. Using this URL we can send a message to our app with parameters. In WhatsApp, we can generate a deep link to send a message to a phone number with some message in it. Deep links are used to open your app’s specific screen with a URL link. 

What are We going to Build in this Article? 

We will be building a simple application in which we will be creating a deep link and on clicking on that link we will be passing our message to our app and displaying that message in a text view. A sample video is given below to get an idea about what we are going to do in this article.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Project just refer to this article on How to Create New Project in Android Studio. The code can be implemented in both Java and Kotlin Programming Language for Android.

Step 2: Working with activity_main.xml

Navigate to app > res > layout > activity_main.xml and add the below code to it. Comments are added in the code to get to know in detail. 

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="5"
    tools:context=".MainActivity">
    <!-- text view for displaying welcome message -->
    <TextView
        android:id="@+id/idTVWelcome"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:padding="10dp"
        android:text="Welcome to "
        android:textAlignment="center"
        android:textAllCaps="false"
        android:textColor="@color/purple_500"
        android:textSize="30sp" />
    <!-- text view for displaying the organization
        name from the link which we have generated -->
    <TextView
        android:id="@+id/idTVMessage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/idTVWelcome"
        android:text="Organization Name"
        android:textAlignment="center"
        android:textAllCaps="false"
        android:textColor="@color/purple_500"
        android:textSize="30sp"
        android:textStyle="bold" />
</RelativeLayout>


Step 3: Working with the AndroidManifest.xml file

Navigate to the app > AndroidManifest.xml and add the below code to it. As we are creating a deep link for our MainActivity file so we have to add this code in the MainActivity part. Below is the code which is to be added to the AndroidManifext.xml file. Comments are added in the code to get to know in more detail. 

XML




<!-- as we want to open main activity from our link so we are specifying
    only in main activity or we can specify that in different activity as well
    on below line we are adding intent filter to our MainActivity -->
  
<intent-filter>
  <!-- below line is to set the action to our intent to view -->
  <action android:name="android.intent.action.VIEW" />
    
  <!-- on below line we are adding a default category to our intent -->
  <category android:name="android.intent.category.DEFAULT" />
    
  <!-- on below line we are adding a category to make our app browsable -->
  <category android:name="android.intent.category.BROWSABLE" />
    
  <!-- on below line we are specifying the host name and
      the scheme type from which we will be calling our app -->
   <data
        android:host="www.chaitanyamunje.com"
        android:scheme="https" />
</intent-filter>
  
<!-- below is the same filter as above just the scheme is changed to http -->
<!-- so we can open our app with the url starting with https and http as well -->
<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:host="www.chaitanyamunje.com"
    android:scheme="http" />
</intent-filter>


Below is the complete code for the AndroidManifest.xml file.

XML




<?xml version="1.0" encoding="utf-8"?>
    package="com.example.deeplinks">
  
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.DeepLinks">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
  
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
  
            <!-- as we want to open main activity from our link so we are specifying
                only in main activity or we can specify that in different activity as well -->
            <!-- on below line we are adding intent filter to our MainActivity -->
            <intent-filter>
                <!-- below line is to set the action to our intent to view -->
                <action android:name="android.intent.action.VIEW" />
                
                <!-- on below line we are adding a default category to our intent -->
                <category android:name="android.intent.category.DEFAULT" />
                
                <!-- on below line we are adding a category to make our app browsable -->
                <category android:name="android.intent.category.BROWSABLE" />
                
                <!-- on below line we are specifying the host name and
                    the scheme type from which we will be calling our app -->
                <data
                    android:host="www.chaitanyamunje.com"
                    android:scheme="https" />
            </intent-filter>
  
            <!-- below is the same filter as above just the scheme is changed to http -->
            <!-- so we can open our app with the url starting with https and http as well -->
            <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:host="www.chaitanyamunje.com"
                    android:scheme="http" />
            </intent-filter>
        </activity>
    </application>
</manifest>


Step 4: Working with MainActivity File

Navigate to app > java > your app’s package name > MainActivity File (Java or Kotlin) and add the below code to it. Comments are added in the code to get to know in detail.

Java




import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.List;
  
public class MainActivity extends AppCompatActivity {
  
    // creating a variable for our text view
    private TextView messageTV;
    private Uri uri;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // initializing our variable
        messageTV = findViewById(R.id.idTVMessage);
        // getting the data from our intent in our uri.
        uri = getIntent().getData();
  
        // checking if the uri is null or not.
        if (uri != null) {
            
            // if the uri is not null then we are getting
            // the path segments and storing it in list.
            List<String> parameters = uri.getPathSegments();
            
            // after that we are extracting string 
            // from that parameters.
            String param = parameters.get(parameters.size() - 1);
            
            // on below line we are setting that string 
            // to our text view which we got as params.
            messageTV.setText(param);
        }
    }
}


Kotlin




import android.net.Uri
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
  
class MainActivity : AppCompatActivity() {
  
    // creating a variable for our text view
    private lateinit var messageTV: TextView
    private var uri: Uri? = null
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // initializing our variable
        messageTV = findViewById(R.id.idTVMessage)
        // getting the data from our intent in our uri.
        uri = intent.data
  
        // checking if the uri is null or not.
        if (uri != null) {
            // if the uri is not null then we are getting
            // the path segments and storing it in list.
            val parameters = uri!!.pathSegments
            
            // after that we are extracting string
            // from that parameters.
            val param = parameters[parameters.size - 1]
            
            // on below line we are setting that
            // string to our text view which
            // we got as params.
            messageTV.text = param
        }
    }
}


Now we have added the URL in our AndroidManifest file as https://www.chaitanyamunje.com/hello/GeeksForGeeks. The URL from which we will send messages to our MainActivity File. In the above URL the “https” is our scheme, “www.chaitanyamunje.com” is our hostname and “hello” is our first parameter and “GeeksForGeeks” is a second parameter which we are going to show in our app as an organization name. You can change your parameters according to your requirement. Now run your app and see the output of the app.

Output:

As you have run your app you will get to see the text as the Organization name, now close the application and click on the link which is shown above from the device on which your application is installed. After clicking on that link a pop-up message will be displayed to select the application. Inside that pop-up message select, your application, and your app will be open. We are passing a message as “GeeksForGeeks” which will be displayed in the place of the Organization name.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments