Android – Deep Linking with Kotlin
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" ?>
< RelativeLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:fillViewport = "true"
android:orientation = "vertical"
tools:context = ".MainActivity" >
< 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" />
< 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
< 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 = "https" />
</ intent-filter >
< 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 >
< 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 = "https" />
</ intent-filter >
< 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() {
lateinit var msgTV: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
msgTV = findViewById(R.id.idTVMsg)
val uri: Uri? = intent.data
if (uri != null ) {
val parameters: List<String> = uri.getPathSegments()
val param = parameters[parameters.size - 1 ]
msgTV.setText(param)
}
}
}
|
Now run your application to see the output of it.
Output:
Last Updated :
30 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...