1

EDIT: Stll NO SOLUTION! At least post code with Progress Bar, that is also getting NullPointer

How can you change a Progress Dialog color for each rotation programmatically?

Code:

if (checkValidation()) {
                    progressDialog.setTitle(setStringResource(R.string.reg_user));
                    progressDialog.setMessage(setStringResource(R.string.wait));
                    progressDialog.setCancelable(false);
                    progressDialog.show();
                    registerUser(getName, getEmail, getPassword);
                }

Also, one more question. What if I want to change the title of same progress dialog using like this progressDialog.setMessage(setStringResource(R.string.creating_user)); But for particular time period?

Like I want to change the title of same progress dialog after 30 seconds.

Pooja Singh
  • 121
  • 6

2 Answers2

1

Firstly this is not recommended solution but you can achieve it like below

Runs a timer for each second and update it using below method.

ProgressBar progressbar=(ProgressBar) progressDialog.findViewById(android.R.id.progress);
progressbar.getIndeterminateDrawable().setColorFilter(Color.parseColor("#C60000"), android.graphics.PorterDuff.Mode.SRC_IN);

EDIT

The code written below is in kotlin language

    pDialog = ProgressDialog.show(mContext, "Title goes here", "Message goes here")
    pDialog?.show()

    val progressbar = pDialog!!.findViewById(android.R.id.progress) as ProgressBar
    progressbar.indeterminateDrawable.setColorFilter(Color.parseColor("#C60000"), android.graphics.PorterDuff.Mode.SRC_IN)

see output

enter image description here

In Java

ProgressDialog progressDialog = ProgressDialog.show(this, "Title goes here", "Message goes here");
progressDialog.show();

ProgressBar progressbar = (ProgressBar) progressDialog.findViewById(android.R.id.progress);
runTimer(progressbar);

After the initialization of progressDialog call the below method. Also, don't forget to take the reference of progressbar in a global variable.

void runTimer(final ProgressBar progressbar){

        final Handler handler = new Handler();
        final int delay = 1000; //milliseconds

        handler.postDelayed(new Runnable(){
            public void run(){
                count++;
                switch (count%3){
                    case 0:
                        progressbar.getIndeterminateDrawable().setColorFilter(Color.RED, android.graphics.PorterDuff.Mode.SRC_IN);
                        break;
                    case 1:
                        progressbar.getIndeterminateDrawable().setColorFilter(Color.GREEN, android.graphics.PorterDuff.Mode.SRC_IN);
                        break;
                    case 2:
                        progressbar.getIndeterminateDrawable().setColorFilter(Color.BLUE, android.graphics.PorterDuff.Mode.SRC_IN);
                        break;
                }



                handler.postDelayed(this, delay);
            }
        }, delay);
    }

You need to declare count variable globally.

int count = 0;
Rahul Khurana
  • 8,577
  • 7
  • 33
  • 60
0

try this

 progressDialog = (ProgressBar) findViewById(R.id.progressbar_Horizontal);
    AnimationDrawable animdrawable = getProgressBarAnimation();
    progressDialog.setBackgroundDrawable(animdrawable);

For animation

    private AnimationDrawable getProgressBarAnimation() {

    GradientDrawable rainbow1 = new GradientDrawable(Orientation.LEFT_RIGHT,
            new int[]{Color.RED, Color.MAGENTA, Color.BLUE, Color.CYAN, Color.GREEN, Color.YELLOW});

    GradientDrawable rainbow2 = new GradientDrawable(Orientation.LEFT_RIGHT,
            new int[]{Color.YELLOW, Color.RED, Color.MAGENTA, Color.BLUE, Color.CYAN, Color.GREEN});

    GradientDrawable rainbow3 = new GradientDrawable(Orientation.LEFT_RIGHT,
            new int[]{Color.GREEN, Color.YELLOW, Color.RED, Color.MAGENTA, Color.BLUE, Color.CYAN});

    GradientDrawable rainbow4 = new GradientDrawable(Orientation.LEFT_RIGHT,
            new int[]{Color.CYAN, Color.GREEN, Color.YELLOW, Color.RED, Color.MAGENTA, Color.BLUE});

    GradientDrawable rainbow5 = new GradientDrawable(Orientation.LEFT_RIGHT,
            new int[]{Color.BLUE, Color.CYAN, Color.GREEN, Color.YELLOW, Color.RED, Color.MAGENTA});

    GradientDrawable rainbow6 = new GradientDrawable(Orientation.LEFT_RIGHT,
            new int[]{Color.MAGENTA, Color.BLUE, Color.CYAN, Color.GREEN, Color.YELLOW, Color.RED});


    GradientDrawable[] gds = new GradientDrawable[]{rainbow1, rainbow2, rainbow3, rainbow4, rainbow5, rainbow6};

    AnimationDrawable animation = new AnimationDrawable();

    for (GradientDrawable gd : gds) {
        animation.addFrame(gd, 100);

    }

    animation.setOneShot(false);

    return animation;


}
Ramkumar.M
  • 681
  • 6
  • 21