Introduction
In this article, I am going to illustrate how a developer can link their app with the Google Play Store so that a user can navigate there to rate the applications by clicking on a particular button or image.
Google provides several application programming interfaces to perform this operation. I am going to explain two aspects - 1.How you can navigate your user to the Play Store when the user's device has Google Play Store installed; 2. when the user device doesn't have Google Play Store (Virtual Device - Emulator).
Creating Project
Step 1
Create a new Android Studio Project as below.
Step 2
Give the name of project location and package name of your project and then click on Next.
Step-3
Choose "Phone and Tablet" as application category and set KitKat-API-19 as minimum SDK. Then, click on Next.
Step-3
Select Empty activity as main activity of your project and click Next.
Step-4
One Kotlin file named MainActivity.kt and one Layout file named activity_main.xml will be created.
Creating Layout
Step-5
Add the following xml code to your activity_main.xml file.
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.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="manigautam.app.myplaystoreratingapp.MainActivity">
-
- <Button
- android:id="@+id/btnplaystore"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/googleplayimage"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- />
-
- </android.support.constraint.ConstraintLayout>
Step-6
Your design should look like this.
Operational Code
Step -7
Initialize the button in Kotlin file.
We have no need to do parsing of Resource integer value like java here because in the latest version of Kotlin, the function extension technique does it automatically.
Step-8
Set the listener onClick on this button.
Now, the complete Kotlin code of your MainActivity.kt file looks like this.
- package manigautam.app.myplaystoreratingapp
- import android.content.Intent
- import android.net.Uri
- import android.support.v7.app.AppCompatActivity
- import android.os.Bundle
- import android.widget.Button
- import kotlinx.android.synthetic.main.activity_main.*
-
- class MainActivity : AppCompatActivity() {
-
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- var btncallstore:Button=findViewById(R.id.btnplaystore)
- btnplaystore.setOnClickListener {
- try {
- var playstoreuri1: Uri = Uri.parse("market://details?id=" + packageName)
-
-
- var playstoreIntent1: Intent = Intent(Intent.ACTION_VIEW, playstoreuri1)
- startActivity(playstoreIntent1)
-
- }catch (exp:Exception){
- var playstoreuri2: Uri = Uri.parse("http://play.google.com/store/apps/details?id=" + packageName)
-
- var playstoreIntent2: Intent = Intent(Intent.ACTION_VIEW, playstoreuri2)
- startActivity(playstoreIntent2)
- }
-
- }
- }
- }
Output
Build and run your application. After running your application, your screen looks something like below. When clicked on the button, it will call Google Play Store and display your app so that a user can rate your app.
The current sample app is not live; that’s why it's showing that the required application is not found along with the search screen. If your app is live, then it will display the screen of the following live application.