Deep Linking in Android with Example
Last Updated :
06 Jan, 2025
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:android="http://schemas.android.com/apk/res/android"
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"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
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.
Similar Reads
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 usin
5 min read
OpenIntents in Android with Example OI refers to the "OpenIntents" project in Android are a way for one app to request an action from another app. This can be done using either explicit or implicit intents, allowing them to share functionality. The OpenIntents project provides a set of commonly-used intents that can be used by develop
4 min read
Line Graph View in Android with Example If you are looking for a view to represent some statistical data or looking for a UI for displaying a graph in your app then in this article we will take a look on creating a line graph view in our Android App using the GraphView library. What we are going to build in this article? We will be buildi
3 min read
Context Menu in Android with Example In Android, there are three types of menus available to define a set of options and actions in the Android apps. Here in this article let's discuss the detail of the Context Menu. In Android, the context menu is like a floating menu and arises when the user has long-pressed or clicked on an item and
4 min read
Android ListView in Java with Example A ListView in Android is a type of AdapterView that displays a vertically scrollable list of items, with each item positioned one below the other. Using an adapter, items are inserted into the list from an array or database efficiently. For displaying the items in the list method setAdaptor() is use
3 min read
TreeView in Android with Example If you are looking for new UI designs to represent huge data, then there are so many ways to represent this type of data. You can use pie charts, graphs, and many more view types to implement these views. For displaying such huge data then we can prefer using a TreeView. TreeView is similar to that
4 min read
Internal Storage in Android with Example The aim of this article is to show users how to use internal storage. In this article will be creating an application that can write data to a file and store it in internal storage and read data from the file and display it on the main activity using TextView. Saving and loading data on the internal
5 min read
PhotoView in Android with Example In this article, PhotoView is added to android. PhotoView aims to help produce an easily usable implementation of a zooming Android ImageView using multi-touch and double-tap. Besides that, it has many more features like it notifying the application when the user taps on the photo or when the displa
2 min read
TextView widget in Android with Examples Widget refers to the elements of the UI (User Interface) that help the user interact with the Android App. TextView is one of many such widgets which can be used to improve the UI of the app. TextView refers to the widget which displays some text on the screen based on the layout, size, colour, etc
5 min read
Android Sensors with Example In our childhood, we all have played many android games like Moto Racing and Temple run in which by tilting the phone the position of the character changes. So, all these happen because of the sensors present in your Android device. Most Android-powered devices have built-in sensors that measure mot
4 min read