How to open the Google Play Store in an Android app?


Introduction

Many times in android applications we can see that OTP received for user authentication is detected automatically and it is added within the application automatically. Users don't have to add the otp manually. This can be done in android applications with the help of broadcast receivers. In this article we will take a look on How to listen for incoming sms messages in android application

Implementation

We will be creating a simple application in which we will be displaying two text views. First text view is for displaying the heading of our application. Second text view we will be creating for displaying the SMS which is being received. Now let's move towards android studio for creating a new project.

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="Listen Incoming SMS Message in Android"
       android:textAlignment="center"
       android:textColor="@color/black"
       android:textSize="20sp"
       android:textStyle="bold" />
   <!--creating a text view on below line-->
   <TextView
       android:id="@+id/idTVMessage"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_below="@id/idTVHeading"
       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="Message will appear here"
       android:textAlignment="center"
       android:textColor="@color/black"
       android:textSize="20sp"
       android:textStyle="bold" />
</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 which is used to display the heading of our application. After that we are creating a button on clicking on that button we will be opening the google play store application. Now let's move towards MainActivity.java file to add functionality for this button.

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.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
   // creating variable for button on below line.
   private Button openPlayBtn;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       // initializing variables for button on below line.
       openPlayBtn = findViewById(R.id.idBtnOpenGooglePlay);
       // adding click listener for button on below line.
       openPlayBtn.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               // on below line we are creating the uri to open google play store to open google maps application
               Uri uri = Uri.parse("https://play.google.com/store/apps/details?id=com.google.android.apps.maps");
               // initializing intent with action view.
               Intent i = new Intent(Intent.ACTION_VIEW, uri);
               // set flags on below line.
               i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
               // on below line we are starting the activity.
               startActivity(i);
           }
       });
   }
}

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 variables for button and edittext with the id which we have given in our activity_main.xml file. After that we are adding an on click listener for the button. Inside the on click listener we are creating the uri which we have to open. We are passing the web page URL for the google play store. After that we are creating an intent with the action as an action view. Then we are setting the flags for that intent. Lastly we are calling start activity to open the Google Play store.

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 article we have taken a look on How to open the Google Play store in an android application.

Updated on: 09-May-2023

289 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements