Android integration via SDK

Ramp Android SDK is a library that allows you to easily integrate Ramp into your Android app and communicate with it.

Overview

In this tutorial, we'll install the SDK via Gradle and initialize it with configuration data. We'll also go over implementing callbacks for events sent by Ramp.

Installing the Gradle Plugin

To install the Ramp Android SDK, add its repository in your root build.gradle at the end of repositories:

allprojects {
repositories {
maven { url 'https://jitpack.io' }
maven { url 'https://button.passbase.com/__android' }
}
}

Add the dependency in your build.gradle file:

dependencies {
implementation 'com.github.RampNetwork:ramp-sdk-android:1.+'
}

Initializing the SDK

Go to an Activity or a Fragment in your directory where you want to start a transaction with Ramp. Add the following import statements to the top of the file:

import network.ramp.sdk.events.model.Purchase
import network.ramp.sdk.facade.Config
import network.ramp.sdk.facade.RampCallback
import network.ramp.sdk.facade.RampSDK

After that, you need to initialize the SDK with your activity context.

class MainActivity : AppCompatActivity() {
private lateinit var rampSdk: RampSDK
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
rampSdk = RampSDK(this)
// ...
}
// ...
}

Filling the configuration object with your data

Ramp widget allows you to provide some useful parameters before displaying it. Most of the parameters are optional. You can set options such as user wallet address, desired cryptocurrency and crypto amount, etc. In order to do so, we need to add those parameters to a Config object provided by SDK.

val config = Config(
hostLogoUrl = "https://example.com/logo.png",
hostAppName = "My App",
userAddress = "user_blockchain_address",
// optional, skip this param to use the production URL
url = "https://ri-widget-staging.firebaseapp.com/",
swapAsset = "ETH",
fiatCurrency = "USD",
fiatValue = "10",
userEmailAddress = "test@example.com"
)

You can find the description of all available parameters in our documentation.

Implementing callbacks

Create a callback object RampCallback to handle transaction events that you can react to in your app.

Methods you can get by implementing this object:

  • onPurchaseCreated(purchase: Purchase, purchaseViewToken: String, apiUrl: String) is called when a purchase is created, and returns a Purchase object, containing all its parameters. All fields of are described in the documentation here.
  • onPurchaseFailed() is called when Ramp fails in any aspect. You will see the reason of failure on the screen.
  • onWidgetClose() is called when Ramp finishes the flow and can be closed, or user closed it manually.
val callback = object: RampCallback {
override fun onPurchaseFailed() {
// ...
}
override fun onPurchaseCreated(
purchase: Purchase,
purchaseViewToken: String,
apiUrl: String
) {
// ...
}
override fun onWidgetClose() {
// ...
}
}

Starting the widget

That's it, now you just need to run the startTransaction(config: Config, callback: RampCallback) method to open the Ramp widget. This method takes config object that you created above.

rampSdk.startTransaction(config, callback)

Example & Demo App

A full code example for a basic integration can be found below. You can find a demo integration showing how to integrate the Ramp SDK for Android on our Github.

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
import network.ramp.sdk.events.model.Purchase
import network.ramp.sdk.facade.Config
import network.ramp.sdk.facade.RampCallback
import network.ramp.sdk.facade.RampSDK
class MainActivity : AppCompatActivity() {
// initialize the SDK
private lateinit var rampSdk: RampSDK
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
rampSdk = RampSDK(this)
button.setOnClickListener {
// fill configuration object with your data
val config = Config(
hostLogoUrl = "https://example.com/logo.png",
hostAppName = "My App",
userAddress = "0x4b7f8e04b82ad7f9e4b4cc9e1f81c5938e1b719f",
url = "https://ri-widget-staging.firebaseapp.com/",
swapAsset = "ETH",
fiatCurrency = "USD",
fiatValue = "10",
userEmailAddress = "test@example.com"
)
// implement callbacks
val callback = object : RampCallback {
override fun onPurchaseFailed() {
...
}
override fun onPurchaseCreated(
purchase: Purchase,
purchaseViewToken: String,
apiUrl: String
) {
...
}
override fun onWidgetClose() {
...
}
}
// start Ramp
rampSdk.startTransaction(config, callback)
}
}
}