Utility Class to Show Loading Dialog in Kotlin

Today we are going to discuss how to Show Loading Dialog in Kotlin anywhere in the application with a simple utility class. You can add this Utility Class to Show Loading Dialog in the Kotlin project. You can simply copy and paste the code into your class and use it right out of the box. Also, you can make changes in the XML file to adjust the position, color, and style.

Show Loading Dialog in Kotlin

XML Layout Code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:gravity="center"
    android:orientation="vertical">

    <ProgressBar
        android:id="@+id/progress"
        android:textSize="50px"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_centerInParent="true"/>
    <TextView
        android:id="@+id/text"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        ></TextView>
</RelativeLayout>

We have created an XML file with the name “layout_loading_screen”. In this file paste the above code.

Loading Screen Code in Kotlin

After creating the XML file, you have to create a class file in Kotlin with the name of LoadingScreen. After that place the code inside this file.

package com.handyopnion

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.Window
import android.widget.TextView


object LoadingScreen {
    var dialog: Dialog? = null //obj
    fun displayLoadingWithText(context: Context?, text: String?, cancelable: Boolean) { // function -- context(parent (reference))
        dialog = Dialog(context!!)
        dialog!!.requestWindowFeature(Window.FEATURE_NO_TITLE)
        dialog!!.setContentView(R.layout.layout_loading_screen)
        dialog!!.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        dialog!!.setCancelable(cancelable)
        val textView = dialog!!.findViewById<TextView>(R.id.text)
        textView.text = text
        try {
            dialog!!.show()
        } catch (e: Exception) {
        }
    }

    fun hideLoading() {
        try {
            if (dialog != null) {
                dialog!!.dismiss()
            }
        } catch (e: Exception) {
        }
    }
}

In the above code, you can see two functions. The first function is displayLoadingWithText() and the second function is hideLoading(). In displayLoadingWithText() we have to pass the context, String, and boolean. In context, we can"this" pointer from Activity or Fragment. After that, we have to pass the String and the string can be any text you want to display. At last, we need to pass the boolean, indicating that either loading dialog is cancelable or not?

How to use Loading class from Activity Code in Kotlin

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        LoadingScreen.displayLoadingWithText(this,"Please wait...",false)
}

Here we call the displayLoadingWithText() function in the Main Activity and pass the parameters.

That’s It. This is how to make Utility Class Show Loading Dialog in Kotlin

If you have any questions feel free to ask in the comments section below.

Next Article

Show Alert Dialog With an Input Field (EditText) in Android Kotlin

Please share this post:
Posts created 31

Ask a Question

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

%d bloggers like this: