Open In App
Related Articles

How to Integrate Razorpay Payment Gateway in Android?

Like Article
Save Article
Report issue

Many apps nowadays require to have a payment gateway inside their application so that users can do any transactions inside their apps to purchase any product or any service. Many apps use the payment gateway features but the integration of this payment gateway is a difficult task in Android applications. So to make this task simple and easy Razorpay have provided a service with the help of this we can integrate the payment solutions in our app very easily and we can also manage all payment methods in our app. In this article, we will take a look at the implementation of a payment gateway in our Android app. 

What we are going to build in this article? 

We will be building a simple Android application in which we will be displaying an EditText and a button. Inside this screen, we have to add the amount which is to be paid and on clicking the button we will open the Razorpay payment gateway and will make a payment. In this article, we will be adding test credentials for implementing Razorpay in Android. A sample video is given below to get an idea about what we are going to do in this article. 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: Add dependency of Razor pay library in build.gradle file

Navigate to the Gradle Scripts > build.gradle(Module:app) and add the below dependency in the dependencies section.   

implementation ‘com.razorpay:checkout:1.6.4’

 After adding this dependency sync your project and now we will move towards the XML part. 

Step 3: Adding permissions to the Internet

Navigate to the app > AndroidManifest.xml file and add the below code to it. 


<uses-permission android:name="android.permission.INTERNET" />


Step 4: 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 version="1.0" encoding="utf-8"?>
    <!--EditText text to enter amount-->
        android:hint="Enter amount to be payed"
        android:inputType="number" />
    <!--button to make payment-->
        android:text="Pay using RazorPay"
        android:textAllCaps="false" />


Step 5: Generating an API key for using Razorpay

Browser the Razorpay site in Google or you can click on the link here. After clicking on this link you simply have to signup with your email and password and add some basic information such as your phone number. 

Note: Here we are creating a testing credential for using Razor Pay. 

Inside the setting screen, click on Create a new key option your key will be generated. We will be using key ID in our application to test Razor pay. The key-id will start with rzp_test 

Step 6: Working with the file

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


import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.razorpay.Checkout;
import com.razorpay.PaymentResultListener;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity implements PaymentResultListener {
    // variables for our 
    // edit text and button.
    private EditText amountEdt;
    private Button payBtn;
    protected void onCreate(Bundle savedInstanceState) {
        // initializing all our variables.
        amountEdt = findViewById(;
        payBtn = findViewById(;
        // adding on click listener to our button.
        payBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // on below line we are getting 
                // amount that is entered by user.
                String samount = amountEdt.getText().toString();
                // rounding off the amount.
                int amount = Math.round(Float.parseFloat(samount) * 100);
                // initialize Razorpay account.
                Checkout checkout = new Checkout();
                // set your id as below
                checkout.setKeyID("Enter your key id here");
                // set image
                // initialize json object
                JSONObject object = new JSONObject();
                try {
                    // to put name
                    object.put("name", "Geeks for Geeks");
                    // put description
                    object.put("description", "Test payment");
                    // to set theme color
                    object.put("theme.color", "");
                    // put the currency
                    object.put("currency", "INR");
                    // put amount
                    object.put("amount", amount);
                    // put mobile number
                    object.put("", "9284064503");
                    // put email
                    object.put("", "");
                    // open razorpay to checkout activity
          , object);
                } catch (JSONException e) {
    public void onPaymentSuccess(String s) {
        // this method is called on payment success.
        Toast.makeText(this, "Payment is successful : " + s, Toast.LENGTH_SHORT).show();
    public void onPaymentError(int i, String s) {
        // on payment failed.
        Toast.makeText(this, "Payment Failed due to error : " + s, Toast.LENGTH_SHORT).show();


Now run your app and see the output of the app.


As we are using test credentials so our payment will not be done. For making your payments live you have to make your application live in the Razorpay console and generate a new key. 

Last Updated : 31 Jan, 2021
Like Article
Save Article
Share your thoughts in the comments
Similar Reads