Custom Drawable For Progressbar/Progressdialog

Show Android custom ProgressBar or ProgressDialog dynamically in any class

Follow this answer.

The only thing you will need to do in addition to that is to add your custom loading drawable, using setIndeterminateDrawable():

ProgressDialog pd = new ProgressDialog(getActivity(), R.style.MyTheme);
pd.setCancelable(false);
pd.setIndeterminateDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.my_progress_indeterminate));
pd.setProgressStyle(android.R.style.Widget_ProgressBar_Small);
pd.show();`

No need for a custom ProgressDialog subclass.

Custom ProgressDialog

Override onCreate method of ProgressDialog and make any changes you want to its progress bar.

ProgressDialog mProgress = new ProgressDialog(this){
@Override
protected void onCreate(android.os.Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Now we retrive this dialog ProgressBar
ProgresBar bar = (ProgressBar) findViewById(android.R.id.progress);
//Do what you want with it here
};
};

How to Customize a Progress Bar In Android

Customizing a ProgressBar requires defining the attribute or properties for the background and progress of your progress bar.

Create an XML file named customprogressbar.xml in your res->drawable folder:

custom_progressbar.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<!-- Define the background properties like color etc -->
<item android:id="@android:id/background">
<shape>
<gradient
android:startColor="#000001"
android:centerColor="#0b131e"
android:centerY="1.0"
android:endColor="#0d1522"
android:angle="270"
/>
</shape>
</item>

<!-- Define the progress properties like start color, end color etc -->
<item android:id="@android:id/progress">
<clip>
<shape>
<gradient
android:startColor="#007A00"
android:centerColor="#007A00"
android:centerY="1.0"
android:endColor="#06101d"
android:angle="270"
/>
</shape>
</clip>
</item>
</layer-list>

Now you need to set the progressDrawable property in customprogressbar.xml (drawable)

You can do this in the XML file or in the Activity (at run time).

Do the following in your XML:

<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:progressDrawable="@drawable/custom_progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

At run time do the following

// Get the Drawable custom_progressbar                     
Drawable draw=res.getDrawable(R.drawable.custom_progressbar);
// set the drawable as progress drawable
progressBar.setProgressDrawable(draw);

Edit: corrected xml layout

Android change ProgressDialog drawable while dialog works

Finally i find a solution

ProgressDialog contains inside a ProgressBar which display our drawable.

View view = inflater.inflate(a.getResourceId(com.android.internal.R.styleable.AlertDialog_horizontalProgressLayout, R.layout.alert_dialog_progress), null);
mProgress = (ProgressBar) view.findViewById(R.id.progress); // THIS
mProgressNumber = (TextView) view.findViewById(R.id.progress_number);
mProgressPercent = (TextView) view.findViewById(R.id.progress_percent);
setView(view);

When we try to set drawable again to ProgressDialog,ProgressDialog really set a new drawable. But this drawable didn't have a bounds (coordinates to display this view).
So we must set it hardly. I choose a copy way - just a copy bounds from current drawable of ProgressDialog. You can use another way.

Initialize dialog :

private void initDialog() {
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setIndeterminate(true);
mProgressDialog.setTitle("Please wait.");
mProgressDialog.setMessage("Connecting to LinkedIn.");
mProgressDialog.setCancelable(false);
}

Show

    mProgressDialog.show();

Change drawable

private void changeToDone(int resId) {
//Getting a progressBar from dialog
ProgressBar bar = (ProgressBar) mProgressDialog.findViewById(android.R.id.progress);
//Getting a DONE(new) drawable from resources
Drawable drawable = getResources().getDrawable(resId);
//Getting a drawable from progress dialog
Drawable indeterminateDrawable = bar.getIndeterminateDrawable();
//Obtain a bounds of current drawable
Rect bounds = indeterminateDrawable.getBounds();
//Set bounds to DONE(new) drawable
drawable.setBounds(bounds);
//Set a new drawable
bar.setIndeterminateDrawable(drawable);

mProgressDialog.setTitle("Done.");
mProgressDialog.setMessage("Connected.");
}

Note :

Solution untested on specific cases like an

  • We have a progress bar in ActionBar/TitleBar
  • Another cases when we already have an android.R.progress on the screen window. In this case i think my solution will be produce unexpected behavior.


Related Topics



Leave a reply



Submit