Check App is Installed or Not Installed – Kotlin | Android Studio

Add kotlin code on your android studio’s project and check app is Installed or not Installed using package name. So you can add some codes on Androidmanifast.xml and MainActivity.kt.

How to Do

Open Androidmanifast.xml file then paste given code –

<queries>
    <!-- Explicit apps you know in advance about: -->
    <package android:name="AppEnterPackageName"/>
    <package android:name="AppEnterPackageName2"/>
    <package android:name="AppEnterPackageName3"/>
</queries>

Then open MainActivity.kt and use these code on your app –

@Suppress("DEPRECATION")
fun isPackageInstalled(packageName: String?, packageManager: PackageManager): Boolean {
    return try {
        packageManager.getApplicationInfo(packageName!!, 0).enabled
    } catch (e: PackageManager.NameNotFoundException) {
        false
    }
}
var TKisInstalled = isPackageInstalled("AppEnterPackageName", this.packageManager)
    if(TKisInstalled ) {
      do_something
    }
    else{
       do_something
    }

View my code for better understanding

Androidmanifast.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">
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@drawable/icon_icon"
        android:label="@string/app_name"
        android:roundIcon="@drawable/icon_icon"
        android:supportsRtl="true"
        android:theme="@style/Theme.tkapp"
        tools:targetApi="31">
        <activity
            android:name=".welcome_screen"
            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.lib_name"
                android:value="" />
        </activity>
        <activity
            android:name=".MainActivity"
            android:exported="true">
        </activity>
    </application>
    <queries>
        <package android:name="AppEnterPackageName"/>
    </queries>
</manifest>

MainActivity.kt

package com.thekroyaard.wadirect

import android.content.Intent
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle


class MainActivity : AppCompatActivity() {

    @Suppress("DEPRECATION")
    fun isPackageInstalled(packageName: String?, packageManager: PackageManager): Boolean {
        return try {
            packageManager.getApplicationInfo(packageName!!, 0).enabled
        } catch (e: PackageManager.NameNotFoundException) {
            false
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        var TKisInstalled = isPackageInstalled("com.whatsapp", this.packageManager)

         if(TKisInstalled ) {
         do_something
                  }
             else{
                  do_something
                   }
    }
}

Use this method and check or add some code when app installed or show toast when app in not installed on device. Thanks for visiting.