Create a Bitcoin Watcher Android App with Kotlin

We continue our learning of Kotlin to develop Android applications. Today we will see how to make HTTP requests with Kotlin. For this, we will develop an application to know the price of Bitcoin in real-time.

Note that you can discover this tutorial in a video on YouTube:


Adding dependencies

To make HTTP requests in our application, we will use the OkHttp library provided by Square Up. For that, we add the OkHttp dependency in our buid.gradle file which will be have the following form:


Configuring the Android Manifest

Like we are going to make some HTTP requests, we need to add the INTERNET permission in our AndroidManifest.xml file:


Creating the User Interface

The User Interface of our Android Application made with Kotlin will be quite simple. An ImageView to display a logo, a TextView to display the price of the Bitcoin in US Dollars and Euros and also a ProgressBar which will be displayed during the HTTP request to get the Bitcoin price.

We use the ConstraintLayout to place easily these views with the Design view on Android Studio. It gives us the following code for our activity_main.xml file:


Creating a load entry on the menu

To let the users to load the price of the Bitcoin, we are going to use a dedicated entry in the menu of our application. We add it in the res/menu/main.xml file like that:


Writing the Kotlin code of the Main Activity

Now, it’s time to write the Kotlin code of the Main Activity. To get the Bitcoin price in real time, we are going to use the Coin Desk API which the end point is there: https://api.coindesk.com/v1/bpi/currentprice.json .

You can make a simple call to this end point on a browser for example. You should obtain the following result:

In the JSON response, you can see we will need first to get the bpi object of the main object. Then, we will to get the USD and the EUR objects. Finally, we will read the rate property of these two objects.

For that, we will create a Request object with the URL of the Coin Desk end point in parameter. Then, we will pass this Request object in parameter of the newCall method of our OkHttpClient instance. We will enqueue the call and the result will be obtained in a Callback object.

In this Callback object, we will obtain the JSON data got in the onResponse method. In this method, we will have to convert the data read to String to keep only the integer part of the rates.

Finally, we need to display the rates got from the Coin Desk API. For that, we need to execute our code in the UI Thread. So, we use a runOnUiThread call which is shortcuted in Kotlin. It will give us the following for the MainActivity:


Testing our Bitcoin Price Android App

Now, it’s time to try our Bitcoin Price Android App made with Kotlin. By launching the application, you should have the following screen:

Our Android App at start

Click on the load entry in the Action Bar and the Bitcoin prices should be loaded. You should enjoy the following display:

Bitcoin prices displayed

Great!

That’s all for that tutorial. To discover more Android tutorials, don’t hesitate to subscribe on the SSaurel’s Channel:

https://www.youtube.com/channel/UCXoh49OXVg1UGjgWX1JRvlw

You can also visit SSaurel’s Blog if you want to discover more tutorials on Android and Java development: https://www.ssaurel.com/blog

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *