How to check programmatically if an application is installed or not in Android?


Introduction

Many times in android applications we have to take help of some other applications from our application. We have to open another application from our application. For example if we want to open a Google map from our application to show the route between two locations. Before opening the Google Maps we have to firstly check whether the Google Maps application is currently installed on the user device. In this article we will take a look at How we can check whether the application is installed or not.

Implementation

We will be creating a simple application in which we will be creating a simple textview for displaying the heading of our application. After that we are creating a button which we will use to check whether Google Maps is installed in an android device.

Step 1: Creating a new project in Android Studio

Navigate to Android studio as shown in below screen. In the below screen click on New Project to create a new Android Studio Project.

After clicking on New Project you will get to see the below screen.

Inside this screen we have to simply select Empty Activity and click on Next. After clicking on next you will get to see the screen below.

Inside this screen we have to simply specify the project name. Then the package name will be generated automatically.

Note − Make sure to select the Language as Java.

After specifying all the details click on Finish to create a new Android studio project.

Once our project has been created we will get to see 2 files which are open i.e activity_main.xml and MainActivity.java file.

Step 2: Working with activity_main.xml

Navigate to activity_main.xml. If this file is not visible. To open this file. In the left pane navigate to app>res>layout>activity_main.xml to open this file. After opening this file. Add the below code to it. Comments are added in the code to get to know in detail.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">
   <!-- creating a text view on below line-->
   <TextView
       android:id="@+id/idTVHeading"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_centerInParent="true"
       android:layout_marginStart="20dp"
       android:layout_marginTop="20dp"
       android:layout_marginEnd="20dp"
       android:layout_marginBottom="20dp"
       android:padding="4dp"
       android:text="Check weather Application is installed or not in Android"
       android:textAlignment="center"
       android:textColor="@color/black"
       android:textSize="20sp"
       android:textStyle="bold" />
   <!-- creating a button on below line -->
   <Button
       android:id="@+id/idBtnCheckMaps"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_below="@id/idTVHeading"
       android:layout_margin="15dp"
       android:text="Check Google Maps Application"
       android:textAllCaps="false" />
</RelativeLayout>

Explanation: In the above code we are creating a root layout as a Relative Layout. Inside this layout we are creating a text view for displaying the heading of our application. After that we are creating a button which we will be using to check weather Google Maps is installed on our device or not.

Step 3: Working with MainActivity.java file

Navigate to MainActivity.java. If this file is not visible. To open this file. In the left pane navigate to app>res>layout>MainActivity.java to open this file. After opening this file. Add the below code to it. Comments are added in the code to get to know in detail.

package com.example.java_test_application;

import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

   // creating variables on below line for text view.
   private Button checkAppBtn;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       // initializing variables on below line.
       checkAppBtn = findViewById(R.id.idBtnCheckMaps);

       // on below line adding click listener
       checkAppBtn.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               // on below line we are checking if the google maps is installed or not by specifying package name of google maps.
               if (checkInstallation(MainActivity.this, "com.google.android.apps.maps")) {
                   // on below line displaying a toast message if maps is installed.
                   Toast.makeText(MainActivity.this, "Google Maps is installed on this device..", Toast.LENGTH_SHORT).show();
               } else {
                   // on below line displaying toast message if google maps is not installed.
                   Toast.makeText(MainActivity.this, "Google Maps is not installed on this device..", Toast.LENGTH_SHORT).show();
               }
           }
       });
   }
   public static boolean checkInstallation(Context context, String packageName) {
       // on below line creating a variable for package manager.
       PackageManager pm = context.getPackageManager();
       try {
           // on below line getting package info
           pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
           // on below line returning true if package is installed.
           return true;
       } catch (PackageManager.NameNotFoundException e) {
           // returning false if package is not installed on device.
           return false;
       }
   }
}

Explanation: In the above code firstly we are creating variables for our button. Now we will get to see the onCreate method. This is the default method of every android application. This method is called when the application view is created. Inside this method we are setting the content view i.e the layout file named activity_main.xml to set the UI from that file. Inside the onCreate method we are initializing the button variable with the id which we have given in our activity_main.xml file. After that we are adding an on click listener for our button. Inside the on click listener we are checking whether Google Maps application is installed or not by passing the package name of Google Maps application. Accordingly we are displaying the toast message.

After adding the above code now we have to simply click on the green icon in the top bar to run our application on a mobile device.

Note − Make sure you are connected to your real device or emulator.

Output

Conclusion

In the above tutorial we have taken a look on How to check programmatically if an application is installed or not in Android.

Updated on: 08-May-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements