DeepLink Android Example kotlin

29-Dec-2024

DeepLink Android Example kotlin

Output:


for testing, we put the link in the message app which we want to open in our app








by using the deep links in Android, you can open your app by clicking a link 



1/  Create A New Project: ( DeepLink Android Example )


2/  Modify your AndroidManifest.xml



<?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:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.DeeplinkKotlin"
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" />

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

<data android:scheme="https"/>
<data android:host="tutorialb.com"/>
<data android:pathPrefix="/post"/>
<data android:scheme="http"/>
<data android:host="tutorialb.com"/>
<data android:pathPrefix="/post"/>

</intent-filter>
</activity>
</application>

</manifest>



3/  In the intent filter data tag here you can put your website hostname and the path prefix to open the link in your app



<data android:scheme="https"/>
<data android:host="tutorialb.com"/>
<data android:pathPrefix="/post"/>
<data android:scheme="http"/>
<data android:host="tutorialb.com"/>
<data android:pathPrefix="/post"/>



4/  Modify your activity_main.xml




<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/showDeepLinkText"
android:text="hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>





5/  Modify your MainActvity.kt


to know which link you opened in your app



package com.zissofworks.deeplinkkotlin

import android.os.Bundle
import android.widget.TextView
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat


class MainActivity : AppCompatActivity() {

private lateinit var showDeepLinkText: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}

showDeepLinkText = findViewById(R.id.showDeepLinkText)
val intent = intent
val data = intent.data

if (data != null) {
val deepLinkInfo = data.toString()
showDeepLinkText.text = deepLinkInfo
Toast.makeText(this, "Deep Link Data: $deepLinkInfo", Toast.LENGTH_SHORT).show()
}
}
}







Comments