Progress Dialog Util Class – Kotlin
- agulevski10
- Jan 28, 2020
- 2 min read
This is progress dialog util class if you need in your project. It is written in Kotlin. You can just copy the code and paste inside you own class and use it straight out of the box. Just pass context and message. Also, you can adjust the position, color, padding and style inside this class. Everything is done grammatically. The dialog is set not to be canceled if clicked outside it.
Java
import android.content.Context import android.graphics.Color import android.view.Gravity import android.view.ViewGroup import android.view.WindowManager import android.widget.LinearLayout import android.widget.ProgressBar import android.widget.TextView import androidx.appcompat.app.AlertDialog object ProgressDialogUtil { fun setProgressDialog(context: Context, message: String): AlertDialog { val padding = 50 val linearLayout = LinearLayout(context) linearLayout.orientation = LinearLayout.HORIZONTAL linearLayout.setPadding(padding, padding, padding, padding) linearLayout.gravity = Gravity.START var params = LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT ) params.gravity = Gravity.CENTER linearLayout.layoutParams = params val progressBar = ProgressBar(context) progressBar.isIndeterminate = true progressBar.setPadding(0, 0, padding, 0) progressBar.layoutParams = params params = LinearLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT ) params.gravity = Gravity.CENTER val tvText = TextView(context) tvText.text = message tvText.setTextColor(Color.parseColor("#000000")) tvText.textSize = 20.toFloat() tvText.layoutParams = params linearLayout.addView(progressBar) linearLayout.addView(tvText) val builder = AlertDialog.Builder(context) builder.setCancelable(false) builder.setView(linearLayout) val dialog = builder.create() val window = dialog.window if (window != null) { val layoutParams = WindowManager.LayoutParams() layoutParams.copyFrom(dialog.window?.attributes) layoutParams.width = LinearLayout.LayoutParams.WRAP_CONTENT layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT dialog.window?.attributes = layoutParams } return dialog } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import android.content.Context
import android.graphics.Color
import android.view.Gravity
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.LinearLayout
import android.widget.ProgressBar
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
object ProgressDialogUtil {
fun setProgressDialog(context: Context, message: String): AlertDialog {
val padding = 50
val linearLayout = LinearLayout(context)
linearLayout.orientation = LinearLayout.HORIZONTAL
linearLayout.setPadding(padding, padding, padding, padding)
linearLayout.gravity = Gravity.START
var params = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
)
params.gravity = Gravity.CENTER
linearLayout.layoutParams = params
val progressBar = ProgressBar(context)
progressBar.isIndeterminate = true
progressBar.setPadding(0, 0, padding, 0)
progressBar.layoutParams = params
params = LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
params.gravity = Gravity.CENTER
val tvText = TextView(context)
tvText.text = message
tvText.setTextColor(Color.parseColor("#000000"))
tvText.textSize = 20.toFloat()
tvText.layoutParams = params
linearLayout.addView(progressBar)
linearLayout.addView(tvText)
val builder = AlertDialog.Builder(context)
builder.setCancelable(false)
builder.setView(linearLayout)
val dialog = builder.create()
val window = dialog.window
if (window != null) {
val layoutParams = WindowManager.LayoutParams()
layoutParams.copyFrom(dialog.window?.attributes)
layoutParams.width = LinearLayout.LayoutParams.WRAP_CONTENT
layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT
dialog.window?.attributes = layoutParams
}
return dialog
}
}
And to use it just call setProgressDialog function and add context and message, like this:
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val dialog = ProgressDialogUtil.setProgressDialog(this, "Loading...") dialog.show() }
1
2
3
4
5
6
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val dialog = ProgressDialogUtil.setProgressDialog(this, "Loading...")
dialog.show()
}
This is how it looks like:






Comments