├── .gitignore ├── README.md ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── library ├── .gitignore ├── build.gradle └── src │ ├── androidTest │ └── java │ │ └── ca │ │ └── abushawish │ │ └── multistatetogglebutton │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── ca │ │ │ └── abushawish │ │ │ └── multistatetogglebutton │ │ │ └── MultiStateToggleButton.java │ └── res │ │ └── values │ │ ├── attrs.xml │ │ └── strings.xml │ └── test │ └── java │ └── ca │ └── abushawish │ └── multistatetogglebutton │ └── ExampleUnitTest.java ├── mstb-gif-record.gif ├── sample ├── .gitignore ├── build.gradle └── src │ ├── androidTest │ └── java │ │ └── ca │ │ └── abushawish │ │ └── multistatetogglebutton │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── ca │ │ │ └── abushawish │ │ │ └── multistatetogglebutton │ │ │ └── sample │ │ │ └── SampleActivity.java │ └── res │ │ ├── drawable │ │ ├── ic_filter_1_black_24px.xml │ │ ├── ic_filter_2_black_24px.xml │ │ ├── ic_filter_3_black_24px.xml │ │ ├── ic_filter_4_black_24px.xml │ │ ├── ic_filter_5_black_24px.xml │ │ ├── ic_filter_6_black_24px.xml │ │ ├── ic_filter_7_black_24px.xml │ │ ├── ic_flash_auto_black_24px.xml │ │ ├── ic_flash_off_black_24px.xml │ │ └── ic_flash_on_black_24px.xml │ │ ├── layout │ │ └── activity_sample.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── arrays.xml │ │ ├── colors.xml │ │ ├── dimen.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── ca │ └── abushawish │ └── multistatetogglebutton │ └── ExampleUnitTest.java └── settings.gradle /.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/dictionaries 41 | .idea/libraries 42 | 43 | # Keystore files 44 | *.jks 45 | 46 | # External native build folder generated in Android Studio 2.2 and later 47 | .externalNativeBuild 48 | 49 | # Google Services (e.g. APIs or Firebase) 50 | google-services.json 51 | 52 | # Freeline 53 | freeline.py 54 | freeline/ 55 | freeline_project_description.json 56 | 57 | #Android generated 58 | bin 59 | gen 60 | 61 | #Eclipse 62 | .project 63 | .classpath 64 | .settings 65 | 66 | #IntelliJ IDEA 67 | .idea 68 | *.iml 69 | *.ipr 70 | *.iws 71 | out 72 | 73 | #Gradle 74 | .gradle 75 | build 76 | 77 | #Maven 78 | target 79 | release.properties 80 | pom.xml.* 81 | project.properties 82 | 83 | #Ant 84 | build.xml 85 | local.properties 86 | gradle.properties 87 | proguard.cfg 88 | 89 | #OSX 90 | .DS_Store/ 91 | 92 | # Crashlytics 93 | com_crashlytics_export_strings.xml 94 | crashlytics-build.properties 95 | crashlytics.properties 96 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | MultiState Toggle Button 2 | =============== 3 | 4 | Android's ToggleButton offers only two states, MultiStateToggleButton fixes this by offering as many states depending on the number of drawable resources passed in. 5 | 6 | ![](https://github.com/Abushawish/MultiStateToggleButton/blob/master/mstb-gif-record.gif) 7 | 8 | Including in your project 9 | ------------------------- 10 | 11 | ```groovy 12 | compile 'ca.abushawish.multistatetogglebutton:multistatetogglebutton:1.0' 13 | ``` 14 | 15 | Check here for the latest [Releases](https://github.com/Abushawish/MultiStateToggleButton/releases). 16 | 17 | Usage 18 | ----- 19 | 20 | Create an integer-array of drawable resource IDs in your [arrays.xml](https://developer.android.com/samples/MediaRouter/res/values/arrays.html) 21 | 22 | ```xml 23 | 24 | 25 | 26 | @drawable/ic_flash_auto_black_24px 27 | @drawable/ic_flash_on_black_24px 28 | @drawable/ic_flash_off_black_24px 29 | 30 | 31 | ``` 32 | 33 | Then, in your layout file, add the 'MultiStateToggleButton' with the 'app:drawable_resource_list' attribute and the reference to the above created integer-array as it's value. 34 | 35 | ```xml 36 | 42 | ``` 43 | 44 | **Alternativaly, programatically:** 45 | 46 | Initialize the ```MultiStateToggleButton``` and populate a ```List``` of your drawable resource IDs. 47 | 48 | ```java 49 | private MultiStateToggleButton mMultiStateToggleButton = (MultiStateToggleButton) findViewById(R.id.mstb_sample_button); 50 | 51 | private List mDrawableResourceIDs; = new ArrayList<>(); 52 | mDrawableResourceIDs.add(R.drawable.ic_filter_1_black_24px); 53 | mDrawableResourceIDs.add(R.drawable.ic_filter_2_black_24px); 54 | ... 55 | mDrawableResourceIDs.add(R.drawable.ic_filter_7_black_24px); 56 | ``` 57 | 58 | Then, set the populated drawable resource ID list to the multiStateToggleButton object as so: 59 | 60 | ```java 61 | mMultiStateToggleButton.setDrawableResourceList(mDrawableResourceIDs); 62 | ``` 63 | 64 | Implement the `MultiStateToggleButton.OnStateChangeListener`. 65 | 66 | ```java 67 | public class SampleActivity extends Activity implements MultiStateToggleButton.OnStateChangeListener{ 68 | 69 | ... 70 | 71 | @Override 72 | public void onStateChanged(View view, int newState) { 73 | switch (view.getId()) { 74 | case R.id.mstb_sample_button: 75 | // Do something on state changed or with the int newState 76 | break; 77 | } 78 | } 79 | } 80 | ``` 81 | 82 | Developed By 83 | -------------------- 84 | Moe Abushawish 85 | 86 | License 87 | ----------- 88 | 89 | ``` 90 | Copyright 2017 Mohammed Abushawish 91 | 92 | Licensed under the Apache License, Version 2.0 (the "License"); 93 | you may not use this file except in compliance with the License. 94 | You may obtain a copy of the License at 95 | 96 | http://www.apache.org/licenses/LICENSE-2.0 97 | 98 | Unless required by applicable law or agreed to in writing, software 99 | distributed under the License is distributed on an "AS IS" BASIS, 100 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 101 | See the License for the specific language governing permissions and 102 | limitations under the License. 103 | ``` 104 | -------------------------------------------------------------------------------- /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 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:2.3.3' 9 | 10 | // NOTE: Do not place your application dependencies here; they belong 11 | // in the individual module build.gradle files 12 | } 13 | } 14 | 15 | allprojects { 16 | repositories { 17 | jcenter() 18 | } 19 | } 20 | 21 | task clean(type: Delete) { 22 | delete rootProject.buildDir 23 | } 24 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Abushawish/MultiStateToggleButton/e9c784cc005930fb82a6fce43425dd486e50421c/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sat Jul 01 17:56:12 EDT 2017 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-3.3-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /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 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 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 Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /library/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /library/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | ext { 4 | PUBLISH_GROUP_ID = 'ca.abushawish.multistatetogglebutton' 5 | PUBLISH_ARTIFACT_ID = 'multistatetogglebutton' 6 | PUBLISH_VERSION = "1.0" 7 | } 8 | 9 | android { 10 | compileSdkVersion 25 11 | buildToolsVersion "25.0.3" 12 | 13 | defaultConfig { 14 | minSdkVersion 16 15 | targetSdkVersion 25 16 | versionCode 1 17 | versionName "1.0" 18 | 19 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 20 | 21 | } 22 | buildTypes { 23 | release { 24 | minifyEnabled false 25 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 26 | } 27 | } 28 | } 29 | 30 | dependencies { 31 | compile fileTree(dir: 'libs', include: ['*.jar']) 32 | 33 | // Android 34 | compile 'com.android.support:appcompat-v7:25.3.1' 35 | 36 | // Test 37 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 38 | exclude group: 'com.android.support', module: 'support-annotations' 39 | }) 40 | testCompile 'junit:junit:4.12' 41 | } 42 | 43 | apply from: 'https://raw.githubusercontent.com/blundell/release-android-library/master/android-release-aar.gradle' 44 | -------------------------------------------------------------------------------- /library/src/androidTest/java/ca/abushawish/multistatetogglebutton/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package ca.abushawish.multistatetogglebutton; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation 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() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("ca.abushawish.multistatetogglebutton.test", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /library/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /library/src/main/java/ca/abushawish/multistatetogglebutton/MultiStateToggleButton.java: -------------------------------------------------------------------------------- 1 | package ca.abushawish.multistatetogglebutton; 2 | 3 | import android.content.Context; 4 | import android.content.res.TypedArray; 5 | import android.support.annotation.IntRange; 6 | import android.support.annotation.NonNull; 7 | import android.support.annotation.Nullable; 8 | import android.support.annotation.Size; 9 | import android.support.v7.widget.AppCompatImageButton; 10 | import android.util.AttributeSet; 11 | import android.util.Log; 12 | import android.view.View; 13 | 14 | import java.util.ArrayList; 15 | import java.util.List; 16 | 17 | /** 18 | * Android's regular {@link android.widget.ToggleButton} offers only two states for the toggle (checked and unchecked), 19 | * MultiStateToggleButton fixes that by offering as many states as needed depending on the number of 20 | * drawable resource IDs in the list passed into this class, using {@link MultiStateToggleButton#setDrawableResourceList(List)} 21 | * or assigning an XML array into the drawable_resource_list attribute in the XML. 22 | * 23 | * @author mabushawish 24 | * @version 1.0.0 25 | */ 26 | public class MultiStateToggleButton extends AppCompatImageButton implements View.OnClickListener { 27 | 28 | private final String TAG = this.getClass().getSimpleName(); 29 | private final int INVALID_RES_DEFAULT = -1; 30 | private final int STARTING_STATE_DEFAULT = 0; 31 | 32 | private List mDrawableResourceList; 33 | private OnClickListener mWrappedOnClickListener; 34 | private OnStateChangeListener mOnStateChangeListener; 35 | private int mCurrentState; 36 | 37 | public MultiStateToggleButton(Context context) { 38 | super(context); 39 | if (!isInEditMode()) { 40 | init(context, null); 41 | } 42 | } 43 | 44 | public MultiStateToggleButton(Context context, AttributeSet attrs) { 45 | super(context, attrs); 46 | if (!isInEditMode()) { 47 | init(context, attrs); 48 | } 49 | } 50 | 51 | public MultiStateToggleButton(Context context, AttributeSet attrs, int defStyle) { 52 | super(context, attrs, defStyle); 53 | if (!isInEditMode()) { 54 | init(context, attrs); 55 | } 56 | } 57 | 58 | /** 59 | * Initializes the class and extracts XML attributes 60 | * 61 | * @param context The calling Android context 62 | * @param attrs The collection of XML attributes 63 | */ 64 | private void init(@NonNull Context context, @Nullable AttributeSet attrs) { 65 | mDrawableResourceList = new ArrayList<>(); 66 | mCurrentState = STARTING_STATE_DEFAULT; 67 | 68 | super.setOnClickListener(this); 69 | 70 | TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, 71 | R.styleable.MultiStateToggleButton, 0, 0); 72 | 73 | try { 74 | int arrayResourcesId = typedArray.getResourceId(R.styleable.MultiStateToggleButton_drawable_resource_list, INVALID_RES_DEFAULT); 75 | 76 | if (arrayResourcesId != INVALID_RES_DEFAULT) { 77 | final TypedArray resourcesArray = getResources().obtainTypedArray(arrayResourcesId); 78 | 79 | for (int i = 0; i < resourcesArray.length(); i++) { 80 | final int resourceId = resourcesArray.getResourceId(i, INVALID_RES_DEFAULT); 81 | if (resourceId != INVALID_RES_DEFAULT) { 82 | mDrawableResourceList.add(resourceId); 83 | } else { 84 | throw new IllegalArgumentException(TAG + ": Invalid drawable resource ID found in the " + 85 | "given mDrawableResourceList (drawable_resource_list in XML)"); 86 | } 87 | } 88 | 89 | if (resourcesArray.length() < 1) { 90 | Log.w(TAG, "Empty mDrawableResourceList (drawable_resource_list in XML), is this intentional?"); 91 | } else { 92 | // Set default state to first drawable resource if array isn't empty 93 | stateChangedAction(STARTING_STATE_DEFAULT); 94 | } 95 | 96 | resourcesArray.recycle(); 97 | } else { 98 | Log.w(TAG, "No given/valid mDrawableResourceList (drawable_resource_list in XML), is this intentional?"); 99 | } 100 | 101 | } finally { 102 | typedArray.recycle(); 103 | } 104 | } 105 | 106 | @Override 107 | public void setOnClickListener(OnClickListener onClickListener) { 108 | /* Need mWrappedOnClickListener because otherwise the current onClickListener will get overwritten 109 | when setOnClickListener is called on an instance of this class. */ 110 | mWrappedOnClickListener = onClickListener; 111 | } 112 | 113 | @Override 114 | public void onClick(View view) { 115 | if (mWrappedOnClickListener != null) { 116 | mWrappedOnClickListener.onClick(view); 117 | } 118 | nextState(); 119 | } 120 | 121 | /** 122 | * Assigns a listener to trigger for when the current state changes 123 | * 124 | * @param onStateChangeListener The callback to be invoked when the state is changed 125 | */ 126 | public void setOnStateChangesListener(@NonNull OnStateChangeListener onStateChangeListener) { 127 | mOnStateChangeListener = onStateChangeListener; 128 | } 129 | 130 | /** 131 | * Gets the current state 132 | * 133 | * @return The current state (> 0 and < size of drawableResourceList - 1) 134 | */ 135 | public int getCurrentState() { 136 | return mCurrentState; 137 | } 138 | 139 | /** 140 | * Manually set the current state. 141 | * 142 | * @param stateToSetFor The state to set for (> 0 and < size of drawableResourceList - 1). 143 | */ 144 | public void setState(@IntRange(from = 0) int stateToSetFor) { 145 | if (stateToSetFor < 0 || stateToSetFor > mDrawableResourceList.size() - 1) { 146 | throw new IllegalArgumentException(TAG + ": The given state to set for (" + stateToSetFor + ") is either less than zero or greater than " + 147 | "the size of the given mDrawableResourceList (drawable_resource_list in XML)."); 148 | } 149 | 150 | mCurrentState = stateToSetFor; 151 | 152 | stateChangedAction(stateToSetFor); 153 | } 154 | 155 | /** 156 | * Programmatically assign the drawableResourceList to set from when the state changes 157 | * 158 | * @param drawableResourceList A list of drawable resource IDs (R.drawable.pic_name_here) - Non-null, min size of 1. 159 | */ 160 | public void setDrawableResourceList(@NonNull @Size(min = 1) List drawableResourceList) { 161 | mDrawableResourceList = drawableResourceList; 162 | stateChangedAction(STARTING_STATE_DEFAULT); 163 | } 164 | 165 | 166 | /** 167 | * Increments the current state, wrapping to 0 if greater than mDrawableResourceList 168 | */ 169 | private void nextState() { 170 | mCurrentState++; 171 | 172 | if (mCurrentState > mDrawableResourceList.size() - 1) { 173 | mCurrentState = STARTING_STATE_DEFAULT; 174 | } 175 | 176 | stateChangedAction(mCurrentState); 177 | } 178 | 179 | /** 180 | * A private function that performs the various actions that occur following a state change (informing 181 | * listeners, setting current viewable image resource, and some error checking. 182 | * 183 | * @param stateToSetFor The current state being assigned 184 | */ 185 | private void stateChangedAction(@IntRange(from = 0) int stateToSetFor) { 186 | if (stateToSetFor >= 0 && stateToSetFor < mDrawableResourceList.size()) { 187 | setDrawableResource(stateToSetFor); 188 | if (mOnStateChangeListener != null) { 189 | mOnStateChangeListener.onStateChanged(this, stateToSetFor); 190 | } 191 | } 192 | } 193 | 194 | /** 195 | * Sets the current visible image resource from the mDrawableResourceList based on the given stateToSetFor 196 | * 197 | * @param stateToSetFor The state to assign the current image for 198 | */ 199 | private void setDrawableResource(int stateToSetFor) { 200 | setImageResource(mDrawableResourceList.get(stateToSetFor)); 201 | invalidate(); 202 | requestLayout(); 203 | } 204 | 205 | /** 206 | * Interface definition for a callback to be invoked when the state is changed. 207 | */ 208 | public interface OnStateChangeListener { 209 | 210 | /** 211 | * Called when the current state has changed 212 | * 213 | * @param view The view who's state just changed 214 | * @param newState The current state produced following the click. 215 | */ 216 | void onStateChanged(View view, int newState); 217 | } 218 | } -------------------------------------------------------------------------------- /library/src/main/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /library/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | MultiStateToggleButton 3 | 4 | -------------------------------------------------------------------------------- /library/src/test/java/ca/abushawish/multistatetogglebutton/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package ca.abushawish.multistatetogglebutton; 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() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /mstb-gif-record.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Abushawish/MultiStateToggleButton/e9c784cc005930fb82a6fce43425dd486e50421c/mstb-gif-record.gif -------------------------------------------------------------------------------- /sample/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.3" 6 | defaultConfig { 7 | applicationId "ca.abushawish.multistatetogglebutton.sample" 8 | minSdkVersion 16 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | 24 | // Android 25 | compile fileTree(dir: 'libs', include: ['*.jar']) 26 | compile 'com.android.support:appcompat-v7:25.3.1' 27 | compile 'com.android.support.constraint:constraint-layout:1.0.2' 28 | 29 | // MultiStateToggleButton library 30 | compile project(":library") 31 | 32 | // Test 33 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 34 | exclude group: 'com.android.support', module: 'support-annotations' 35 | }) 36 | testCompile 'junit:junit:4.12' 37 | 38 | // Butterknife 39 | compile 'com.jakewharton:butterknife:8.6.0' 40 | annotationProcessor 'com.jakewharton:butterknife-compiler:8.6.0' 41 | } 42 | -------------------------------------------------------------------------------- /sample/src/androidTest/java/ca/abushawish/multistatetogglebutton/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package ca.abushawish.multistatetogglebutton; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation 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() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("ca.abushawish.multistatetogglebutton", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /sample/src/main/java/ca/abushawish/multistatetogglebutton/sample/SampleActivity.java: -------------------------------------------------------------------------------- 1 | package ca.abushawish.multistatetogglebutton.sample; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.view.View; 6 | import android.widget.Toast; 7 | 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | 11 | import butterknife.BindView; 12 | import butterknife.ButterKnife; 13 | import ca.abushawish.multistatetogglebutton.MultiStateToggleButton; 14 | 15 | public class SampleActivity extends AppCompatActivity implements MultiStateToggleButton.OnStateChangeListener{ 16 | 17 | @BindView(R.id.mstb_sample_button_1) 18 | protected MultiStateToggleButton mMultiStateToggleButton1; 19 | 20 | @BindView(R.id.mstb_sample_button_2) 21 | protected MultiStateToggleButton mMultiStateToggleButton2; 22 | 23 | private List mDrawableResourceIDs; 24 | 25 | @Override 26 | protected void onCreate(Bundle savedInstanceState) { 27 | super.onCreate(savedInstanceState); 28 | setContentView(R.layout.activity_sample); 29 | ButterKnife.bind(this); 30 | 31 | mDrawableResourceIDs = new ArrayList<>(); 32 | mDrawableResourceIDs.add(R.drawable.ic_filter_1_black_24px); 33 | mDrawableResourceIDs.add(R.drawable.ic_filter_2_black_24px); 34 | mDrawableResourceIDs.add(R.drawable.ic_filter_3_black_24px); 35 | mDrawableResourceIDs.add(R.drawable.ic_filter_4_black_24px); 36 | mDrawableResourceIDs.add(R.drawable.ic_filter_5_black_24px); 37 | mDrawableResourceIDs.add(R.drawable.ic_filter_6_black_24px); 38 | mDrawableResourceIDs.add(R.drawable.ic_filter_7_black_24px); 39 | 40 | mMultiStateToggleButton1.setOnStateChangesListener(this); 41 | mMultiStateToggleButton2.setOnStateChangesListener(this); 42 | mMultiStateToggleButton2.setDrawableResourceList(mDrawableResourceIDs); 43 | } 44 | 45 | @Override 46 | public void onStateChanged(View view, int newState) { 47 | 48 | String newStateString = null; 49 | switch (view.getId()) { 50 | case R.id.mstb_sample_button_1: 51 | newStateString = getResources().getString(R.string.current_state, newState); 52 | break; 53 | case R.id.mstb_sample_button_2: 54 | newStateString = getResources().getString(R.string.current_state, newState); 55 | break; 56 | } 57 | 58 | if (newStateString != null) { 59 | Toast.makeText(this, newStateString, Toast.LENGTH_SHORT).show(); 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/ic_filter_1_black_24px.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/ic_filter_2_black_24px.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/ic_filter_3_black_24px.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/ic_filter_4_black_24px.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/ic_filter_5_black_24px.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/ic_filter_6_black_24px.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/ic_filter_7_black_24px.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/ic_flash_auto_black_24px.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/ic_flash_off_black_24px.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 10 | 14 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/ic_flash_on_black_24px.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /sample/src/main/res/layout/activity_sample.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 18 | 19 | 24 | 25 | 34 | 35 | 36 | 37 | 44 | 45 | 50 | 51 | 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Abushawish/MultiStateToggleButton/e9c784cc005930fb82a6fce43425dd486e50421c/sample/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Abushawish/MultiStateToggleButton/e9c784cc005930fb82a6fce43425dd486e50421c/sample/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Abushawish/MultiStateToggleButton/e9c784cc005930fb82a6fce43425dd486e50421c/sample/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Abushawish/MultiStateToggleButton/e9c784cc005930fb82a6fce43425dd486e50421c/sample/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Abushawish/MultiStateToggleButton/e9c784cc005930fb82a6fce43425dd486e50421c/sample/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Abushawish/MultiStateToggleButton/e9c784cc005930fb82a6fce43425dd486e50421c/sample/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Abushawish/MultiStateToggleButton/e9c784cc005930fb82a6fce43425dd486e50421c/sample/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Abushawish/MultiStateToggleButton/e9c784cc005930fb82a6fce43425dd486e50421c/sample/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Abushawish/MultiStateToggleButton/e9c784cc005930fb82a6fce43425dd486e50421c/sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Abushawish/MultiStateToggleButton/e9c784cc005930fb82a6fce43425dd486e50421c/sample/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/values/arrays.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | @drawable/ic_flash_auto_black_24px 5 | @drawable/ic_flash_on_black_24px 6 | @drawable/ic_flash_off_black_24px 7 | 8 | -------------------------------------------------------------------------------- /sample/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /sample/src/main/res/values/dimen.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 15dp 4 | 30dp 5 | 50dp 6 | -------------------------------------------------------------------------------- /sample/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | ]> 6 | 7 | 8 | &appname; 9 | Current State: %d 10 | XML attribute assigned, 3-state &appname; with transparent background: 11 | Programmatically set, 7-state &appname; with red background: 12 | 13 | -------------------------------------------------------------------------------- /sample/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /sample/src/test/java/ca/abushawish/multistatetogglebutton/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package ca.abushawish.multistatetogglebutton; 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() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':sample', ':library' 2 | --------------------------------------------------------------------------------