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

后端 未结 17 1046
面向向阳花
面向向阳花 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条回答
  •  萌比男神i
    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:

    
    

    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

提交回复
热议问题