Ramp Android integration tutorial
If you have an Android app built with Kotlin, here's a tutorial on how to add Ramp to it in just a few steps.
You can see the full code sample at https://github.com/RampNetwork/integration-example-android.
What do we need to do?
- compose a URL for the Ramp widget with parameters of your choice
- create an intent and send it to the system to handle it
- [OPTIONAL] prepare the app for handle a return intent after a successful purchase
Composing the widget URL
To compose widget URL you can simply create uri with 'https://buy.ramp.network' address.
This has one big caveat - the user has to copy-paste their information like their crypto address in the widget.
To avoid that, the Ramp widget allows you to provide some parameters before displaying it. You can set options such as wallet address, cryptocurrency and crypto amount, etc. In order to do so, we need to create a URL with proper query parameters as showcased in the snippet below. You can find the description of all available parameters in our documentation.
Let's declare some parameters then.
Now, you need to compose the URL using these parameters.
Create an intent and send it
In order to open Ramp in a browser, we need to create an intent with the action parameter set to
Intent.ACTION_VIEW and add the previously created URL as
data. Then, we'll start an activity with this intent. From now on, the system will handle it on its own and it'll open the browser with our URL loaded.
[OPTIONAL] Handling a URL callback
One of the query parameters you can pass to Ramp widget URL is
finalUrl. If you do this, Ramp will redirect your users to this URL after the purchase is completed. You can use this mechanism for your app to detect purchase completion and perform some actions like notifying your user about the purchase status.
Step 1 - Define a redirection URL
Now, we need to register a URL scheme that's unique for our app. Usually the best way is to simply use the app's name. If your app's name is Ramp-Example, the scheme may be
ramp-example. Next, append a host that is unique for completing Ramp Purchase, for example
Having these two, you can now define a value for the
finalUrl parameter - it will be
Add this parameter to the URL you created earlier.
Step 2 - Register deep link to app content
To create a link to your app content, we need to add an intent filter with
category elements in the
AndroidManifest.xml file. Intent filter should contain
ACTION_VIEW value, two categories with the values
host that you defined in step 1. You can find more details on how to add a custom URL scheme in the Android documentation.
To prevent multiple instances, add
singleTask value in the
Step 3 - Handle final callback in your activity
Now it's time to handle the URL callback inside your app by overriding the
onNewIntent method in your
Activity just like in the snippets below.
We also need to ensure that incoming intents will be processed properly in the
onCreate method by using
processIntent(it) in case our activity was killed or is newly created.
That's it - your app can now use Ramp to allow your users to buy crypto easily.