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.
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:
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:
Hi, thanks for the code.
I have this error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: , PID: 23828
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{/.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method ‘android.content.res.Resources android.content.Context.getResources()’ on a null object reference
and the line of the null is:
val linearLayout = LinearLayout(context)
(line 17)
could you have some idea why this error?
Hi, Franco
Thanks for reaching us.
In MainActivity in your onCreate function, put your dialog ( ProgressDialogUtil.setProgressDialog(this, “Loading…”) ) after setContentView(R.layout.activity_main) line.
this
keyword points to your MainActivity, andthis
will be null before onCreate function is called.If you have any issues, please feel free to ask.
Best,
DenOfDevelopers.
Thanks! now it shows, but how can I call it out outside the onCreate function?
Thanks now it shows, but how can I call it outside the onCreate function? I mean to hide the dialog from another function.