Open In App

How to Check if Application is Installed in Your Android Phone and Open the App?

Last Updated : 29 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to check if a particular app is installed on our phones or not. If yes then we will be having an option to open the app. Else it will show a toast message saying Not available. So here we are going to learn how to implement that feature. Note that we are going to implement this project using the Java language.  

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 

XML




<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="16sp"
    tools:context=".MainActivity">
 
    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="200dp"
        android:hint="Type a Package Name"
        app:layout_constraintTop_toTopOf="parent" />
 
    <Button
        android:id="@+id/check"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="300dp"
        android:text="Check"
        app:layout_constraintTop_toTopOf="parent" />
 
    <Button
        android:id="@+id/open"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="350dp"
        android:text="Open"
        android:visibility="invisible"
        app:layout_constraintTop_toTopOf="parent" />
 
</androidx.constraintlayout.widget.ConstraintLayout>


Step 3: Working with the MainActivity.java file

Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.

Java




package com.example.checkifappisinstalled;
 
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
 
import androidx.appcompat.app.AppCompatActivity;
 
public class MainActivity extends AppCompatActivity {
     
    EditText editText;
    Button check, open;
    boolean flag = false;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        check = findViewById(R.id.check);
        open = findViewById(R.id.open);
        editText = findViewById(R.id.name);
        check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // check if app is available or not
                if (available(editText.getText().toString())) {
                    Toast.makeText(MainActivity.this, "Available", Toast.LENGTH_LONG).show();
                    // if available se flag as true
                    flag = true;
                    setvalue(flag);
                } else {
                    flag = false;
                    setvalue(flag);
                    Toast.makeText(MainActivity.this, "Not Available", Toast.LENGTH_LONG).show();
                }
            }
        });
        if (flag) {
            open.setVisibility(View.VISIBLE);
        } else {
            open.setVisibility(View.INVISIBLE);
        }
        open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // open the app using the package name
                Intent intent = getPackageManager().getLaunchIntentForPackage(editText.getText().toString());
                if (intent != null) {
                    startActivity(intent);
                }
            }
        });
    }
 
    private void setvalue(boolean flag) {
        if (flag) {
            open.setVisibility(View.VISIBLE);
        } else {
            open.setVisibility(View.INVISIBLE);
        }
    }
 
    // check with the package name
    // if app is available or not
    private boolean available(String name) {
        boolean available = true;
        try {
            // check if available
            getPackageManager().getPackageInfo(name, 0);
        } catch (PackageManager.NameNotFoundException e) {
            // if not available set
            // available as false
            available = false;
        }
        return available;
    }
 
    @Override
    protected void onStart() {
        super.onStart();
 
    }
}


Step 4: Steps to find the package name of an Android app

We will be requiring the package name of an app to search for the app.

On your PC/Mac:

Step 1: Open https://play.google.com/store in your web browser.

Step 2: Type the name of the app in the search bar to look for the app.

Step 3: Open the app page and look at the URL. The package name forms the end part of the URL i.e. after the id=?. For example for GeeksforGeeks android app it is free.programming.programming.

For Android mobile device:

Step 1: Click on the (share) button.

Step 2: Use it and share the Play Store app link to any service from where you can select and copy text

The app package name is at the end of the app link after id=

Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads