├── .gitignore
├── LICENSE
├── README.md
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── loadingbutton
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── kusu
│ │ └── loadingbutton
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── kusu
│ │ │ └── loadingbutton
│ │ │ ├── CircularAnimatedDrawable.java
│ │ │ └── LoadingButton.java
│ └── res
│ │ └── values
│ │ ├── attrs.xml
│ │ ├── colors.xml
│ │ ├── dimens.xml
│ │ └── strings.xml
│ └── test
│ └── java
│ └── com
│ └── kusu
│ └── loadingbutton
│ └── ExampleUnitTest.java
├── sampledemo.gif
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | .idea
4 | /local.properties
5 | .DS_Store
6 | /build
7 | /captures
8 | .externalNativeBuild
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2019 Koushik Mondal
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # LoadingButton
2 | Show or hide loading on button
3 |
4 | [](https://jitpack.io/#koushikcse/LoadingButton)
5 | []( https://android-arsenal.com/details/1/7683 )
6 |
7 | 
8 |
9 | ## Add to Gradle
10 |
11 | Add this to your project level `build.gradle` file
12 |
13 | ```gradle
14 | repositories {
15 | maven { url "https://jitpack.io" }
16 | }
17 | ```
18 |
19 | And then add this to your module level `build.gradle` file
20 |
21 | ```gradle
22 | dependencies {
23 | implementation 'com.github.koushikcse:LoadingButton:1.7'
24 | }
25 | ```
26 | ## How it works
27 |
28 | Sometimes we need to show loading on button as per requirement we don't want to block full screen view with default loading or dialogs. So you can use this library to show loading on button hide loading after that when you want.
29 |
30 | ## How to setup
31 |
32 | Its very easy to use **LoadingButton** as its a custom button and contain all features of android default button with some extra attributes.
33 |
34 | ### Add it to a layout
35 |
36 | ```xml
37 |
54 | ...
55 |
56 | ```
57 |
58 |
59 | ### Methods to use in View Class
60 |
61 | You can easily use to show loading on button click by calling `showLoading();` and hide loading by calling
62 | `hideLoading();` whenever you want to stop loading.
63 |
64 | **Show Loading**
65 |
66 | `LoadingButton loadingButton = (LoadingButton) findViewById(R.id.loadingButton);
67 | loadingButton.showLoading();
68 | `
69 |
70 | **Hide Loading**
71 |
72 | `LoadingButton loadingButton = (LoadingButton) findViewById(R.id.loadingButton);
73 | loadingButton.hideLoading();
74 | `
75 |
76 | # LICENSE
77 |
78 | MIT License
79 |
80 | Copyright (c) 2019 Koushik Mondal
81 |
82 | Permission is hereby granted, free of charge, to any person obtaining a copy
83 | of this software and associated documentation files (the "Software"), to deal
84 | in the Software without restriction, including without limitation the rights
85 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
86 | copies of the Software, and to permit persons to whom the Software is
87 | furnished to do so, subject to the following conditions:
88 |
89 | The above copyright notice and this permission notice shall be included in all
90 | copies or substantial portions of the Software.
91 |
92 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
93 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
94 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
95 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
96 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
97 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
98 | SOFTWARE.
99 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | google()
6 | jcenter()
7 | mavenCentral()
8 | }
9 | dependencies {
10 | classpath 'com.android.tools.build:gradle:3.6.1'
11 | classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
12 |
13 | // NOTE: Do not place your application dependencies here; they belong
14 | // in the individual module build.gradle files
15 | }
16 | }
17 |
18 | allprojects {
19 | repositories {
20 | mavenCentral()
21 | google()
22 | jcenter()
23 | maven { url "https://jitpack.io" }
24 | }
25 | }
26 |
27 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | android.enableJetifier=true
2 | android.useAndroidX=true
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/koushikcse/LoadingButton/2ac5f014ad70314c9885f3064bdc50db3ca69810/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Fri Jun 26 16:28:35 IST 2020
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/loadingbutton/.gitignore:
--------------------------------------------------------------------------------
1 | # Built application files
2 | *.apk
3 | *.ap_
4 |
5 | # Files for the ART/Dalvik VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # Generated files
12 | bin/
13 | gen/
14 | out/
15 |
16 | # Gradle files
17 | .gradle/
18 | build/
19 |
20 | # Local configuration file (sdk path, etc)
21 | local.properties
22 |
23 | # Proguard folder generated by Eclipse
24 | proguard/
25 |
26 | # Log Files
27 | *.log
28 |
29 | # Android Studio Navigation editor temp files
30 | .navigation/
31 |
32 | # Android Studio captures folder
33 | captures/
34 |
35 | # IntelliJ
36 | *.iml
37 | .idea/workspace.xml
38 | .idea/tasks.xml
39 | .idea/gradle.xml
40 | .idea/assetWizardSettings.xml
41 | .idea/dictionaries
42 | .idea/libraries
43 | .idea/caches
44 |
45 | # Keystore files
46 | # Uncomment the following line if you do not want to check your keystore files in.
47 | #*.jks
48 |
49 | # External native build folder generated in Android Studio 2.2 and later
50 | .externalNativeBuild
51 |
52 | # Google Services (e.g. APIs or Firebase)
53 | google-services.json
54 |
55 | # Freeline
56 | freeline.py
57 | freeline/
58 | freeline_project_description.json
59 |
60 | # fastlane
61 | fastlane/report.xml
62 | fastlane/Preview.html
63 | fastlane/screenshots
64 | fastlane/test_output
65 | fastlane/readme.md
66 |
--------------------------------------------------------------------------------
/loadingbutton/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'com.github.dcendents.android-maven'
3 |
4 | repositories {
5 | mavenCentral()
6 | google()
7 | jcenter()
8 | maven { url "https://jitpack.io" }
9 | }
10 |
11 | group='com.github.koushikcse'
12 | version = '1.7'
13 |
14 | android {
15 | compileSdkVersion 29
16 |
17 | defaultConfig {
18 | minSdkVersion 21
19 | targetSdkVersion 29
20 | versionCode 8
21 | versionName "1.7"
22 |
23 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
24 |
25 | }
26 |
27 | buildTypes {
28 | release {
29 | minifyEnabled false
30 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
31 | }
32 | }
33 |
34 | }
35 |
36 | dependencies {
37 | implementation fileTree(dir: 'libs', include: ['*.jar'])
38 |
39 | implementation 'androidx.appcompat:appcompat:1.1.0'
40 | testImplementation 'junit:junit:4.12'
41 | androidTestImplementation 'androidx.test:runner:1.2.0'
42 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/loadingbutton/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/loadingbutton/src/androidTest/java/com/kusu/loadingbutton/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.kusu.loadingbutton;
2 |
3 | import android.content.Context;
4 | import androidx.test.platform.app.InstrumentationRegistry;
5 | import androidx.test.ext.junit.runners.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.kusu.loadingbutton.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/loadingbutton/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
--------------------------------------------------------------------------------
/loadingbutton/src/main/java/com/kusu/loadingbutton/CircularAnimatedDrawable.java:
--------------------------------------------------------------------------------
1 | package com.kusu.loadingbutton;
2 |
3 | import android.animation.Animator;
4 | import android.animation.ObjectAnimator;
5 | import android.animation.ValueAnimator;
6 | import android.graphics.Canvas;
7 | import android.graphics.ColorFilter;
8 | import android.graphics.Paint;
9 | import android.graphics.PixelFormat;
10 | import android.graphics.Rect;
11 | import android.graphics.RectF;
12 | import android.graphics.drawable.Animatable;
13 | import android.graphics.drawable.Drawable;
14 | import android.util.Property;
15 | import android.view.animation.DecelerateInterpolator;
16 | import android.view.animation.Interpolator;
17 | import android.view.animation.LinearInterpolator;
18 |
19 | class CircularAnimatedDrawable extends Drawable implements Animatable {
20 |
21 | private static final Interpolator ANGLE_INTERPOLATOR = new LinearInterpolator();
22 | private static final Interpolator SWEEP_INTERPOLATOR = new DecelerateInterpolator();
23 | private static final int ANGLE_ANIMATOR_DURATION = 1000;
24 | private static final int SWEEP_ANIMATOR_DURATION = 1000;
25 | public static final int MIN_SWEEP_ANGLE = 30;
26 | private final RectF fBounds = new RectF();
27 |
28 | private ObjectAnimator mObjectAnimatorSweep;
29 | private ObjectAnimator mObjectAnimatorAngle;
30 | private boolean mModeAppearing;
31 | private Paint mPaint;
32 | private float mCurrentGlobalAngleOffset;
33 | private float mCurrentGlobalAngle;
34 | private float mCurrentSweepAngle;
35 | private float mBorderWidth;
36 | private boolean mRunning;
37 |
38 | public CircularAnimatedDrawable(int color, float borderWidth) {
39 | mBorderWidth = borderWidth;
40 |
41 | mPaint = new Paint();
42 | mPaint.setAntiAlias(true);
43 | mPaint.setStyle(Paint.Style.STROKE);
44 | mPaint.setStrokeWidth(borderWidth);
45 | mPaint.setColor(color);
46 |
47 | setupAnimations();
48 | }
49 |
50 | @Override
51 | public void draw(Canvas canvas) {
52 | float startAngle = mCurrentGlobalAngle - mCurrentGlobalAngleOffset;
53 | float sweepAngle = mCurrentSweepAngle;
54 | if (!mModeAppearing) {
55 | startAngle = startAngle + sweepAngle;
56 | sweepAngle = 360 - sweepAngle - MIN_SWEEP_ANGLE;
57 | } else {
58 | sweepAngle += MIN_SWEEP_ANGLE;
59 | }
60 | canvas.drawArc(fBounds, startAngle, sweepAngle, false, mPaint);
61 | }
62 |
63 | @Override
64 | public void setAlpha(int alpha) {
65 | mPaint.setAlpha(alpha);
66 | }
67 |
68 | @Override
69 | public void setColorFilter(ColorFilter cf) {
70 | mPaint.setColorFilter(cf);
71 | }
72 |
73 | @Override
74 | public int getOpacity() {
75 | return PixelFormat.TRANSPARENT;
76 | }
77 |
78 | private void toggleAppearingMode() {
79 | mModeAppearing = !mModeAppearing;
80 | if (mModeAppearing) {
81 | mCurrentGlobalAngleOffset = (mCurrentGlobalAngleOffset + MIN_SWEEP_ANGLE * 2) % 360;
82 | }
83 | }
84 |
85 | @Override
86 | protected void onBoundsChange(Rect bounds) {
87 | super.onBoundsChange(bounds);
88 | fBounds.left = bounds.left + mBorderWidth / 2f + .5f;
89 | fBounds.right = bounds.right - mBorderWidth / 2f - .5f;
90 | fBounds.top = bounds.top + mBorderWidth / 2f + .5f;
91 | fBounds.bottom = bounds.bottom - mBorderWidth / 2f - .5f;
92 | }
93 |
94 | private Property mAngleProperty =
95 | new Property(Float.class, "angle") {
96 | @Override
97 | public Float get(CircularAnimatedDrawable object) {
98 | return object.getCurrentGlobalAngle();
99 | }
100 |
101 | @Override
102 | public void set(CircularAnimatedDrawable object, Float value) {
103 | object.setCurrentGlobalAngle(value);
104 | }
105 | };
106 |
107 | private Property mSweepProperty
108 | = new Property(Float.class, "arc") {
109 | @Override
110 | public Float get(CircularAnimatedDrawable object) {
111 | return object.getCurrentSweepAngle();
112 | }
113 |
114 | @Override
115 | public void set(CircularAnimatedDrawable object, Float value) {
116 | object.setCurrentSweepAngle(value);
117 | }
118 | };
119 |
120 | private void setupAnimations() {
121 | mObjectAnimatorAngle = ObjectAnimator.ofFloat(this, mAngleProperty, 360f);
122 | mObjectAnimatorAngle.setInterpolator(ANGLE_INTERPOLATOR);
123 | mObjectAnimatorAngle.setDuration(ANGLE_ANIMATOR_DURATION);
124 | mObjectAnimatorAngle.setRepeatMode(ValueAnimator.RESTART);
125 | mObjectAnimatorAngle.setRepeatCount(ValueAnimator.INFINITE);
126 |
127 | mObjectAnimatorSweep = ObjectAnimator.ofFloat(this, mSweepProperty, 360f - MIN_SWEEP_ANGLE * 2);
128 | mObjectAnimatorSweep.setInterpolator(SWEEP_INTERPOLATOR);
129 | mObjectAnimatorSweep.setDuration(SWEEP_ANIMATOR_DURATION);
130 | mObjectAnimatorSweep.setRepeatMode(ValueAnimator.RESTART);
131 | mObjectAnimatorSweep.setRepeatCount(ValueAnimator.INFINITE);
132 | mObjectAnimatorSweep.addListener(new Animator.AnimatorListener() {
133 | @Override
134 | public void onAnimationStart(Animator animation) {
135 |
136 | }
137 |
138 | @Override
139 | public void onAnimationEnd(Animator animation) {
140 |
141 | }
142 |
143 | @Override
144 | public void onAnimationCancel(Animator animation) {
145 |
146 | }
147 |
148 | @Override
149 | public void onAnimationRepeat(Animator animation) {
150 | toggleAppearingMode();
151 | }
152 | });
153 | }
154 |
155 | @Override
156 | public void start() {
157 | if (isRunning()) {
158 | return;
159 | }
160 | mRunning = true;
161 | mObjectAnimatorAngle.start();
162 | mObjectAnimatorSweep.start();
163 | invalidateSelf();
164 | }
165 |
166 | @Override
167 | public void stop() {
168 | if (!isRunning()) {
169 | return;
170 | }
171 | mRunning = false;
172 | mObjectAnimatorAngle.cancel();
173 | mObjectAnimatorSweep.cancel();
174 | invalidateSelf();
175 | }
176 |
177 | @Override
178 | public boolean isRunning() {
179 | return mRunning;
180 | }
181 |
182 | public void setCurrentGlobalAngle(float currentGlobalAngle) {
183 | mCurrentGlobalAngle = currentGlobalAngle;
184 | invalidateSelf();
185 | }
186 |
187 | public float getCurrentGlobalAngle() {
188 | return mCurrentGlobalAngle;
189 | }
190 |
191 | public void setCurrentSweepAngle(float currentSweepAngle) {
192 | mCurrentSweepAngle = currentSweepAngle;
193 | invalidateSelf();
194 | }
195 |
196 | public float getCurrentSweepAngle() {
197 | return mCurrentSweepAngle;
198 | }
199 |
200 | }
201 |
--------------------------------------------------------------------------------
/loadingbutton/src/main/java/com/kusu/loadingbutton/LoadingButton.java:
--------------------------------------------------------------------------------
1 | package com.kusu.loadingbutton;
2 |
3 | import android.annotation.SuppressLint;
4 | import android.content.Context;
5 | import android.content.res.Resources;
6 | import android.content.res.TypedArray;
7 | import android.graphics.Canvas;
8 | import android.graphics.Color;
9 | import android.graphics.Rect;
10 | import android.graphics.drawable.Drawable;
11 | import android.graphics.drawable.LayerDrawable;
12 | import android.graphics.drawable.ShapeDrawable;
13 | import android.graphics.drawable.shapes.RoundRectShape;
14 | import androidx.appcompat.widget.AppCompatButton;
15 | import android.util.AttributeSet;
16 | import android.view.MotionEvent;
17 | import android.view.View;
18 |
19 | public class LoadingButton extends AppCompatButton implements View.OnTouchListener {
20 |
21 | private boolean isShadowColorDefined = false;
22 | //Custom values
23 | private boolean isShadowEnabled = true;
24 | private boolean isCircularButton = false;
25 | private int mButtonColor;
26 | private int mloaderColor;
27 | private int mShadowColor;
28 | private int mShadowHeight;
29 | private int mCornerRadius;
30 | private boolean isLoading = false;
31 | //Native values
32 | private int mPaddingLeft;
33 | private int mPaddingRight;
34 | private int mPaddingTop;
35 | private int mPaddingBottom;
36 | //Background drawable
37 | private Drawable pressedDrawable;
38 | private Drawable unpressedDrawable;
39 |
40 | private CircularAnimatedDrawable mAnimatedDrawable;
41 | private int mPaddingProgress = 15;
42 | private int mStrokeWidth = 10;
43 | private int mMaxProgress = 100;
44 | private int mColorIndicator;
45 | private int mProgress = 100;
46 | private String text = "";
47 | private Canvas mcanvas;
48 |
49 | @SuppressLint("ClickableViewAccessibility")
50 | public LoadingButton(Context context) {
51 | super(context);
52 | init();
53 | this.setOnTouchListener(this);
54 | }
55 |
56 | @SuppressLint("ClickableViewAccessibility")
57 | public LoadingButton(Context context, AttributeSet attrs) {
58 | super(context, attrs);
59 | init();
60 | parseAttrs(context, attrs);
61 | this.setOnTouchListener(this);
62 | }
63 |
64 | @SuppressLint("ClickableViewAccessibility")
65 | public LoadingButton(Context context, AttributeSet attrs, int defStyleAttr) {
66 | super(context, attrs, defStyleAttr);
67 | init();
68 | parseAttrs(context, attrs);
69 | this.setOnTouchListener(this);
70 | }
71 |
72 | @Override
73 | protected void onFinishInflate() {
74 | super.onFinishInflate();
75 | refresh();
76 | }
77 |
78 | private void init() {
79 | //Init default values
80 | isShadowEnabled = true;
81 | Resources resources = getResources();
82 | if (resources == null) return;
83 | mloaderColor = resources.getColor(R.color.white);
84 | mButtonColor = resources.getColor(R.color.fbutton_default_color);
85 | mShadowColor = resources.getColor(R.color.fbutton_default_shadow_color);
86 | mShadowHeight = resources.getDimensionPixelSize(R.dimen.fbutton_default_shadow_height);
87 | mCornerRadius = resources.getDimensionPixelSize(R.dimen.fbutton_default_conner_radius);
88 | text = getText().toString();
89 | }
90 |
91 | private void parseAttrs(Context context, AttributeSet attrs) {
92 | //Load from custom attributes
93 | TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.LoadingButton);
94 | if (typedArray == null) return;
95 | for (int i = 0; i < typedArray.getIndexCount(); i++) {
96 | int attr = typedArray.getIndex(i);
97 | if (attr == R.styleable.LoadingButton_lb_isShadowEnable) {
98 | isShadowEnabled = typedArray.getBoolean(attr, true); //Default is true
99 | } else if (attr == R.styleable.LoadingButton_lb_buttonColor) {
100 | mButtonColor = typedArray.getColor(attr, getResources().getColor(R.color.unpressed_color));
101 | } else if (attr == R.styleable.LoadingButton_lb_loaderColor) {
102 | mloaderColor = typedArray.getColor(attr, getResources().getColor(R.color.white));
103 | } else if (attr == R.styleable.LoadingButton_lb_shadowColor) {
104 | mShadowColor = typedArray.getColor(attr, getResources().getColor(R.color.pressed_color));
105 | isShadowColorDefined = true;
106 | } else if (attr == R.styleable.LoadingButton_lb_shadowHeight) {
107 | mShadowHeight = typedArray.getDimensionPixelSize(attr, R.dimen.fbutton_default_shadow_height);
108 | } else if (attr == R.styleable.LoadingButton_lb_cornerRadius) {
109 | mCornerRadius = typedArray.getDimensionPixelSize(attr, R.dimen.fbutton_default_conner_radius);
110 | } else if (attr == R.styleable.LoadingButton_lb_isCircular) {
111 | isCircularButton = typedArray.getBoolean(attr, false);
112 | } else if (attr == R.styleable.LoadingButton_lb_isLoading) {
113 | isLoading = typedArray.getBoolean(attr, false);
114 | } else if (attr == R.styleable.LoadingButton_lb_loaderMargin) {
115 | mPaddingProgress = mCornerRadius = typedArray.getDimensionPixelSize(attr, R.dimen.fbutton_default_progress_margin);
116 | } else if (attr == R.styleable.LoadingButton_lb_loaderWidth) {
117 | mStrokeWidth = mCornerRadius = typedArray.getDimensionPixelSize(attr, R.dimen.fbutton_default_progress_width);
118 |
119 | }
120 | }
121 | typedArray.recycle();
122 |
123 | //Get paddingLeft, paddingRight
124 | int[] attrsArray = new int[]{
125 | android.R.attr.paddingLeft, // 0
126 | android.R.attr.paddingRight, // 1
127 | };
128 | TypedArray ta = context.obtainStyledAttributes(attrs, attrsArray);
129 | if (ta == null) return;
130 | mPaddingLeft = ta.getDimensionPixelSize(0, 0);
131 | mPaddingRight = ta.getDimensionPixelSize(1, 0);
132 | ta.recycle();
133 |
134 | //Get paddingTop, paddingBottom
135 | int[] attrsArray2 = new int[]{
136 | android.R.attr.paddingTop, // 0
137 | android.R.attr.paddingBottom,// 1
138 | };
139 | TypedArray ta1 = context.obtainStyledAttributes(attrs, attrsArray2);
140 | if (ta1 == null) return;
141 | mPaddingTop = ta1.getDimensionPixelSize(0, 0);
142 | mPaddingBottom = ta1.getDimensionPixelSize(1, 0);
143 | ta1.recycle();
144 | }
145 |
146 |
147 | public void refresh() {
148 | int alpha = Color.alpha(mButtonColor);
149 | float[] hsv = new float[3];
150 | Color.colorToHSV(mButtonColor, hsv);
151 | hsv[2] *= 0.8f; // value component
152 |
153 | //if shadow color was not defined, generate shadow color = 80% brightness
154 | if (!isShadowColorDefined) {
155 | mShadowColor = Color.HSVToColor(alpha, hsv);
156 | }
157 | //Create pressed background and unpressed background drawables
158 |
159 | if (this.isEnabled()) {
160 | if (isShadowEnabled) {
161 | pressedDrawable = createDrawable(mCornerRadius, Color.TRANSPARENT, mButtonColor);
162 | unpressedDrawable = createDrawable(mCornerRadius, mButtonColor, mShadowColor);
163 | } else {
164 | mShadowHeight = 0;
165 | pressedDrawable = createDrawable(mCornerRadius, mShadowColor, Color.TRANSPARENT);
166 | unpressedDrawable = createDrawable(mCornerRadius, mButtonColor, Color.TRANSPARENT);
167 | }
168 | } else {
169 | Color.colorToHSV(mButtonColor, hsv);
170 | hsv[1] *= 0.60f; // saturation component
171 | int disabledColor = mShadowColor = Color.HSVToColor(alpha, hsv);
172 | // Disabled button does not have shadow
173 | pressedDrawable = createDrawable(mCornerRadius, disabledColor, Color.TRANSPARENT);
174 | unpressedDrawable = createDrawable(mCornerRadius, disabledColor, Color.TRANSPARENT);
175 | }
176 | updateBackground(unpressedDrawable);
177 | //Set padding
178 | this.setPadding(mPaddingLeft, mPaddingTop + mShadowHeight, mPaddingRight, mPaddingBottom + mShadowHeight);
179 | }
180 |
181 | private void updateBackground(Drawable background) {
182 | if (background == null) return;
183 | //Set button background
184 | this.setBackground(background);
185 | }
186 |
187 | private LayerDrawable createDrawable(int radius, int topColor, int bottomColor) {
188 |
189 | float[] outerRadius = new float[]{radius, radius, radius, radius, radius, radius, radius, radius};
190 |
191 | //Top
192 | RoundRectShape topRoundRect = new RoundRectShape(outerRadius, null, null);
193 | ShapeDrawable topShapeDrawable = new ShapeDrawable(topRoundRect);
194 | topShapeDrawable.getPaint().setColor(topColor);
195 | //Bottom
196 | RoundRectShape roundRectShape = new RoundRectShape(outerRadius, null, null);
197 | ShapeDrawable bottomShapeDrawable = new ShapeDrawable(roundRectShape);
198 | bottomShapeDrawable.getPaint().setColor(bottomColor);
199 | //Create array
200 | Drawable[] drawArray = {bottomShapeDrawable, topShapeDrawable};
201 | LayerDrawable layerDrawable = new LayerDrawable(drawArray);
202 |
203 | //Set shadow height
204 | if (isShadowEnabled && topColor != Color.TRANSPARENT) {
205 | //unpressed drawable
206 | layerDrawable.setLayerInset(0, 0, 0, 0, 0); /*index, left, top, right, bottom*/
207 | } else {
208 | //pressed drawable
209 | layerDrawable.setLayerInset(0, 0, mShadowHeight, 0, 0); /*index, left, top, right, bottom*/
210 | }
211 | layerDrawable.setLayerInset(1, 0, 0, 0, mShadowHeight); /*index, left, top, right, bottom*/
212 |
213 | return layerDrawable;
214 |
215 | }
216 |
217 | @Override
218 | public void setEnabled(boolean enabled) {
219 | super.setEnabled(enabled);
220 | refresh();
221 | }
222 |
223 | //Getter
224 | public boolean isShadowEnabled() {
225 | return isShadowEnabled;
226 | }
227 |
228 | //Setter
229 | public void setShadowEnabled(boolean isShadowEnabled) {
230 | this.isShadowEnabled = isShadowEnabled;
231 | refresh();
232 | }
233 |
234 | public int getButtonColor() {
235 | return mButtonColor;
236 | }
237 |
238 | public void setButtonColor(int buttonColor) {
239 | this.mButtonColor = buttonColor;
240 | refresh();
241 | }
242 |
243 | public int getShadowColor() {
244 | return mShadowColor;
245 | }
246 |
247 | public void setShadowColor(int shadowColor) {
248 | this.mShadowColor = shadowColor;
249 | isShadowColorDefined = true;
250 | refresh();
251 | }
252 |
253 | public int getShadowHeight() {
254 | return mShadowHeight;
255 | }
256 |
257 | public void setShadowHeight(int shadowHeight) {
258 | this.mShadowHeight = shadowHeight;
259 | refresh();
260 | }
261 |
262 | public void setCornerRadius(int cornerRadius) {
263 | this.mCornerRadius = cornerRadius;
264 | refresh();
265 | }
266 |
267 |
268 | @Override
269 | public boolean onTouch(View view, MotionEvent motionEvent) {
270 | switch (motionEvent.getAction()) {
271 | case MotionEvent.ACTION_DOWN:
272 | updateBackground(pressedDrawable);
273 | this.setPadding(mPaddingLeft, mPaddingTop + mShadowHeight, mPaddingRight, mPaddingBottom);
274 | break;
275 | case MotionEvent.ACTION_MOVE:
276 | Rect r = new Rect();
277 | view.getLocalVisibleRect(r);
278 | if (!r.contains((int) motionEvent.getX(), (int) motionEvent.getY() + 3 * mShadowHeight) &&
279 | !r.contains((int) motionEvent.getX(), (int) motionEvent.getY() - 3 * mShadowHeight)) {
280 | updateBackground(unpressedDrawable);
281 | this.setPadding(mPaddingLeft, mPaddingTop + mShadowHeight, mPaddingRight, mPaddingBottom + mShadowHeight);
282 | }
283 | break;
284 | case MotionEvent.ACTION_OUTSIDE:
285 | case MotionEvent.ACTION_CANCEL:
286 | case MotionEvent.ACTION_UP:
287 | updateBackground(unpressedDrawable);
288 | this.setPadding(mPaddingLeft, mPaddingTop + mShadowHeight, mPaddingRight, mPaddingBottom + mShadowHeight);
289 | break;
290 | }
291 | return false;
292 | }
293 |
294 | @Override
295 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
296 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
297 | int widthSize = MeasureSpec.getSize(widthMeasureSpec);
298 | if (isCircularButton) {
299 | mCornerRadius = widthSize / 2;
300 | refresh();
301 | }
302 | }
303 |
304 | @Override
305 | protected void onDraw(Canvas canvas) {
306 | super.onDraw(canvas);
307 | mcanvas = canvas;
308 | if (isLoading) {
309 | drawIndeterminateProgress(canvas);
310 | setText("");
311 | } else {
312 | if (text.length() != 0)
313 | setText(text);
314 | }
315 | }
316 |
317 | private void drawIndeterminateProgress(Canvas canvas) {
318 | if (mAnimatedDrawable == null) {
319 | int offset = (getWidth() - getHeight()) / 2;
320 | //mColorIndicator = getResources().getColor(R.color.white);
321 | mColorIndicator = mloaderColor;
322 | mAnimatedDrawable = new CircularAnimatedDrawable(mColorIndicator, mStrokeWidth);
323 | int left = offset + mPaddingProgress;
324 | int right = getWidth() - offset - mPaddingProgress;
325 | int bottom = getHeight() - mPaddingProgress;
326 | int top = mPaddingProgress;
327 | mAnimatedDrawable.setBounds(left, top, right, bottom);
328 | mAnimatedDrawable.setCallback(this);
329 | mAnimatedDrawable.start();
330 | } else {
331 | mAnimatedDrawable.draw(canvas);
332 | }
333 | }
334 |
335 | private void setLoading(boolean loading) {
336 | isLoading = loading;
337 | if (isLoading) {
338 | drawIndeterminateProgress(mcanvas);
339 | setText("");
340 | } else {
341 | if (text.length() != 0)
342 | setText(text);
343 | }
344 | }
345 |
346 | public String getButtonText() {
347 | return text;
348 | }
349 |
350 | public void setButtonText(String text) {
351 | this.text = text;
352 | }
353 |
354 | public void showLoading() {
355 | setLoading(true);
356 | }
357 |
358 | public void hideLoading() {
359 | setLoading(false);
360 | }
361 | }
362 |
--------------------------------------------------------------------------------
/loadingbutton/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/loadingbutton/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #fff44336
4 | #ffd32f2f
5 | #ffeeeeee
6 | #00000000
7 | #FFF
8 | #000
9 | #3eadeb
10 | #ff3493c8
11 |
--------------------------------------------------------------------------------
/loadingbutton/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 8dp
5 | 10dp
6 | 10dp
7 | 5dp
8 | 5dp
9 | 4dp
10 | 15dp
11 | 10dp
12 |
--------------------------------------------------------------------------------
/loadingbutton/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | LoadingButton
3 |
4 |
--------------------------------------------------------------------------------
/loadingbutton/src/test/java/com/kusu/loadingbutton/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.kusu.loadingbutton;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/sampledemo.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/koushikcse/LoadingButton/2ac5f014ad70314c9885f3064bdc50db3ca69810/sampledemo.gif
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':loadingbutton'
2 |
--------------------------------------------------------------------------------