Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Main Activity of the Chuck Norris Facts Android App for the SSaurel's Channel
package com.ssaurel.chucknorrisfacts
import android.os.Bundle
import android.text.Html
import android.view.View
import okhttp3.*
import org.json.JSONObject
class MainActivity : AppCompatActivity() {
// we will use the ICNDB API for Chuck Norris Facts
val URL = ""
var okHttpClient: OkHttpClient = OkHttpClient()
override fun onCreate(savedInstanceState: Bundle?) {
nextBtn.setOnClickListener {
private fun loadRandomFact() {
runOnUiThread {
progressBar.visibility = View.VISIBLE
val request: Request = Request.Builder().url(URL).build()
okHttpClient.newCall(request).enqueue(object: Callback {
override fun onFailure(call: Call?, e: IOException?) {
override fun onResponse(call: Call?, response: Response?) {
val json = response?.body()?.string()
// we get the joke from the Web Service
val txt = (JSONObject(json).getJSONObject("value").get("joke")).toString()
// we update the UI from the UI Thread
runOnUiThread {
progressBar.visibility = View.GONE
// we use Html class to decode html entities
factTv.text = Html.fromHtml(txt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment