Kotlin Create Android App Shortcut Example

Kotlin Create Android App Shortcut Example

Project Flow Image:
Step 1:
Open AndroidManifest.xml and add following code.
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kotlinkatta.demo">
<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.Demo">
<activity
android:name=".AppShortcutActivity"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />
</activity>
</application>
</manifest>
Step 2:
Create a new resource file : res/xml/shortcuts.xml and add following code.
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:shortcutId="compose"
android:enabled="true"
android:icon="@drawable/ic_baseline_app_shortcut_24"
android:shortcutShortLabel="@string/app_name"
android:shortcutLongLabel="@string/app_name"
android:shortcutDisabledMessage="@string/app_name">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="com.kotlinkatta.demo"
android:targetClass="com.kotlinkatta.demo.AppShortcutActivity" />
<categories android:name="android.shortcut.conversation" />
</shortcut>
</shortcuts>
Step 3:
Create a new Activity class : AppShortcutActivity.kt and add following code.
package com.kotlinkatta.demo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class AppShortcutActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_app_shortcut)
}
}
Step 4:
Open layout xml : activity_app_shortcut.xml and add following code.
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AppShortcutActivity">

<TextView
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="22dp"
android:gravity="center"
android:text="App Shortcut Activity"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
Output:

Comments