├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── images ├── foo_bar.png ├── gravity_center.png ├── gravity_left.png ├── gravity_right.png ├── gravity_staggered.png ├── label_update_after.png ├── label_update_before.png ├── screenshot.png ├── trainer_sizes.png └── wrapping_example.png ├── library ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── eu │ │ └── fiskur │ │ └── chipcloud │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── adroitandroid │ │ │ └── chipcloud │ │ │ ├── Chip.java │ │ │ ├── ChipCloud.java │ │ │ ├── ChipListener.java │ │ │ └── FlowLayout.java │ └── res │ │ ├── drawable │ │ ├── chip.xml │ │ └── chip_selected.xml │ │ ├── layout │ │ └── chip.xml │ │ └── values │ │ ├── attrs.xml │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── ids.xml │ │ └── strings.xml │ └── test │ └── java │ └── eu │ └── fiskur │ └── chipcloud │ └── ExampleUnitTest.java └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | app 10 | .idea 11 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: android 2 | android: 3 | components: 4 | - build-tools-23.0.3 5 | - android-24 6 | - extra-android-m2repository 7 | - extra-google-m2repository 8 | 9 | script: 10 | - ./gradlew test --continue 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Jonathan Fisher 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 | # ChipCloud 2 | 5 | The ChipCloud library was originally a (very) quickly knocked up Android view for some larger hackathon project by [fiskurgit](https://github.com/fiskurgit). It creates a wrapping cloud of material '[Chips](https://www.google.com/design/spec/components/chips.html)'. Basic demo of their version is [available on the Play Store](https://play.google.com/store/apps/details?id=eu.fiskur.chipclouddemo) - I'm maintaining this fork for features I required in it. 6 | 7 | ![Trainer Sizes](images/trainer_sizes.png) 8 | 9 | ## Usage 10 | 11 | Add to your Android layout xml: 12 | ```xml 13 | 17 | ``` 18 | 19 | Configure in xml: 20 | ```xml 21 | 40 | ``` 41 | or in code: 42 | ```java 43 | ChipCloud chipCloud = (ChipCloud) findViewById(R.id.chip_cloud); 44 | 45 | new ChipCloud.Configure() 46 | .chipCloud(chipCloud) 47 | .selectedColor(Color.parseColor("#ff00cc")) 48 | .selectedFontColor(Color.parseColor("#ffffff")) 49 | .deselectedColor(Color.parseColor("#e1e1e1")) 50 | .deselectedFontColor(Color.parseColor("#333333")) 51 | .selectTransitionMS(500) 52 | .deselectTransitionMS(250) 53 | .labels(someStringArray) 54 | .mode(ChipCloud.Mode.MULTI) 55 | .allCaps(false) 56 | .gravity(ChipCloud.Gravity.CENTER) 57 | .textSize(getResources().getDimensionPixelSize(R.dimen.default_textsize)) 58 | .verticalSpacing(getResources().getDimensionPixelSize(R.dimen.vertical_spacing)) 59 | .minHorizontalSpacing(getResources().getDimensionPixelSize(R.dimen.min_horizontal_spacing)) 60 | .typeface(Typeface.createFromAsset(getContext().getAssets(), "RobotoSlab-Regular.ttf")) 61 | .chipListener(new ChipListener() { 62 | @Override 63 | public void chipSelected(int index) { 64 | //... 65 | } 66 | @Override 67 | public void chipDeselected(int index) { 68 | //... 69 | } 70 | }) 71 | .build(); 72 | ``` 73 | 74 | Default value of textSize is equivalent to 13sp, of verticalSpacing is equivalent to 8dp, of minHorizontalSpacing is equivalent to 8dp, of allCaps is false. 75 | 76 | All chips have a height of 32dp and a padding of 12dp on left and right within the chip, [as per material guidelines](https://www.google.com/design/spec/components/chips.html). 77 | 78 | Add items dynamically too: 79 | ```java 80 | chipCloud.addChip("Foo"); 81 | chipCloud.addChip("Bar"); 82 | 83 | //or 84 | 85 | chipCloud.addChips(someStringArray); 86 | ``` 87 | 88 | Set the selected index using ```chipCloud.setSelectedChip(2)``` 89 | 90 | Real-world example for shoe sizes: 91 | ![Shoe Sizes](images/wrapping_example.png) 92 | 93 | Labels can also be updated in-place, e.g. 94 | 95 | from ![Before](images/label_update_before.png) to ![After](images/label_update_after.png) 96 | using ```update()``` as illustrated below 97 | 98 | ```java 99 | new ChipCloud.Configure().chipCloud(binding.chipCloud).labels(newChipLabels).update(); 100 | ``` 101 | 102 | ## Modes 103 | 104 | ```java 105 | public enum Mode { 106 | SINGLE, MULTI, REQUIRED, NONE 107 | } 108 | ``` 109 | 110 | The default mode is single choice (where it's valid to have no chip selected), if you want a RadioGroup manadatory style where once a chip is selected there must always be a selected item use ```chipCloud.setMode(ChipCloud.Mode.REQUIRED);``` (or set in xml or the builder). There's a multiple select mode too: ```chipCloud.setMode(ChipCloud.Mode.MULTIPLE);```. If you want to deactiviate selecting of chips you can set the select mode to ```chipCloud.setMode(ChipCloud.Mode.NONE);```. 111 | 112 | ## Gravity 113 | 114 | ```java 115 | public enum Gravity { 116 | LEFT, RIGHT, CENTER, STAGGERED 117 | } 118 | ``` 119 | 120 | The default gravity is LEFT. CENTER and STAGGERED are similar except that CENTER leaves only minimum horizontal spacing between the chips whereas in STAGGERED chips occupy the available space equally while having at least minimum horizontal spacing between them. 121 | 122 | | ![Left](images/gravity_left.png) | ![Right](images/gravity_right.png) | ![Center](images/gravity_center.png) | ![Staggered](images/gravity_staggered.png) | 123 | |:---:|:---:|:---:|:---:| 124 | | LEFT | RIGHT | CENTER | STAGGERED | 125 | 126 | ## Dependency 127 | 128 | Add jitpack.io to your root build.gradle, eg: 129 | 130 | ```groovy 131 | allprojects { 132 | repositories { 133 | jcenter() 134 | maven { url "https://jitpack.io" } 135 | } 136 | } 137 | ``` 138 | 139 | then add the dependency to your project build.gradle: 140 | 141 | ```groovy 142 | dependencies { 143 | compile fileTree(dir: 'libs', include: ['*.jar']) 144 | compile 'com.github.adroitandroid:ChipCloud:2.2.1' 145 | } 146 | ``` 147 | You can find the latest version in the releases tab above: https://github.com/adroitandroid/ChipCloud/releases 148 | 149 | ## Licence 150 | 151 | Full licence here: https://github.com/fiskurgit/ChipCloud/blob/master/LICENSE 152 | 153 | In short: 154 | 155 | > The MIT License is a permissive license that is short and to the point. It lets people do anything they want with your code as long as they provide attribution back to you and don’t hold you liable. 156 | -------------------------------------------------------------------------------- /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.2.0' 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 | maven { url 'http://oss.sonatype.org/content/repositories/snapshots' } 19 | } 20 | } 21 | 22 | task clean(type: Delete) { 23 | delete rootProject.buildDir 24 | } 25 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adroitandroid/ChipCloud/b49064a25f933fd26978edd5d79d52d5e2aa294a/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Sep 27 11:02:57 GMT 2016 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-2.14.1-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 | -------------------------------------------------------------------------------- /images/foo_bar.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adroitandroid/ChipCloud/b49064a25f933fd26978edd5d79d52d5e2aa294a/images/foo_bar.png -------------------------------------------------------------------------------- /images/gravity_center.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adroitandroid/ChipCloud/b49064a25f933fd26978edd5d79d52d5e2aa294a/images/gravity_center.png -------------------------------------------------------------------------------- /images/gravity_left.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adroitandroid/ChipCloud/b49064a25f933fd26978edd5d79d52d5e2aa294a/images/gravity_left.png -------------------------------------------------------------------------------- /images/gravity_right.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adroitandroid/ChipCloud/b49064a25f933fd26978edd5d79d52d5e2aa294a/images/gravity_right.png -------------------------------------------------------------------------------- /images/gravity_staggered.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adroitandroid/ChipCloud/b49064a25f933fd26978edd5d79d52d5e2aa294a/images/gravity_staggered.png -------------------------------------------------------------------------------- /images/label_update_after.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adroitandroid/ChipCloud/b49064a25f933fd26978edd5d79d52d5e2aa294a/images/label_update_after.png -------------------------------------------------------------------------------- /images/label_update_before.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adroitandroid/ChipCloud/b49064a25f933fd26978edd5d79d52d5e2aa294a/images/label_update_before.png -------------------------------------------------------------------------------- /images/screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adroitandroid/ChipCloud/b49064a25f933fd26978edd5d79d52d5e2aa294a/images/screenshot.png -------------------------------------------------------------------------------- /images/trainer_sizes.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adroitandroid/ChipCloud/b49064a25f933fd26978edd5d79d52d5e2aa294a/images/trainer_sizes.png -------------------------------------------------------------------------------- /images/wrapping_example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adroitandroid/ChipCloud/b49064a25f933fd26978edd5d79d52d5e2aa294a/images/wrapping_example.png -------------------------------------------------------------------------------- /library/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /library/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion '25.0.2' 6 | 7 | defaultConfig { 8 | minSdkVersion 14 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | compile fileTree(dir: 'libs', include: ['*.jar']) 23 | testCompile 'junit:junit:4.12' 24 | compile 'com.android.support:appcompat-v7:25.1.0' 25 | } 26 | -------------------------------------------------------------------------------- /library/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/fish/Library/Android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /library/src/androidTest/java/eu/fiskur/chipcloud/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package eu.fiskur.chipcloud; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /library/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /library/src/main/java/com/adroitandroid/chipcloud/Chip.java: -------------------------------------------------------------------------------- 1 | package com.adroitandroid.chipcloud; 2 | 3 | import android.content.Context; 4 | import android.graphics.PorterDuff; 5 | import android.graphics.PorterDuffColorFilter; 6 | import android.graphics.Typeface; 7 | import android.graphics.drawable.Drawable; 8 | import android.graphics.drawable.TransitionDrawable; 9 | import android.support.v4.content.ContextCompat; 10 | import android.util.AttributeSet; 11 | import android.util.TypedValue; 12 | import android.view.LayoutInflater; 13 | import android.view.View; 14 | 15 | public class Chip extends android.support.v7.widget.AppCompatTextView implements View.OnClickListener { 16 | 17 | private int index = -1; 18 | private boolean selected = false; 19 | private ChipListener listener = null; 20 | private int selectedFontColor = -1; 21 | private int unselectedFontColor = -1; 22 | private TransitionDrawable crossfader; 23 | private int selectTransitionMS = 750; 24 | private int deselectTransitionMS = 500; 25 | private boolean isLocked = false; 26 | private ChipCloud.Mode mode; 27 | 28 | public void setChipListener(ChipListener listener) { 29 | this.listener = listener; 30 | } 31 | 32 | public Chip(Context context) { 33 | super(context); 34 | init(); 35 | } 36 | 37 | public Chip(Context context, AttributeSet attrs) { 38 | super(context, attrs); 39 | init(); 40 | } 41 | 42 | public Chip(Context context, AttributeSet attrs, int defStyleAttr) { 43 | super(context, attrs, defStyleAttr); 44 | init(); 45 | } 46 | 47 | public void initChip(Context context, int index, String label, Typeface typeface, int textSizePx, 48 | boolean allCaps, int selectedColor, int selectedFontColor, int unselectedColor, 49 | int unselectedFontColor, ChipCloud.Mode mode) { 50 | 51 | this.index = index; 52 | this.selectedFontColor = selectedFontColor; 53 | this.unselectedFontColor = unselectedFontColor; 54 | this.mode = mode; 55 | 56 | Drawable selectedDrawable = ContextCompat.getDrawable(context, R.drawable.chip_selected); 57 | 58 | if (selectedColor == -1) { 59 | selectedDrawable.setColorFilter(new PorterDuffColorFilter(ContextCompat.getColor(context, R.color.dark_grey), PorterDuff.Mode.MULTIPLY)); 60 | } else { 61 | selectedDrawable.setColorFilter(new PorterDuffColorFilter(selectedColor, PorterDuff.Mode.MULTIPLY)); 62 | } 63 | 64 | if (selectedFontColor == -1) { 65 | this.selectedFontColor = ContextCompat.getColor(context, R.color.white); 66 | } 67 | 68 | Drawable unselectedDrawable = ContextCompat.getDrawable(context, R.drawable.chip_selected); 69 | if (unselectedColor == -1) { 70 | unselectedDrawable.setColorFilter(new PorterDuffColorFilter(ContextCompat.getColor(context, R.color.light_grey), PorterDuff.Mode.MULTIPLY)); 71 | } else { 72 | unselectedDrawable.setColorFilter(new PorterDuffColorFilter(unselectedColor, PorterDuff.Mode.MULTIPLY)); 73 | } 74 | 75 | if (unselectedFontColor == -1) { 76 | this.unselectedFontColor = ContextCompat.getColor(context, R.color.chip); 77 | } 78 | 79 | Drawable backgrounds[] = new Drawable[2]; 80 | backgrounds[0] = unselectedDrawable; 81 | backgrounds[1] = selectedDrawable; 82 | 83 | crossfader = new TransitionDrawable(backgrounds); 84 | 85 | //Bug reported on KitKat where padding was removed, so we read the padding values then set again after setting background 86 | int leftPad = getPaddingLeft(); 87 | int topPad = getPaddingTop(); 88 | int rightPad = getPaddingRight(); 89 | int bottomPad = getPaddingBottom(); 90 | 91 | setBackgroundCompat(crossfader); 92 | 93 | setPadding(leftPad, topPad, rightPad, bottomPad); 94 | 95 | setText(label); 96 | unselect(); 97 | 98 | if (typeface != null) { 99 | setTypeface(typeface); 100 | } 101 | setAllCaps(allCaps); 102 | if (textSizePx > 0) { 103 | setTextSize(TypedValue.COMPLEX_UNIT_PX, textSizePx); 104 | } 105 | } 106 | 107 | public void setLocked(boolean isLocked) { 108 | this.isLocked = isLocked; 109 | } 110 | 111 | public void setSelectTransitionMS(int selectTransitionMS) { 112 | this.selectTransitionMS = selectTransitionMS; 113 | } 114 | 115 | public void setDeselectTransitionMS(int deselectTransitionMS) { 116 | this.deselectTransitionMS = deselectTransitionMS; 117 | } 118 | 119 | private void init() { 120 | setOnClickListener(this); 121 | } 122 | 123 | @Override 124 | public void onClick(View v) { 125 | if (mode != ChipCloud.Mode.NONE) 126 | if (selected && !isLocked) { 127 | //set as unselected 128 | unselect(); 129 | if (listener != null) { 130 | listener.chipDeselected(index); 131 | } 132 | } else if (!selected) { 133 | //set as selected 134 | crossfader.startTransition(selectTransitionMS); 135 | 136 | setTextColor(selectedFontColor); 137 | if (listener != null) { 138 | listener.chipSelected(index); 139 | } 140 | } 141 | 142 | selected = !selected; 143 | } 144 | 145 | public void select() { 146 | selected = true; 147 | crossfader.startTransition(selectTransitionMS); 148 | setTextColor(selectedFontColor); 149 | if (listener != null) { 150 | listener.chipSelected(index); 151 | } 152 | } 153 | 154 | private void unselect() { 155 | if (selected) { 156 | crossfader.reverseTransition(deselectTransitionMS); 157 | } else { 158 | crossfader.resetTransition(); 159 | } 160 | 161 | setTextColor(unselectedFontColor); 162 | } 163 | 164 | @SuppressWarnings("deprecation") 165 | private void setBackgroundCompat(Drawable background) { 166 | if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) { 167 | setBackgroundDrawable(background); 168 | } else { 169 | setBackground(background); 170 | } 171 | } 172 | 173 | public void deselect() { 174 | unselect(); 175 | selected = false; 176 | } 177 | 178 | public static class ChipBuilder { 179 | private int index; 180 | private String label; 181 | private Typeface typeface; 182 | private int textSizePx; 183 | private boolean allCaps; 184 | private int selectedColor; 185 | private int selectedFontColor; 186 | private int unselectedColor; 187 | private int unselectedFontColor; 188 | private int chipHeight; 189 | private int selectTransitionMS = 750; 190 | private int deselectTransitionMS = 500; 191 | 192 | private ChipListener chipListener; 193 | private ChipCloud.Mode mode; 194 | 195 | public ChipBuilder index(int index) { 196 | this.index = index; 197 | return this; 198 | } 199 | 200 | public ChipBuilder selectedColor(int selectedColor) { 201 | this.selectedColor = selectedColor; 202 | return this; 203 | } 204 | 205 | public ChipBuilder selectedFontColor(int selectedFontColor) { 206 | this.selectedFontColor = selectedFontColor; 207 | return this; 208 | } 209 | 210 | public ChipBuilder unselectedColor(int unselectedColor) { 211 | this.unselectedColor = unselectedColor; 212 | return this; 213 | } 214 | 215 | public ChipBuilder unselectedFontColor(int unselectedFontColor) { 216 | this.unselectedFontColor = unselectedFontColor; 217 | return this; 218 | } 219 | 220 | public ChipBuilder label(String label) { 221 | this.label = label; 222 | return this; 223 | } 224 | 225 | public ChipBuilder typeface(Typeface typeface) { 226 | this.typeface = typeface; 227 | return this; 228 | } 229 | 230 | public ChipBuilder allCaps(boolean allCaps) { 231 | this.allCaps = allCaps; 232 | return this; 233 | } 234 | 235 | public ChipBuilder textSize(int textSizePx) { 236 | this.textSizePx = textSizePx; 237 | return this; 238 | } 239 | 240 | public ChipBuilder chipHeight(int chipHeight) { 241 | this.chipHeight = chipHeight; 242 | return this; 243 | } 244 | 245 | public ChipBuilder chipListener(ChipListener chipListener) { 246 | this.chipListener = chipListener; 247 | return this; 248 | } 249 | 250 | public ChipBuilder mode(ChipCloud.Mode mode) { 251 | this.mode = mode; 252 | return this; 253 | } 254 | 255 | public ChipBuilder selectTransitionMS(int selectTransitionMS) { 256 | this.selectTransitionMS = selectTransitionMS; 257 | return this; 258 | } 259 | 260 | public ChipBuilder deselectTransitionMS(int deselectTransitionMS) { 261 | this.deselectTransitionMS = deselectTransitionMS; 262 | return this; 263 | } 264 | 265 | public Chip build(Context context) { 266 | Chip chip = (Chip) LayoutInflater.from(context).inflate(R.layout.chip, null); 267 | chip.initChip(context, index, label, typeface, textSizePx, allCaps, selectedColor, 268 | selectedFontColor, unselectedColor, unselectedFontColor, mode); 269 | chip.setSelectTransitionMS(selectTransitionMS); 270 | chip.setDeselectTransitionMS(deselectTransitionMS); 271 | chip.setChipListener(chipListener); 272 | chip.setHeight(chipHeight); 273 | return chip; 274 | } 275 | } 276 | } 277 | -------------------------------------------------------------------------------- /library/src/main/java/com/adroitandroid/chipcloud/ChipCloud.java: -------------------------------------------------------------------------------- 1 | package com.adroitandroid.chipcloud; 2 | 3 | import android.content.Context; 4 | import android.content.res.TypedArray; 5 | import android.graphics.Typeface; 6 | import android.util.AttributeSet; 7 | 8 | public class ChipCloud extends FlowLayout implements ChipListener { 9 | 10 | public enum Mode { 11 | SINGLE, MULTI, REQUIRED, NONE 12 | } 13 | 14 | private Context context; 15 | private int chipHeight; 16 | private int selectedColor = -1; 17 | private int selectedFontColor = -1; 18 | private int unselectedColor = -1; 19 | private int unselectedFontColor = -1; 20 | private int selectTransitionMS = 750; 21 | private int deselectTransitionMS = 500; 22 | private Mode mode = Mode.SINGLE; 23 | private Gravity gravity = Gravity.LEFT; 24 | private Typeface typeface; 25 | private boolean allCaps; 26 | private int textSizePx = -1; 27 | private int verticalSpacing; 28 | private int minHorizontalSpacing; 29 | 30 | private ChipListener chipListener; 31 | 32 | public ChipCloud(Context context) { 33 | super(context); 34 | this.context = context; 35 | init(); 36 | } 37 | 38 | public ChipCloud(Context context, AttributeSet attrs) { 39 | super(context, attrs); 40 | this.context = context; 41 | 42 | TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ChipCloud, 0, 0); 43 | int arrayReference = -1; 44 | try { 45 | selectedColor = a.getColor(R.styleable.ChipCloud_selectedColor, -1); 46 | selectedFontColor = a.getColor(R.styleable.ChipCloud_selectedFontColor, -1); 47 | unselectedColor = a.getColor(R.styleable.ChipCloud_deselectedColor, -1); 48 | unselectedFontColor = a.getColor(R.styleable.ChipCloud_deselectedFontColor, -1); 49 | selectTransitionMS = a.getInt(R.styleable.ChipCloud_selectTransitionMS, 750); 50 | deselectTransitionMS = a.getInt(R.styleable.ChipCloud_deselectTransitionMS, 500); 51 | String typefaceString = a.getString(R.styleable.ChipCloud_typeface); 52 | if (typefaceString != null) { 53 | typeface = Typeface.createFromAsset(getContext().getAssets(), typefaceString); 54 | } 55 | textSizePx = a.getDimensionPixelSize(R.styleable.ChipCloud_textSize, 56 | getResources().getDimensionPixelSize(R.dimen.default_textsize)); 57 | allCaps = a.getBoolean(R.styleable.ChipCloud_allCaps, false); 58 | int selectMode = a.getInt(R.styleable.ChipCloud_selectMode, 1); 59 | switch (selectMode) { 60 | case 0: 61 | mode = Mode.SINGLE; 62 | break; 63 | case 1: 64 | mode = Mode.MULTI; 65 | break; 66 | case 2: 67 | mode = Mode.REQUIRED; 68 | break; 69 | case 3: 70 | mode = Mode.NONE; 71 | break; 72 | default: 73 | mode = Mode.SINGLE; 74 | } 75 | int gravityType = a.getInt(R.styleable.ChipCloud_gravity, 0); 76 | switch (gravityType) { 77 | case 0: 78 | gravity = Gravity.LEFT; 79 | break; 80 | case 1: 81 | gravity = Gravity.RIGHT; 82 | break; 83 | case 2: 84 | gravity = Gravity.CENTER; 85 | break; 86 | case 3: 87 | gravity = Gravity.STAGGERED; 88 | break; 89 | default: 90 | gravity = Gravity.LEFT; 91 | break; 92 | } 93 | minHorizontalSpacing = a.getDimensionPixelSize(R.styleable.ChipCloud_minHorizontalSpacing, 94 | getResources().getDimensionPixelSize(R.dimen.min_horizontal_spacing)); 95 | verticalSpacing = a.getDimensionPixelSize(R.styleable.ChipCloud_verticalSpacing, 96 | getResources().getDimensionPixelSize(R.dimen.vertical_spacing)); 97 | arrayReference = a.getResourceId(R.styleable.ChipCloud_labels, -1); 98 | 99 | } finally { 100 | a.recycle(); 101 | } 102 | 103 | init(); 104 | 105 | if (arrayReference != -1) { 106 | String[] labels = getResources().getStringArray(arrayReference); 107 | addChips(labels); 108 | } 109 | } 110 | 111 | @Override 112 | protected int getMinimumHorizontalSpacing() { 113 | return minHorizontalSpacing; 114 | } 115 | 116 | @Override 117 | protected int getVerticalSpacing() { 118 | return verticalSpacing; 119 | } 120 | 121 | @Override 122 | protected Gravity getGravity() { 123 | return gravity; 124 | } 125 | 126 | private void init() { 127 | chipHeight = getResources().getDimensionPixelSize(R.dimen.material_chip_height); 128 | } 129 | 130 | public void setSelectedColor(int selectedColor) { 131 | this.selectedColor = selectedColor; 132 | } 133 | 134 | public void setSelectedFontColor(int selectedFontColor) { 135 | this.selectedFontColor = selectedFontColor; 136 | } 137 | 138 | public void setUnselectedColor(int unselectedColor) { 139 | this.unselectedColor = unselectedColor; 140 | } 141 | 142 | public void setUnselectedFontColor(int unselectedFontColor) { 143 | this.unselectedFontColor = unselectedFontColor; 144 | } 145 | 146 | public void setSelectTransitionMS(int selectTransitionMS) { 147 | this.selectTransitionMS = selectTransitionMS; 148 | } 149 | 150 | public void setDeselectTransitionMS(int deselectTransitionMS) { 151 | this.deselectTransitionMS = deselectTransitionMS; 152 | } 153 | 154 | public void setMode(Mode mode) { 155 | this.mode = mode; 156 | for (int i = 0; i < getChildCount(); i++) { 157 | Chip chip = (Chip) getChildAt(i); 158 | chip.deselect(); 159 | chip.setLocked(false); 160 | } 161 | } 162 | 163 | public void setGravity(Gravity gravity) { 164 | this.gravity = gravity; 165 | } 166 | 167 | public void setTypeface(Typeface typeface) { 168 | this.typeface = typeface; 169 | } 170 | 171 | public void setTextSize(int textSize) { 172 | this.textSizePx = textSize; 173 | } 174 | 175 | public void setAllCaps(boolean isAllCaps) { 176 | this.allCaps = isAllCaps; 177 | } 178 | 179 | public void setMinimumHorizontalSpacing(int spacingInPx) { 180 | this.minHorizontalSpacing = spacingInPx; 181 | } 182 | 183 | public void setVerticalSpacing(int spacingInPx) { 184 | this.verticalSpacing = spacingInPx; 185 | } 186 | 187 | public void setChipListener(ChipListener chipListener) { 188 | this.chipListener = chipListener; 189 | } 190 | 191 | public void addChips(String[] labels) { 192 | for (String label : labels) { 193 | addChip(label); 194 | } 195 | } 196 | 197 | public void addChip(String label) { 198 | Chip chip = new Chip.ChipBuilder().index(getChildCount()) 199 | .label(label) 200 | .typeface(typeface) 201 | .textSize(textSizePx) 202 | .allCaps(allCaps) 203 | .selectedColor(selectedColor) 204 | .selectedFontColor(selectedFontColor) 205 | .unselectedColor(unselectedColor) 206 | .unselectedFontColor(unselectedFontColor) 207 | .selectTransitionMS(selectTransitionMS) 208 | .deselectTransitionMS(deselectTransitionMS) 209 | .chipHeight(chipHeight) 210 | .chipListener(this) 211 | .mode(mode) 212 | .build(context); 213 | 214 | addView(chip); 215 | } 216 | 217 | public void setSelectedChip(int index) { 218 | Chip chip = (Chip) getChildAt(index); 219 | chip.select(); 220 | if (mode == Mode.REQUIRED) { 221 | for (int i = 0; i < getChildCount(); i++) { 222 | Chip chipp = (Chip) getChildAt(i); 223 | if (i == index) { 224 | chipp.setLocked(true); 225 | } else { 226 | chipp.setLocked(false); 227 | } 228 | } 229 | } 230 | } 231 | 232 | @Override 233 | public void chipSelected(int index) { 234 | 235 | switch (mode) { 236 | case SINGLE: 237 | case REQUIRED: 238 | for (int i = 0; i < getChildCount(); i++) { 239 | Chip chip = (Chip) getChildAt(i); 240 | if (i == index) { 241 | if (mode == Mode.REQUIRED) chip.setLocked(true); 242 | } else { 243 | chip.deselect(); 244 | chip.setLocked(false); 245 | } 246 | } 247 | break; 248 | default: 249 | // 250 | } 251 | 252 | if (chipListener != null) { 253 | chipListener.chipSelected(index); 254 | } 255 | } 256 | 257 | @Override 258 | public void chipDeselected(int index) { 259 | if (chipListener != null) { 260 | chipListener.chipDeselected(index); 261 | } 262 | } 263 | 264 | public boolean isSelected(int index) { 265 | if (index > 0 && index < getChildCount()) { 266 | Chip chip = (Chip) getChildAt(index); 267 | return chip.isSelected(); 268 | } 269 | return false; 270 | } 271 | 272 | //Apparently using the builder pattern to configure an object has been labelled a 'Bloch Builder'. 273 | public static class Configure { 274 | private ChipCloud chipCloud; 275 | private int selectedColor = -1; 276 | private int selectedFontColor = -1; 277 | private int deselectedColor = -1; 278 | private int deselectedFontColor = -1; 279 | private int selectTransitionMS = -1; 280 | private int deselectTransitionMS = -1; 281 | private Mode mode = null; 282 | private String[] labels = null; 283 | private ChipListener chipListener; 284 | private Gravity gravity = null; 285 | private Typeface typeface; 286 | private Boolean allCaps = null; 287 | private int textSize = -1; 288 | private int minHorizontalSpacing = -1; 289 | private int verticalSpacing = -1; 290 | 291 | public Configure chipCloud(ChipCloud chipCloud) { 292 | this.chipCloud = chipCloud; 293 | return this; 294 | } 295 | 296 | public Configure mode(Mode mode) { 297 | this.mode = mode; 298 | return this; 299 | } 300 | 301 | public Configure selectedColor(int selectedColor) { 302 | this.selectedColor = selectedColor; 303 | return this; 304 | } 305 | 306 | public Configure selectedFontColor(int selectedFontColor) { 307 | this.selectedFontColor = selectedFontColor; 308 | return this; 309 | } 310 | 311 | public Configure deselectedColor(int deselectedColor) { 312 | this.deselectedColor = deselectedColor; 313 | return this; 314 | } 315 | 316 | public Configure deselectedFontColor(int deselectedFontColor) { 317 | this.deselectedFontColor = deselectedFontColor; 318 | return this; 319 | } 320 | 321 | public Configure selectTransitionMS(int selectTransitionMS) { 322 | this.selectTransitionMS = selectTransitionMS; 323 | return this; 324 | } 325 | 326 | public Configure deselectTransitionMS(int deselectTransitionMS) { 327 | this.deselectTransitionMS = deselectTransitionMS; 328 | return this; 329 | } 330 | 331 | public Configure labels(String[] labels) { 332 | this.labels = labels; 333 | return this; 334 | } 335 | 336 | public Configure chipListener(ChipListener chipListener) { 337 | this.chipListener = chipListener; 338 | return this; 339 | } 340 | 341 | public Configure gravity(Gravity gravity) { 342 | this.gravity = gravity; 343 | return this; 344 | } 345 | 346 | public Configure typeface(Typeface typeface) { 347 | this.typeface = typeface; 348 | return this; 349 | } 350 | 351 | /** 352 | * @param textSize value in pixels as obtained from @{@link android.content.res.Resources#getDimensionPixelSize(int)} 353 | */ 354 | public Configure textSize(int textSize) { 355 | this.textSize = textSize; 356 | return this; 357 | } 358 | 359 | public Configure allCaps(boolean isAllCaps) { 360 | this.allCaps = isAllCaps; 361 | return this; 362 | } 363 | 364 | public Configure minHorizontalSpacing(int spacingInPx) { 365 | this.minHorizontalSpacing = spacingInPx; 366 | return this; 367 | } 368 | 369 | public Configure verticalSpacing(int spacingInPx) { 370 | this.verticalSpacing = spacingInPx; 371 | return this; 372 | } 373 | 374 | public void build() { 375 | chipCloud.removeAllViews(); 376 | if (mode != null) chipCloud.setMode(mode); 377 | if (gravity != null) chipCloud.setGravity(gravity); 378 | if (typeface != null) chipCloud.setTypeface(typeface); 379 | if (textSize != -1) chipCloud.setTextSize(textSize); 380 | if (allCaps != null) chipCloud.setAllCaps(allCaps); 381 | if (selectedColor != -1) chipCloud.setSelectedColor(selectedColor); 382 | if (selectedFontColor != -1) chipCloud.setSelectedFontColor(selectedFontColor); 383 | if (deselectedColor != -1) chipCloud.setUnselectedColor(deselectedColor); 384 | if (deselectedFontColor != -1) chipCloud.setUnselectedFontColor(deselectedFontColor); 385 | if (selectTransitionMS != -1) chipCloud.setSelectTransitionMS(selectTransitionMS); 386 | if (deselectTransitionMS != -1) chipCloud.setDeselectTransitionMS(deselectTransitionMS); 387 | if (minHorizontalSpacing != -1) chipCloud.setMinimumHorizontalSpacing(minHorizontalSpacing); 388 | if (verticalSpacing != -1) chipCloud.setVerticalSpacing(verticalSpacing); 389 | chipCloud.setChipListener(chipListener); 390 | chipCloud.addChips(labels); 391 | } 392 | 393 | public void update() { 394 | int childCount = chipCloud.getChildCount(); 395 | for (int i = 0; i < childCount; i++) { 396 | Chip chip = (Chip) chipCloud.getChildAt(i); 397 | chip.setText(labels[i]); 398 | chip.invalidate(); 399 | } 400 | chipCloud.invalidate(); 401 | chipCloud.requestLayout(); 402 | } 403 | } 404 | } 405 | -------------------------------------------------------------------------------- /library/src/main/java/com/adroitandroid/chipcloud/ChipListener.java: -------------------------------------------------------------------------------- 1 | package com.adroitandroid.chipcloud; 2 | 3 | public interface ChipListener { 4 | void chipSelected(int index); 5 | 6 | void chipDeselected(int index); 7 | } 8 | -------------------------------------------------------------------------------- /library/src/main/java/com/adroitandroid/chipcloud/FlowLayout.java: -------------------------------------------------------------------------------- 1 | package com.adroitandroid.chipcloud; 2 | 3 | import android.content.Context; 4 | import android.util.AttributeSet; 5 | import android.view.View; 6 | import android.view.ViewGroup; 7 | 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | 11 | /** 12 | * @author RAW 13 | */ 14 | public abstract class FlowLayout extends ViewGroup { 15 | 16 | private int lineHeight; 17 | private LayoutProcessor layoutProcessor = new LayoutProcessor(); 18 | 19 | public enum Gravity { 20 | LEFT, RIGHT, CENTER, STAGGERED 21 | } 22 | 23 | public FlowLayout(Context context) { 24 | super(context); 25 | } 26 | 27 | public FlowLayout(Context context, AttributeSet attrs) { 28 | super(context, attrs); 29 | } 30 | 31 | @Override 32 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 33 | assert (MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.UNSPECIFIED); 34 | 35 | final int width = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight(); 36 | int height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingTop() - getPaddingBottom(); 37 | final int count = getChildCount(); 38 | int lineHeight = 0; 39 | 40 | int xPos = getPaddingLeft(); 41 | int yPos = getPaddingTop(); 42 | 43 | int childHeightMeasureSpec; 44 | if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) { 45 | childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST); 46 | } else { 47 | childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); 48 | } 49 | 50 | for (int i = 0; i < count; i++) { 51 | final View child = getChildAt(i); 52 | if (child.getVisibility() != GONE) { 53 | child.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST), 54 | childHeightMeasureSpec); 55 | final int childW = child.getMeasuredWidth(); 56 | lineHeight = Math.max(lineHeight, child.getMeasuredHeight() + getVerticalSpacing()); 57 | 58 | if (xPos + childW > width) { 59 | xPos = getPaddingLeft(); 60 | yPos += lineHeight; 61 | } 62 | 63 | xPos += childW + getMinimumHorizontalSpacing(); 64 | } 65 | } 66 | this.lineHeight = lineHeight; 67 | 68 | if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.UNSPECIFIED || (MeasureSpec.getMode( 69 | heightMeasureSpec) == MeasureSpec.AT_MOST && yPos + lineHeight < height)) { 70 | height = yPos + lineHeight; 71 | } 72 | setMeasuredDimension(width, height); 73 | } 74 | 75 | @Override 76 | protected void onLayout(boolean changed, int l, int t, int r, int b) { 77 | final int count = getChildCount(); 78 | final int width = r - l; 79 | int xPos = getPaddingLeft(); 80 | int yPos = getPaddingTop(); 81 | layoutProcessor.setWidth(width); 82 | for (int i = 0; i < count; i++) { 83 | final View child = getChildAt(i); 84 | if (child.getVisibility() != GONE) { 85 | final int childW = child.getMeasuredWidth(); 86 | final int childH = child.getMeasuredHeight(); 87 | if (xPos + childW > width) { 88 | xPos = getPaddingLeft(); 89 | yPos += lineHeight; 90 | layoutProcessor.layoutPreviousRow(); 91 | } 92 | layoutProcessor.addViewForLayout(child, yPos, childW, childH); 93 | xPos += childW + getMinimumHorizontalSpacing(); 94 | } 95 | } 96 | layoutProcessor.layoutPreviousRow(); 97 | layoutProcessor.clear(); 98 | } 99 | 100 | protected abstract int getMinimumHorizontalSpacing(); 101 | 102 | protected abstract int getVerticalSpacing(); 103 | 104 | protected abstract Gravity getGravity(); 105 | 106 | private class LayoutProcessor { 107 | 108 | private int rowY; 109 | private final List viewsInCurrentRow; 110 | private final List viewWidths; 111 | private final List viewHeights; 112 | private int width; 113 | 114 | private LayoutProcessor() { 115 | viewsInCurrentRow = new ArrayList<>(); 116 | viewWidths = new ArrayList<>(); 117 | viewHeights = new ArrayList<>(); 118 | } 119 | 120 | void addViewForLayout(View view, int yPos, int childW, int childH) { 121 | rowY = yPos; 122 | viewsInCurrentRow.add(view); 123 | viewWidths.add(childW); 124 | viewHeights.add(childH); 125 | } 126 | 127 | void clear() { 128 | viewsInCurrentRow.clear(); 129 | viewWidths.clear(); 130 | viewHeights.clear(); 131 | } 132 | 133 | void layoutPreviousRow() { 134 | Gravity gravity = getGravity(); 135 | int minimumHorizontalSpacing = getMinimumHorizontalSpacing(); 136 | switch (gravity) { 137 | case LEFT: 138 | int xPos = getPaddingLeft(); 139 | for (int i = 0; i < viewsInCurrentRow.size(); i++) { 140 | viewsInCurrentRow.get(i).layout(xPos, rowY, xPos + viewWidths.get(i), rowY + viewHeights.get(i)); 141 | xPos += viewWidths.get(i) + minimumHorizontalSpacing; 142 | } 143 | break; 144 | case RIGHT: 145 | int xEnd = width - getPaddingRight(); 146 | for (int i = viewsInCurrentRow.size() - 1; i >= 0; i--) { 147 | int xStart = xEnd - viewWidths.get(i); 148 | viewsInCurrentRow.get(i).layout(xStart, rowY, xEnd, rowY + viewHeights.get(i)); 149 | xEnd = xStart - minimumHorizontalSpacing; 150 | } 151 | break; 152 | case STAGGERED: 153 | int totalWidthOfChildren = 0; 154 | for (int i = 0; i < viewWidths.size(); i++) { 155 | totalWidthOfChildren += viewWidths.get(i); 156 | } 157 | int horizontalSpacingForStaggered = (width - totalWidthOfChildren - getPaddingLeft() 158 | - getPaddingRight()) / (viewsInCurrentRow.size() + 1); 159 | xPos = getPaddingLeft() + horizontalSpacingForStaggered; 160 | for (int i = 0; i < viewsInCurrentRow.size(); i++) { 161 | viewsInCurrentRow.get(i).layout(xPos, rowY, xPos + viewWidths.get(i), rowY + viewHeights.get(i)); 162 | xPos += viewWidths.get(i) + horizontalSpacingForStaggered; 163 | } 164 | break; 165 | case CENTER: 166 | totalWidthOfChildren = 0; 167 | for (int i = 0; i < viewWidths.size(); i++) { 168 | totalWidthOfChildren += viewWidths.get(i); 169 | } 170 | xPos = getPaddingLeft() + (width - getPaddingLeft() - getPaddingRight() - 171 | totalWidthOfChildren - (minimumHorizontalSpacing * (viewsInCurrentRow.size() - 1))) / 2; 172 | for (int i = 0; i < viewsInCurrentRow.size(); i++) { 173 | viewsInCurrentRow.get(i).layout(xPos, rowY, xPos + viewWidths.get(i), rowY + viewHeights.get(i)); 174 | xPos += viewWidths.get(i) + minimumHorizontalSpacing; 175 | } 176 | break; 177 | } 178 | clear(); 179 | } 180 | 181 | void setWidth(int width) { 182 | this.width = width; 183 | } 184 | } 185 | } 186 | -------------------------------------------------------------------------------- /library/src/main/res/drawable/chip.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /library/src/main/res/drawable/chip_selected.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /library/src/main/res/layout/chip.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | -------------------------------------------------------------------------------- /library/src/main/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /library/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #333 4 | #333333 5 | #bbbbbb 6 | #ffffff 7 | 8 | #ffffaa 9 | #999933 10 | 11 | #aaffaa 12 | #339933 13 | 14 | -------------------------------------------------------------------------------- /library/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 13sp 4 | 32dp 5 | 8dp 6 | 8dp 7 | 8 | -------------------------------------------------------------------------------- /library/src/main/res/values/ids.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /library/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | ChipCloud 3 | 4 | One 5 | Two 6 | Three 7 | Four 8 | Five 9 | 10 | 11 | -------------------------------------------------------------------------------- /library/src/test/java/eu/fiskur/chipcloud/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package eu.fiskur.chipcloud; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * To work on unit tests, switch the Test Artifact in the Build Variants view. 9 | */ 10 | public class ExampleUnitTest { 11 | @Test 12 | public void additionIsCorrect() throws Exception { 13 | assertEquals(4, 2 + 2); 14 | } 15 | } -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':library' 2 | 3 | --------------------------------------------------------------------------------