Open In App

Android – Deep Linking with Kotlin

Improve
Improve
Like Article
Like
Save
Share
Report

Deep Links are seen in most applications where users can use these links to redirect to any specific screen or activity within the application. We can also pass data using these links within our application. In this article, we will take a look at Implementing Deep Links in Android applications using Kotlin. 

Note: If you are looking to implement Deep Links in Android using Java. Check out the following article: Deep Links in Android using Java

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 

XML




<?xml version="1.0" encoding="utf-8"?>
<!--on below line we are creating a swipe to refresh layout-->
<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:orientation="vertical"
    tools:context=".MainActivity">
  
    <!--text view for displaying heading-->
    <TextView
        android:id="@+id/idTVHead"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/idTVMsg"
        android:layout_margin="4dp"
        android:gravity="center"
        android:padding="4dp"
        android:text="@string/app_name"
        android:textAlignment="center"
        android:textColor="@color/purple_200"
        android:textSize="20sp"
        android:textStyle="bold" />
  
    <!--text view for displaying a message-->
    <TextView
        android:id="@+id/idTVMsg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_margin="20dp"
        android:padding="4dp"
        android:text="Message"
        android:textAlignment="center"
        android:textAllCaps="false"
        android:textColor="@color/purple_200"
        android:textSize="20sp"
        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.kt file so we have to add this code in the MainActivity part. Below is the code which is to be added in 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.gtappdevelopers.kotlingfgproject">
  
    <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.KotlinGFGProject"
        android:usesCleartextTraffic="true">
          
        <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 the MainActivity.kt file

Navigate to app>java>your app’s package name>MainActivity.kt file and add the below code to it. Comments are added in the code to get to know in detail. 

Kotlin




package com.gtappdevelopers.kotlingfgproject
  
import android.net.Uri
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
  
class MainActivity : AppCompatActivity() {
  
    // on below line we are creating 
    // variables for our text view
    lateinit var msgTV: TextView
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
          
        // on below line we are initializing
        // our variable with their ids.
        msgTV = findViewById(R.id.idTVMsg)
  
        // getting the data from our
        // intent in our uri.
        val 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: List<String> = uri.getPathSegments()
  
            // 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.
            msgTV.setText(param)
        }
    }
}


Now run your application to see the output of it. 

Output:



Last Updated : 30 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads