Chuck Norris Facts is an Internet phenomenon with joke “facts” about martial artist and actor Chuck Norris. The “facts” are jokes about Norris’ toughness, attitude, virility, “alpha-male status”, and masculinity.

Chuck Norris Facts Android App with Kotlin

In that tutorial, you are going to build your own Chuck Norris Facts Android Application with Kotlin. Android Studio is the IDE used in this tutorial. It is a good way to discover how to make JSON Web Services calls in Kotlin and how to use the great OkHttp 3 library.

The facts will be got from the Internet Chuck Norris Database which offers a simple API to get random Chuck Norris Facts :

http://api.icndb.com/jokes/random

Note that you can also watch this tutorial on YouTube :

Adding the OkHttp dependency

To make the network calls on the Web Service, we are going to use the OkHttp Library. So, we need to add the OkHttp 3.10 dependency in our build.gradle file :

Configuring the Android Manifest

To make network calls, we need to add the INTERNET permission on the Android Manifest of our Android Application :

Creating the User Interface

Next step is to create the User Interface of our Android Application. We will use a ConstraintLayout as the parent layout for our User Interface.

Our User Interface will have an ImageView at the top to display the face of Chuck Norris :

Chuck Norris

Then, we add a TextView to display a Chuck Norris Fact. We define a constraint to place the TextView just below the ImageView. After that, we add a Button which will let to users to load new Chuck Norris Fact by querying the Internet Chuck Norris Database API. Finally, we add a ProgressBar which will be centered on the screen.

It gives us the following XML for our User Interface :

Testing the API

Before writing the Kotlin code of the MainActivity, we are going to test the response returned by the Internet Chuck Norris Database API. We will use the following endpoint :

https://api.icndb.com/jokes/random

This Web Service returns a new Chuck Norris Fact randomly at each call. By entering the URL in a Web Browser, you should have the following result :

Chuck Norris Fact API in Action

So, we will need to parse the JSON response for getting the joke property of the value object from the root object.

Writing the Kotlin code of the Main Activity

Now, it’s time to write the Kotlin code of the MainActivity. We define a value to store the URL of the endpoint of the API we are going to call. Then, we instantiate an OkHttpClient object.

In the onCreate method of the MainActivity, we have just to set an OnClickListener on the Button letting the users to load new Chuck Norris Facts.

The call of the Web Service is made in a loadRandomFact dedicated method. We display the ProgressBar just before to call the Web Service. Then, we build a Request object with the URL of the endpoint in parameter.

After that, we create a new call on the OkHttpClient instance by calling its newCall method with the Request instance in parameter. To process the call to the Web Service, we call the enqueue method with a Callback instance in parameter.

In the onResponse method, we get the body of the response and then, we build a JSONObject. Last step is to get the joke property of the value object. With the Chuck Norris Fact read, we can display it in the TextView by encapsulating all in a runOnUiThread block to be sure the update of the User Interface is made in the UI Thread.

It gives us the following code for the MainActivity of our Chuck Norris Fact Android App :

Chuck Norris Facts Android App in Action

Best part of the tutorial is coming. Indeed, it’s time to put the Chuck Norris Facts Android App in Action. When, the application will be launched, you will have the following result :

Chuck Norris Facts Android App in Action

That’s all for that tutorial. Feel free to leave comments if you have some questions concerning this tutorial.

To discover more tutorials on Android Development, you can also subscribe to the SSaurel’s Channel on YouTube :