Matej Špilár
Matej Špilár

Reputation: 2687

creation of custom progress bar

Can somebody explain me how to achieve a custom progress bar that draws upon some ImageButton like this:

enter image description here

I was searching and implementing it myself, but wasn't event close to what is in the picture :(

The only similiar thing I found were this and this

Upvotes: 1

Views: 162

Answers (1)

MrEngineer13
MrEngineer13

Reputation: 38856

This example code should give what you're looking for. Let me know if it helps.

enter image description here

EDIT: I edited the link to only point to the specific project which should make it easier.

Download the project as a zip from the link I provided then extract it. Inside the romannurik-code-e77853751bbd folder navigate to misc/pinprogress and then you will be inside this project. From the src folder add PinProgressButton.java to your project along with all of the items in the res directory except ic_launcher.png.

Add it to your layout

<com.yourpackage.pinprogress.PinProgressButton
                android:id="@+id/pin_progress_1"
                style="@style/PinProgressButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|right" />

Reference it from your activity

 final PinProgressButton pinProgress1 = (PinProgressButton) findViewById(
            R.id.pin_progress_1);

To set the progress

pinProgress1.setProgress(progressValue);

This is to change the content description

    CompoundButton.OnCheckedChangeListener checkedChangeListener
            = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
            updatePinProgressContentDescription((PinProgressButton) compoundButton);
        }
    };

    pinProgress1.setOnCheckedChangeListener(checkedChangeListener);

    updatePinProgressContentDescription(pinProgress1);
}

private void updatePinProgressContentDescription(PinProgressButton button) {
    int progress = button.getProgress();
    if (progress <= 0) {
        button.setContentDescription(getString(button.isChecked()
                ? R.string.content_desc_pinned_not_downloaded
                : R.string.content_desc_unpinned_not_downloaded));
    } else if (progress >= 100) {
        button.setContentDescription(getString(button.isChecked()
                ? R.string.content_desc_pinned_downloaded
                : R.string.content_desc_unpinned_downloaded));
    } else {
        button.setContentDescription(getString(button.isChecked()
                ? R.string.content_desc_pinned_downloading
                : R.string.content_desc_unpinned_downloading));
    }
}

Upvotes: 1

Related Questions