Create a Bitcoin Price Index Watcher Application for Android
Bitcoin is a worldwide cryptocurrency and digital payment system called the first decentralized digital currency, since the system works without a central repository or single administrator.
In that tutorial, we are going to create a Bitcoin Price Index Watcher Application for Android with Android Studio. In this application, we will offer to the users Bitcoin Price Index in US Dollars, Great Britain Pounds and Euros.
Note that you can discover this tutorial in video on YouTube :
To get the Bitcoin Price Index in US Dollars, Great Britain Pounds and Euros, we are going to use the Coin Desk API in version 1 and particularly the current price endpoint : https://api.coindesk.com/v1/bpi/currentprice.json
Creating the User Interface
First step is to create a simple interface for our application with the Bitcoin logo and a TextView to display the Bitcoin Price Index values :
To let the users to start the Bitcoin Price Index loading, we add a load item in the menu of our Main Activity :
Updating the Android Manifest
Like we are going to make an internet call, we need to add the internet permission in our Android Manifest :
Adding OkHttp dependency
To call the Coin Desk API, we are going to use the great OkHttp library in version 3.9. So, we add the dependency in the Gradle build file :
Writing the Java Code
Now, it’s time to write the Java code of the Main Activity. We start by defining the Bitcoin Price Index endpoint as a constant. Then, we define a property for the OkHttpClient we are going to use. We add also a property for the ProgressDialog that will be used to display a waiting message to the users during the loading and a property for the TextView.
In the onOptionsItemSelected, we manage the load menu click. If a user clicks on the load item in the action bar, we are going to call the load method. In the load method, we create the web request to reach the Bitcoin Price Index endpoint. Then, we make a new call on the OkHttpClient instance and we enqueue the call. Note that when you call the enqueue method, the web call is made in a separate thread.
In the onResponse method of the Callback interface, we get the response body in string format. Then, we make the User Interface changes inside a runOnUiThread block. In this block, we call the parseBpiResponse with the body to parse.
Finally, we can write the parseBpiResponse method in which we are going to parse the content got from the Bitcoin Price Index API as a JSON message. We start by building the JSON Object from the body got from the web service.
Then, we get the time object and then its updated property to know the last update of the Bitcoin Price Index we got. After that, we get the bpi JSON Object and we get the USD, GBP and EUR JSON Objects to get the rate values we want to display to the user. When we have read all these values, we can update the TextView.
This gives us the following code for the Main Activity :
Testing our Bitcoin Price Index Watcher App
Now, it’s time to test our Bitcoin Price Index Watcher Application. Once the activity is launched, we have to click on the load item in the Action Bar and you should see the Bitcoin Price Index displayed on the screen :
It works great. To improve the application, you could store the values got and display the BPI history to your users. It’s your time to code !
To discover more tutorials on Android development, don’t hesitate to subscribe to the SSaurel’s Channel on YouTube :