ProgressDialog is deprecated.What is the alternate one to use?

后端 未结 17 847
面向向阳花
面向向阳花 2020-11-27 09:11

I have come across to see that ProgressDialog is now deprecated. What would be alternate one to use in place of that apart from ProgressBar. I am

相关标签:
17条回答
  • 2020-11-27 09:50

    I use DelayedProgressDialog from https://github.com/Q115/DelayedProgressDialog It does the same as ProgressDialog with the added benefit of a delay if necessary.

    Using it is similar to ProgressDialog before Android O:

    DelayedProgressDialog progressDialog = new DelayedProgressDialog();
    progressDialog.show(getSupportFragmentManager(), "tag");
    
    0 讨论(0)
  • 2020-11-27 09:55

    You can use SpotDialog by using the library wasabeef you can find the complete tutorial from the following link:

    SpotsDialog Example in Android

    0 讨论(0)
  • 2020-11-27 09:56

    Yes, ProgressDialog is deprecated but Dialog isn't.

    You can inflate your own XML file ( containing a progress bar and a loading text) into your dialog object and then display or hide it using the show() and dismiss() functions. Here is an example (Kotlin):

    ProgressDialog class:

    class ProgressDialog {
    companion object {
        fun progressDialog(context: Context): Dialog{
            val dialog = Dialog(context)
            val inflate = LayoutInflater.from(context).inflate(R.layout.progress_dialog, null)
            dialog.setContentView(inflate)
            dialog.setCancelable(false)
            dialog.window!!.setBackgroundDrawable(
                    ColorDrawable(Color.TRANSPARENT))
            return dialog
        }
      }
    }
    

    XML

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:background="#fff"
    android:padding="13dp"
    android:layout_height="wrap_content">
    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyle"
        android:layout_width="100dp"
        android:layout_margin="7dp"
        android:layout_height="100dp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_margin="7dp"
        android:layout_toEndOf="@+id/progressBar"
        android:text="Loading..." />
    </RelativeLayout>
    

    In your code: Just do var dialog = ProgressDialog.progressDialog(context)

    To show: dialog.show()

    To hide: dialog.dismiss()

    0 讨论(0)
  • 2020-11-27 10:00

    Yes, in API level 26 it's deprecated. Instead, you can use progressBar.

    To create it programmatically:

    First get a reference to the root layout

    RelativeLayout layout = findViewById(R.id.display);  //specify here Root layout Id
    

    or

    RelativeLayout layout = findViewById(this);
    

    Then add the progress bar

    progressBar = new ProgressBar(youractivity.this, null, android.R.attr.progressBarStyleLarge);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100, 100);
    params.addRule(RelativeLayout.CENTER_IN_PARENT);
    layout.addView(progressBar, params);
    

    To show the progress bar

    progressBar.setVisibility(View.VISIBLE);
    

    To hide the progress bar

    progressBar.setVisibility(View.GONE);
    

    To disable the user interaction you just need to add the following code

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                               WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
    

    To get user interaction back you just need to add the following code

    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
    

    Just for future reference, change the android.R.attr.progressBarStyleSmall to android.R.attr.progressBarStyleHorizontal.

    The code below only works above API level 21

    progressBar.setProgressTintList(ColorStateList.valueOf(Color.RED));
    

    To create it via xml:

    <ProgressBar
            android:id="@+id/progressbar"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:indeterminate="true"
            android:max="100"
            android:backgroundTint="@color/white"
            android:layout_below="@+id/framelauout"
            android:indeterminateTint="#1a09d6"
            android:layout_marginTop="-7dp"/>
    

    In your activity

    progressBar = (ProgressBar) findViewById(R.id.progressbar);
    

    Showing/hiding the progress bar is the same

     progressBar.setVisibility(View.VISIBLE); // To show the ProgressBar 
     progressBar.setVisibility(View.INVISIBLE); // To hide the ProgressBar
    

    Here is a sample image of what it would look like:

    For more details:
    1. Reference one
    2. Reference Two

    0 讨论(0)
  • 2020-11-27 10:00

    You don't need to import any custom library.

    I prefer to use the modern AlertDialog so this is the Kotlin version for the great answer posted by Kishan Donga in this page.

    Kotlin code:

    fun setProgressDialog(context:Context, message:String):AlertDialog {
        val llPadding = 30
        val ll = LinearLayout(context)
        ll.orientation = LinearLayout.HORIZONTAL
        ll.setPadding(llPadding, llPadding, llPadding, llPadding)
        ll.gravity = Gravity.CENTER
        var llParam = LinearLayout.LayoutParams(
                      LinearLayout.LayoutParams.WRAP_CONTENT,
                      LinearLayout.LayoutParams.WRAP_CONTENT)
        llParam.gravity = Gravity.CENTER
        ll.layoutParams = llParam
    
        val progressBar = ProgressBar(context)
        progressBar.isIndeterminate = true
        progressBar.setPadding(0, 0, llPadding, 0)
        progressBar.layoutParams = llParam
    
        llParam = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT)
        llParam.gravity = Gravity.CENTER
        val tvText = TextView(context)
        tvText.text = message
        tvText.setTextColor(Color.parseColor("#000000"))
        tvText.textSize = 20.toFloat()
        tvText.layoutParams = llParam
    
        ll.addView(progressBar)
        ll.addView(tvText)
    
        val builder = AlertDialog.Builder(context)
        builder.setCancelable(true)
        builder.setView(ll)
    
        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
    }
    

    Usage:

    val dialog = setProgressDialog(this, "Loading..")
    dialog.show()
    

    Output:

    0 讨论(0)
提交回复
热议问题