edbaev edbaev - 13 days ago 6
Android Question

progressBar.setIndeterminateDrawable() doesn't work

When I try change my indeteminateDrawable progressbar just gone(
I have to change drawable for my progressbar. I tried invalidate, requestLayout and other. I dont know how I can fix it.
Thank you..............

Here code:

progressBar.setIndeterminateDrawable(getResources().getDrawable(R.drawable.‌​progress_indeterminate_horizontal_didnt_like));

progressBar.setIndeterminate(true);


Yes. It's visible in XML.

<ProgressBar
android:id="@+id/progress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
android:indeterminateDrawable="@drawable/progress_indeterminate_horizontal_to_review" />





private void showProgressByCategory(String category) {
progressBar = (ProgressBar) findViewById(R.id.progress);


switch (CATEGORY.getPosition(category)) {
case CATEGORY.DIDNT_LIKE_POS:
// Didnt like
progressBar.setIndeterminateDrawable(getResources().getDrawable(
R.drawable.progress_indeterminate_horizontal_didnt_like));

break;
case CATEGORY.TO_SEE_POS:
// To See
progressBar.setIndeterminateDrawable(getResources().getDrawable(
R.drawable.progress_indeterminate_horizontal_to_see));
break;
case CATEGORY.SAW_AND_LIKED_POS:
// Saw And Liked
progressBar.setIndeterminateDrawable(getResources().getDrawable(
R.drawable.progress_indeterminate_horizontal_saw_liked));
break;
case CATEGORY.TO_REVIEW_POS:
// To Review
progressBar.setIndeterminateDrawable(getResources().getDrawable(
R.drawable.progress_indeterminate_horizontal_to_review));

break;
}
progressBar.setIndeterminate(true);
}

Answer

I fixed the same problem by creating a new ProgressBar whenever I want to change the Drawable.

Note:

  1. Hide previous ProgressBar.
  2. Create new one and show it.

Code:

public class MyProgressDialog extends ProgressDialog {
    public MyProgressDialog(Context context) {
        super(context);
    }

    public ResultType resultType;

    public enum ResultType {
        eNone,
        eSuccess,
        eFail;
    }

    public void hide(String message, long completionIndicatorInterval) {
        hide();

        if ( (completionIndicatorInterval > 0) &&
                (resultType != ResultType.eNone)) {
            final MyProgressDialog completedDialog = new MyProgressDialog(getContext());
            completedDialog.setCancelable(false);
            completedDialog.setMessage(message);

            if (resultType == ResultType.eSuccess) {
                Drawable drawable = getContext().getResources().getDrawable(R.drawable.check);
                completedDialog.setIndeterminateDrawable(drawable);
                completedDialog.setIndeterminate(true);
            } else {
                Drawable drawable = getContext().getResources().getDrawable(R.drawable.close_image);
                completedDialog.setIndeterminateDrawable(drawable);
                completedDialog.setIndeterminate(true);
            }

            completedDialog.show();

            Handler handler = new Handler();
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    hide();
                    completedDialog.hide();
                }
            };
            handler.postDelayed(runnable, completionIndicatorInterval);
        }
    }
}
Comments