Git Product home page Git Product logo

circularprogressindicator's Introduction

CircularProgressIndicator

Simple but customizable view for displaying progress

With custom colors

With or without dot

With custom progress text (more examples here)

How to use

Add view to your layout:

<antonkozyriatskyi.circularprogressindicator.CircularProgressIndicator
    android:id="@+id/circular_progress"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_gravity="center"
    android:layout_marginBottom="8dp"
    android:layout_marginTop="8dp"
    app:progressColor="@color/colorPrimary"
    app:progressBackgroundColor="#efefefef"
    app:progressStrokeWidth="8dp"
    app:drawDot="true"
    app:dotColor="@color/colorAccent"
    app:dotWidth="16dp"
    app:textSize="24sp"
    app:textColor="@color/colorPrimaryDark" />

Since all attributes have default values, you can specify none of them. Thus following code also works:

<antonkozyriatskyi.circularprogressindicator.CircularProgressIndicator
    android:id="@+id/circular_progress"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_gravity="center"
    android:layout_marginBottom="8dp"
    android:layout_marginTop="8dp" />

Than find it in code and set progress:

CircularProgressIndicator circularProgress = findViewById(R.id.circular_progress);

// you can set max and current progress values individually
circularProgress.setMaxProgress(10000);
circularProgress.setCurrentProgress(5000);
// or all at once
circularProgress.setProgress(5000, 10000);

// you can get progress values using following getters
circularProgress.getProgress() // returns 5000
circularProgress.getMaxProgress() // returns 10000

Attributes

Description XML Java Default value
Progress color app:progressColor setter: setProgressColor(color)
getter: getProgressColor()
#3f51b5
Progress background color app:progressBackgroundColor setter: setProgressBackgroundColor(color)
getter: getProgressBackgroundColor()
#e0e0e0
Width of progress stroke app:progressStrokeWidth setters: setProgressStrokeWidthDp(widthInDp) or setProgressStrokeWidthPx(widthInPx)
getter: getProgressStrokeWidth() (returns width in pixels)
8dp
Whether to draw dot. true or false app:drawDot setter: setShouldDrawDot(shoulDrawDot)
getter: isDotEnabled()
true
Dot color app:dotColor setter: setDotColor(dotColor)
getter: getDotColor()
same as progress color
Dot width app:dotWidth setters: setDotWidthDp(widthInDp) or setDotWidthPx(widthInPx)
getter: getDotWidth() (returns width in pixels)
same as progress stroke width
Progress text size app:textSize setters: setTextSizeSp(sizeInSp) or setTextSizePx(sizeInPx)
getter: getTextSize() (returns size in pixels)
24sp
Progress text color app:textColor setter: setTextColor(textColor)
getter: getTextColor()
same as progress color
Whether to use delimiter or not. true or false.
Deprecated and will be removed in next release.
Use ProgressTextAdapter instead.
app:useProgressTextDelimiter setter: setShouldUseDelimiter(shouldUseDelimiter)
getter: isTextDelimiterEnabled()
true
The delimiter to use in progress text.
Deprecated and will be removed in next release.
Use ProgressTextAdapter instead.
app:progressTextDelimiter setter: setProgressTextDelimiter(delimiter)
getter: getProgressTextDelimiter()
,
Prefix for progress text.
Deprecated and will be removed in next release.
Use ProgressTextAdapter instead.
app:progressTextPrefix setter: setProgressTextPrefix(prefix)
getter: getProgressTextPrefix()
null (disabled)
Suffix for progress text.
Deprecated and will be removed in next release.
Use ProgressTextAdapter instead.
app:progressTextSuffix setter: setProgressTextSuffix(suffix)
getter: getProgressTextSuffix()
null (disabled)
Formatting pattern to be used in PatternProgressTextAdapter. Checkout Formatting progress text section. app:formattingPattern not specified

Formatting progress text

Before version 1.0.5 formatting text was limited to specifying prefix, suffix and delimiter. Since version 1.1 you have full control over the text to be displayed (thanks to the repitch for the idea and implementation). So app:useProgressTextDelimiter, app:progressTextDelimiter, app:progressTextPrefix, app:progressTextSuffix attributes are now deprecated and not taken into account.

The only thing you have to do is to provide an implementation of CircularProgressIndicator.ProgressTextAdapter. You must override it's String formatText(double currentProgress);. Library already provides you with two implementations:

  • DefaultProgressTextAdapter that just returns string representation of the progress value cast to int.
  • PatternProgressTextAdapter that uses pattern specified in formattingPattern attribute to format progress (if it is set, otherwise it fallbacks to the DefaultProgressTextAdapter).
Custom formatters' examples

DefaultProgressTextAdapter is a default text adapter, which is used when no formatterPattern attribute set or progressIndicator.setProgressTextAdapter(null) method was called. It's implementation looks like this:

    @Override
    public String formatText(double currentProgress) {
        return String.valueOf((int) currentProgress);
    }

PatternProgressTextAdapter supports formatting progress using Java's formatting patterns. For example, if you want to display progress with two digits for the fractional part, you can use %.2f pattern and set it via

  • attribute:
    app:formattingPattern="@string/pattern" that refers to a string resource
    <string name="pattern">%.2f</string>
    If you don't want to use string resource - you have to use unicode for percent sign (&#37;) instead of sign itself:
    app:formattingPattern="&#37;.2f"

  • or PatternProgressTextAdapter: circularProgress.setProgressTextAdapter(new PatternProgressTextAdapter("%.2f"));

Here is the example of TimeTextAdapter by repitch that displays text in format of 'HH.MM.SS'

private static final CircularProgressIndicator.ProgressTextAdapter TIME_TEXT_ADAPTER = new CircularProgressIndicator.ProgressTextAdapter() {
        @Override
        public String formatText(double time) {
            int hours = (int) (time / 3600);
            time %= 3600;
            int minutes = (int) (time / 60);
            int seconds = (int) (time % 60);
            StringBuilder sb = new StringBuilder();
            if (hours < 10) {
                sb.append(0);
            }
            sb.append(hours).append(":");
            if (minutes < 10) {
                sb.append(0);
            }
            sb.append(minutes).append(":");
            if (seconds < 10) {
                sb.append(0);
            }
            sb.append(seconds);
            return sb.toString();
        }
    };

If you don't want any text to be displayed - just create your own ProgressTextAdapter that returns an empty string.


Download using Gradle

Add this in your root build.gradle at the end of repositories in allprojects section:

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}

Then add this dependency to your module-level build.gradle in dependencies section:

implementation 'com.github.antonKozyriatskyi:CircularProgressIndicator:1.1.0'

If you have any troubles downloading the library - checkout issue#5


License

 Copyright 2018 Anton Kozyriatskyi

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

circularprogressindicator's People

Contributors

antonkozyriatskyi avatar repitch avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.