Open In App

Deep Links in Android using Jetpack Compose

Improve
Improve
Like Article
Like
Save
Share
Report

A deep link is a URL that is used to direct users to a specific page or specific activity within the application. We can also pass data to our application with the help of these deep links. In this article, we will take a look at How to implement Deep Links in Android using Jetpack Compose

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. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Adding a new color in the Color.kt file

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

Kotlin




package com.example.newcanaryproject.ui.theme
 
import androidx.compose.ui.graphics.Color
 
val Purple200 = Color(0xFF0F9D58)
val Purple500 = Color(0xFF0F9D58)
val Purple700 = Color(0xFF3700B3)
val Teal200 = Color(0xFF03DAC5)
 
// on below line we are adding different colors.
val greenColor = Color(0xFF0F9D58)


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 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>


Complete code for the AndroidManifest.xml file is given below. 

XML




<?xml version="1.0" encoding="utf-8"?>
    xmlns:tools="http://schemas.android.com/tools">
    
    <queries>
        <intent>
            <action android:name="android.speech.RecognitionService" />
        </intent>
    </queries>
 
    <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.NewCanaryProject"
        tools:targetApi="31">
 
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:label="@string/app_name"
            android:theme="@style/Theme.NewCanaryProject">
            <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

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




package com.example.newcanaryproject
 
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.*
import com.example.newcanaryproject.ui.theme.*
 
class MainActivity : ComponentActivity() {
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            NewCanaryProjectTheme {
                // on below line we are specifying background color for our application
                Surface(
                    // on below line we are specifying modifier and color for our app
                    modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background
                ) {
 
                    // on below line we are specifying theme as scaffold.
                    Scaffold(
 
                        // in scaffold we are specifying top bar.
                        topBar = {
 
                            // inside top bar we are specifying background color.
                            TopAppBar(backgroundColor = greenColor,
 
                                // along with that we are specifying title for our top bar.
                                title = {
 
                                    // in the top bar we are specifying tile as a text
                                    Text(
 
                                        // on below line we are specifying text
                                        // to display in top app bar.
                                        text = "Deep Links in Android",
 
                                        // on below line we are specifying modifier
                                        // to fill max width.
                                        modifier = Modifier.fillMaxWidth(),
 
                                        // on below line we are specifying text alignment.
                                        textAlign = TextAlign.Center,
 
                                        // on below line we are specifying color for our text.
                                        color = Color.White
                                    )
                                })
                        }) {
                        // on below line we are calling deep
                        // links ui method to display ui
                        DeepLinksUI(intent)
                    }
                }
            }
        }
    }
}
 
@Composable
fun DeepLinksUI(intent: Intent) {
 
    // on below line we are creating a variable for deep link
    val deepLinkMsg = remember {
        mutableStateOf("")
    }
 
    // 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.
        deepLinkMsg.value = param
    }
 
    // on below line we are creating a column for our ui.
    Column(
        // in this column we are adding a modifier for our column
        // and specifying max width, height and size.
        modifier = Modifier
            .fillMaxWidth()
            .fillMaxHeight()
            .fillMaxSize()
 
            // on below line we are adding padding
            // from all sides to our column.
            .padding(6.dp),
 
        // on below line we are adding vertical
        // arrangement for our column as bottom
        verticalArrangement = Arrangement.Center,
 
        // on below line we are adding
        // horizontal alignment for our column.
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        // on below line we are displaying a simple text
        Text(
 
            // on below line we are specifying
            // modifier as padding for our text.
            modifier = Modifier.padding(6.dp),
 
            // on below line we are
            // specifying text as normal image.
            text = "Deep Links in Android",
 
            // on below line we are
            // specifying font weight as bold.
            fontWeight = FontWeight.Bold,
 
            // on below line we are setting
            // color for our text
            color = greenColor,
 
            // on below line we are
            // setting font size.
            fontSize = 20.sp
        )
 
        // on below line we are adding a spacer
        Spacer(modifier = Modifier.height(20.dp))
 
 
        // on below line we are displaying a simple text
        Text(
 
            // on below line we are specifying
            // modifier as padding for our text.
            modifier = Modifier.padding(6.dp),
 
            // on below line we are
            // specifying text as normal image.
            text = deepLinkMsg.value,
 
            // on below line we are specifying
            // font weight as bold.
            fontWeight = FontWeight.Bold,
 
            // on below line we are setting
            // color for our text
            color = greenColor,
 
            // on below line we are
            // setting font size.
            fontSize = 20.sp
        )
 
    }
 
}


Now run your application to see the output of it.

Output



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