The Talent500 Blog
Introductions to deep linking in Android 1

Introductions to deep linking in Android

In android development, deep linking is an important feature. As people are using applications for a personalized experience, it becomes necessary that mobile development brings personalization to the process.

Google introduced deep linking for android development as a way for applications to gather data inside the database as a URL link. If you are an android developer, it’s essential to understand how deep linking and android functions. This article explores the implementation of deep links in an android app.

What is a deep Link?

A deep Link is a URL generated by the application based on the user’s past behavior. The app opens with a specific activity or screen when the user clicks on the deep link. We can customize the deep Link URL to send a message to applications with predefined parameters. For example, using a deep link in WhatsApp, we can create a URL that sends a specific message to a phone number when the link is correct. Deep links are useful in providing direct access to specific screens or tasks of an application with a URL link.

How to create deep links? 

A practical implementation of deep link can be a simple application that displays a particular message when the URL is clicked. Here is a video of the working example:

https://media.geeksforgeeks.org/wp-content/uploads/20210128101827/Screenrecorder-2021-01-28-10-14-37-248.mp4

To create a new project in Android Project, refer to this article on How to Create New Project in Android Studio. The code can be implemented in both Java and Kotlin programming languages for Android.

Step 1: Create an activity_main.xml file 

The first step is to create an activity_main.xml for your android project. You can use Java or Kotlin programming language for this example.

The activity_main.xml code will be like this:

<?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 2: Create an AndroidManifest.xml file

The next step is to create an AndroidManifest.xml file because we will add a deep link to the MainActivity file. Pay attention to the comments to understand what the code does: 

<?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 3: Configure the MainActivity File

Finally, we will make changes to the MainActivity File in Java or Kotlin language as follows: 

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

}

}

}

The output 

As you run the app, you will be greeted by a screen for the organization’s name. Now close the application and click on the link “https://www.chaitanyamunje.com/hello/GeeksForGeeks.” A pop-up will appear, asking you to select the application. Inside the pop-up, select the application you just created. The message “GeeksForGeeks” will be displayed in place of the Organization name.

When to use deep links?

Deep links produce a seamless user journey that increases the likelihood of app installation. Users are directed to the installation page with a single click. These links are also useful in creating app content that makes incentivizing easier. Companies use deep links to increase conversion rates and retention rates.

Conclusion 

In android development, deep links play an important role in improving user experience. We hope this guide on deep linking and android will help you anticipate the importance of the concept for improving your mobile development skills.

Talent 500 is a distributed team-building platform for Fortune 500 companies and fast-growing start-ups. Sign up here to learn more.

0
Manik Sharma

Manik Sharma

Manik Sharma specializes primarily in UI or Software Development using Javascript libraries like React and Redux along with HTML, CSS, and other libraries like Bootstrap, Node.js, Express.js, MongoDB. He loves to talk business and think of cool startup ideas. Definitely, an entrepreneur in making. He is equally interested in discussing innovative ideas that can make a huge difference in someone's life.

Add comment