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

后端 未结 17 846
面向向阳花
面向向阳花 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:43

    It may help to other people.

    Lots of popular apps have the different approach to show the progress of anything like network request, file loading etc. Loading spinner doesn't show the how much content has been loaded or remaining to load. There is a period of uncertainty which is bad in the perspective of UI/UX. Lot of popular apps(Facebook, Linkedin etc) has resolved this issue by showing the bare bones UI displays first. Then the loaded content is gradually populated on-screen.

    I have used the shimmer for my apps to solve this issue.

    There is a good article about this which will be beneficial for other people

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

    you can use AlertDialog as ProgressDialog refer below code for the ProgressDialog. This function you need to call whenever you show a progress dialog.

    Code:

        public void setProgressDialog() {
    
        int llPadding = 30;
        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.HORIZONTAL);
        ll.setPadding(llPadding, llPadding, llPadding, llPadding);
        ll.setGravity(Gravity.CENTER);
        LinearLayout.LayoutParams llParam = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        llParam.gravity = Gravity.CENTER;
        ll.setLayoutParams(llParam);
    
        ProgressBar progressBar = new ProgressBar(this);
        progressBar.setIndeterminate(true);
        progressBar.setPadding(0, 0, llPadding, 0);
        progressBar.setLayoutParams(llParam);
    
        llParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        llParam.gravity = Gravity.CENTER;
        TextView tvText = new TextView(this);
        tvText.setText("Loading ...");
        tvText.setTextColor(Color.parseColor("#000000"));
        tvText.setTextSize(20);
        tvText.setLayoutParams(llParam);
    
        ll.addView(progressBar);
        ll.addView(tvText);
    
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setCancelable(true);
        builder.setView(ll);
    
        AlertDialog dialog = builder.create();
        dialog.show();
        Window window = dialog.getWindow();
        if (window != null) {
            WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
            layoutParams.copyFrom(dialog.getWindow().getAttributes());
            layoutParams.width = LinearLayout.LayoutParams.WRAP_CONTENT;
            layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
            dialog.getWindow().setAttributes(layoutParams);
        }
    }
    

    Output:

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

    You can simply design an xml interface for your progressbar and pass it as a view to a AlertDialog, then show or dismiss the dialog anytime you want.

    progress.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:padding="13dp"
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    
        <ProgressBar
            android:id="@+id/loader"
            android:layout_marginEnd="5dp"
            android:layout_width="45dp"
            android:layout_height="45dp" />
        <TextView
            android:layout_width="wrap_content"
            android:text="Loading..."
            android:textAppearance="?android:textAppearanceSmall"
            android:layout_gravity="center_vertical"
            android:id="@+id/loading_msg"
            android:layout_toEndOf="@+id/loader"
            android:layout_height="wrap_content" />
    
    </LinearLayout>
    

    The code code that displays the progress dialog. Just copy this code and paste it your fragment.

      AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
           private void setDialog(boolean show){
                builder.setView(R.layout.progress);
                Dialog dialog = builder.create();
                if (show)dialog.show();
                else dialog.dismiss();
            }
    

    Then just call the method whenever you want to show the progressdialog and pass true as an argument to show it or false to dismiss the dialog.

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

    ProgressDialog was deprecated in API level 26 .

    "Deprecated" refers to functions or elements that are in the process of being replaced by newer ones.

    ProgressDialog is a modal dialog, which prevents the user from interacting with the app. Instead of using this class, you should use a progress indicator like ProgressBar, which can be embedded in your app's UI.

    Advantage

    I would personally say that ProgressBar has the edge over the two .ProgressBar is a user interface element that indicates the progress of an operation. Display progress bars to a user in a non-interruptive way. Show the progress bar in your app's user interface.

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

    As mentioned on the documentation page the alternative is ProgressBar. ProgressDialog's look can be replicated by placing a ProgressBar into an AlertDialog.

    You can still use it, but Android does not want you to use it, that is why it is deprecated. So you should consider solving your problem in another way, like embedding a ProgressBar into your Layout.

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

    In the progress dialog, user cannot do any kind of work. All the background processes are stopped during progress dialog. So, It is advisable to user progress-bar instead of progress dialog.

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