diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..afbdab33 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.gradle +/local.properties +/.idea/workspace.xml +/.idea/libraries +.DS_Store +/build diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 00000000..9ec1f297 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +MaterialEditText \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 00000000..217af471 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,23 @@ + + + + + + diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml new file mode 100644 index 00000000..e7bedf33 --- /dev/null +++ b/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 00000000..a56a2f89 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 00000000..049b8f70 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,20 @@ + + + + + + + diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 00000000..8303cbdc --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,11 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 00000000..3b312839 --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..dfb35fdd --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + Android API 19 Platform + + + + + + + + + diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..a31bd251 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/.idea/scopes/scope_settings.xml b/.idea/scopes/scope_settings.xml new file mode 100644 index 00000000..922003b8 --- /dev/null +++ b/.idea/scopes/scope_settings.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..def6a6a1 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/MaterialEditText.iml b/MaterialEditText.iml new file mode 100644 index 00000000..135c0f9e --- /dev/null +++ b/MaterialEditText.iml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + diff --git a/build.gradle b/build.gradle new file mode 100644 index 00000000..5bae7ca4 --- /dev/null +++ b/build.gradle @@ -0,0 +1,20 @@ +// Top-level build file where you can add configuration options common to all sub-projects/modules. +buildscript { + repositories { + jcenter() + } + dependencies { + classpath 'com.android.tools.build:gradle:0.13.2' + + // NOTE: Do not place your application dependencies here; they belong + // in the individual module build.gradle files + } +} +allprojects { + repositories { + jcenter() + } +} + +dependencies { +} \ No newline at end of file diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 00000000..1d3591c8 --- /dev/null +++ b/gradle.properties @@ -0,0 +1,18 @@ +# Project-wide Gradle settings. + +# IDE (e.g. Android Studio) users: +# Gradle settings configured through the IDE *will override* +# any settings specified in this file. + +# For more details on how to configure your build environment visit +# http://www.gradle.org/docs/current/userguide/build_environment.html + +# Specifies the JVM arguments used for the daemon process. +# The setting is particularly useful for tweaking memory settings. +# Default value: -Xmx10248m -XX:MaxPermSize=256m +# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 + +# When configured, Gradle will run in incubating parallel mode. +# This option should only be used with decoupled projects. More details, visit +# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects +# org.gradle.parallel=true \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000..8c0fb64a Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..ffaa281a --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Wed Apr 10 15:27:10 PDT 2013 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-2.1-all.zip diff --git a/gradlew b/gradlew new file mode 100644 index 00000000..91a7e269 --- /dev/null +++ b/gradlew @@ -0,0 +1,164 @@ +#!/usr/bin/env bash + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn ( ) { + echo "$*" +} + +die ( ) { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; +esac + +# For Cygwin, ensure paths are in UNIX format before anything is touched. +if $cygwin ; then + [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` +fi + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >&- +APP_HOME="`pwd -P`" +cd "$SAVED" >&- + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules +function splitJvmOpts() { + JVM_OPTS=("$@") +} +eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS +JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" + +exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 00000000..8a0b282a --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,90 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windowz variants + +if not "%OS%" == "Windows_NT" goto win9xME_args +if "%@eval[2+2]" == "4" goto 4NT_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* +goto execute + +:4NT_args +@rem Get arguments from the 4NT Shell from JP Software +set CMD_LINE_ARGS=%$ + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/library/.gitignore b/library/.gitignore new file mode 100644 index 00000000..796b96d1 --- /dev/null +++ b/library/.gitignore @@ -0,0 +1 @@ +/build diff --git a/library/build.gradle b/library/build.gradle new file mode 100644 index 00000000..8f7862ea --- /dev/null +++ b/library/build.gradle @@ -0,0 +1,25 @@ +apply plugin: 'com.android.library' + +android { + compileSdkVersion 21 + buildToolsVersion "21.0.2" + + defaultConfig { + applicationId "com.rengwuxian.materialedittext" + minSdkVersion 7 + targetSdkVersion 21 + versionCode 1 + versionName "1.0" + } + buildTypes { + release { + runProguard false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + compile 'com.android.support:support-annotations:21.0.0' + compile 'com.nineoldandroids:library:2.4.0' +} diff --git a/library/library.iml b/library/library.iml new file mode 100644 index 00000000..0f37f60f --- /dev/null +++ b/library/library.iml @@ -0,0 +1,89 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/library/proguard-rules.pro b/library/proguard-rules.pro new file mode 100644 index 00000000..b780fadc --- /dev/null +++ b/library/proguard-rules.pro @@ -0,0 +1,17 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in D:/Android/sdk/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} diff --git a/library/src/androidTest/java/com/rengwuxian/materialedittext/ApplicationTest.java b/library/src/androidTest/java/com/rengwuxian/materialedittext/ApplicationTest.java new file mode 100644 index 00000000..e09a1270 --- /dev/null +++ b/library/src/androidTest/java/com/rengwuxian/materialedittext/ApplicationTest.java @@ -0,0 +1,13 @@ +package com.rengwuxian.materialedittext; + +import android.app.Application; +import android.test.ApplicationTestCase; + +/** + * Testing Fundamentals + */ +public class ApplicationTest extends ApplicationTestCase { + public ApplicationTest() { + super(Application.class); + } +} \ No newline at end of file diff --git a/library/src/main/AndroidManifest.xml b/library/src/main/AndroidManifest.xml new file mode 100644 index 00000000..638cded6 --- /dev/null +++ b/library/src/main/AndroidManifest.xml @@ -0,0 +1,3 @@ + + diff --git a/library/src/main/java/com/rengwuxian/materialedittext/Density.java b/library/src/main/java/com/rengwuxian/materialedittext/Density.java new file mode 100644 index 00000000..9a1f6d39 --- /dev/null +++ b/library/src/main/java/com/rengwuxian/materialedittext/Density.java @@ -0,0 +1,16 @@ +package com.rengwuxian.materialedittext; + +import android.content.Context; +import android.content.res.Resources; +import android.util.TypedValue; + +/** + * Created by Zhukai on 2014/5/29 0029. + */ +class Density { + public static int dp2px(Context context, float dp) { + Resources r = context.getResources(); + float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()); + return (int) px; + } +} diff --git a/library/src/main/java/com/rengwuxian/materialedittext/MaterialEditText.java b/library/src/main/java/com/rengwuxian/materialedittext/MaterialEditText.java new file mode 100644 index 00000000..b90df3f9 --- /dev/null +++ b/library/src/main/java/com/rengwuxian/materialedittext/MaterialEditText.java @@ -0,0 +1,342 @@ +package com.rengwuxian.materialedittext; + +import android.content.Context; +import android.content.res.TypedArray; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.os.Build; +import android.support.annotation.NonNull; +import android.text.Editable; +import android.text.TextUtils; +import android.text.TextWatcher; +import android.util.AttributeSet; +import android.view.MotionEvent; +import android.view.View; +import android.widget.EditText; + +import com.nineoldandroids.animation.ArgbEvaluator; +import com.nineoldandroids.animation.ObjectAnimator; + +/** + * EditText in Material Design + *

+ * author:rengwuxian date:2014/8/29 0029 + *

+ */ +public class MaterialEditText extends EditText { + /** + * padding to the top of the top text (main text or floating label). + */ + private final int innerPaddingTop; + + /** + * padding to the bottom of the main text. + */ + private final int innerPaddingBottom; + + /** + * padding to the left of the main text. + */ + private final int innerPaddingLeft; + + /** + * padding to the right of the main text + */ + private final int innerPaddingRight; + + /** + * the floating label's text size. + */ + private final int floatingLabelTextSize; + + /** + * the spacing between the main text and the inner components (floating label, bottom ellipsis, characters counter). + */ + private final int innerComponentsSpacing; + + /** + * whether the floating label should be shown. default is false. + */ + private final boolean floatingLabelEnabled; + + /** + * whether to highlight the floating label's text color when focused (with the main color). default is true. + */ + private final boolean highlightFloatingLabel; + + /** + * whether in a dark theme. Used to decide the default textColor, hintTextColor and mainColor. default is false. + */ + private final boolean darkTheme; + + /** + * the underline's highlight color, and the highlight color of the floating label if app:highlightFloatingLabel is set true in the xml. default is black(when app:darkTheme is false) or white(when app:darkTheme is true) + */ + private final int mainColor; + + /** + * the color for when something is wrong.(e.g. exceeding max characters) + */ + private final int errorColor; + + /** + * characters count limit. 0 means no limit. default is 0. NOTE: the character counter will increase the View's height, unless in the full width single line mode(using android:singleLine= true). + */ + // todo full with 是否要实现? 上面的注释最终怎么写? + private final int maxCharacters; + + /** + * whether to show the bottom ellipsis in singleLine mode. default is false. NOTE: the bottom ellipsis will increase the View's height. + */ + private final boolean bottomEllipsis; + + /** + * bottom ellipsis's height + */ + private final int bottomEllipsisHeight; + + /** + * animation fraction of the floating label (0 as totally hidden). + */ + private float floatingLabelFraction; + + /** + * whether the floating label is being shown. + */ + private boolean floatingLabelShown; + + /** + * the floating label's focusFraction + */ + private float focusFraction; + + private ArgbEvaluator focusEvaluator = new ArgbEvaluator(); + Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); + ObjectAnimator showLabelAnimator; + ObjectAnimator labelFocusAnimator; + OnFocusChangeListener interFocusChangeListener; + OnFocusChangeListener outerFocusChangeListener; + + public MaterialEditText(Context context) { + this(context, null); + } + + public MaterialEditText(Context context, AttributeSet attrs) { + this(context, attrs, 0); + } + + public MaterialEditText(Context context, AttributeSet attrs, int style) { + super(context, attrs, style); + + setFocusable(true); + setFocusableInTouchMode(true); + setClickable(true); + + floatingLabelTextSize = getResources().getDimensionPixelSize(R.dimen.floating_label_size); + innerComponentsSpacing = getResources().getDimensionPixelSize(R.dimen.inner_components_spacing); + innerPaddingTop = getResources().getDimensionPixelSize(R.dimen.inner_padding_top); + innerPaddingBottom = getResources().getDimensionPixelSize(R.dimen.inner_padding_bottom); + innerPaddingLeft = getResources().getDimensionPixelSize(R.dimen.inner_padding_left); + innerPaddingRight = getResources().getDimensionPixelSize(R.dimen.inner_padding_right); + bottomEllipsisHeight = getResources().getDimensionPixelSize(R.dimen.bottom_ellipsis_height); + + TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MaterialEditText); + darkTheme = typedArray.getBoolean(R.styleable.MaterialEditText_darkTheme, false); + setTextColor(getResources().getColor(darkTheme ? R.color.white_text_primary : R.color.black_text_primary)); + setHintTextColor(getResources().getColor(darkTheme ? R.color.white_text_hint : R.color.black_text_hint)); + mainColor = typedArray.getColor(R.styleable.MaterialEditText_mainColor, darkTheme ? Color.WHITE : Color.BLACK); + floatingLabelEnabled = typedArray.getBoolean(R.styleable.MaterialEditText_floatLabel, false); + highlightFloatingLabel = typedArray.getBoolean(R.styleable.MaterialEditText_highlightLabel, true); + errorColor = typedArray.getColor(R.styleable.MaterialEditText_errorColor, getResources().getColor(R.color.red_error)); + maxCharacters = typedArray.getInt(R.styleable.MaterialEditText_maxCharacters, 0); + bottomEllipsis = typedArray.getBoolean(R.styleable.MaterialEditText_singleLineEllipsis, false); + typedArray.recycle(); + + initPadding(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { + setBackground(null); + } else { + setBackgroundDrawable(null); + } + initFloatingLabel(); + } + + private float getFloatingLabelFraction() { + return floatingLabelFraction; + } + + private void setFloatingLabelFraction(float floatingLabelFraction) { + this.floatingLabelFraction = floatingLabelFraction; + invalidate(); + } + + private float getFocusFraction() { + return focusFraction; + } + + private void setFocusFraction(float focusFraction) { + this.focusFraction = focusFraction; + invalidate(); + } + + private int getPixel(int dp) { + return Density.dp2px(getContext(), dp); + } + + private void initPadding() { + int extraPaddingTop = floatingLabelTextSize + innerComponentsSpacing; + int extraPaddingBottom = maxCharacters > 0 ? floatingLabelTextSize + innerComponentsSpacing : bottomEllipsis ? innerComponentsSpacing + bottomEllipsisHeight : 0; + if (floatingLabelEnabled) { + setPadding(innerPaddingLeft, extraPaddingTop + innerPaddingTop, innerPaddingRight, innerPaddingBottom + extraPaddingBottom); + } else { + setPadding(innerPaddingLeft, innerPaddingTop, innerPaddingRight, innerPaddingBottom + extraPaddingBottom); + } + } + + private void initFloatingLabel() { + if (floatingLabelEnabled) { + // observe the text changing + addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + } + + @Override + public void afterTextChanged(Editable s) { + if (s.length() == 0) { + if (floatingLabelShown) { + floatingLabelShown = false; + getShowLabelAnimator().reverse(); + } + } else if (!floatingLabelShown) { + floatingLabelShown = true; + if (getShowLabelAnimator().isStarted()) { + getShowLabelAnimator().reverse(); + } else { + getShowLabelAnimator().start(); + } + } + } + }); + if (highlightFloatingLabel) { + // observe the focus state to animate the floating label's text color appropriately + interFocusChangeListener = new OnFocusChangeListener() { + @Override + public void onFocusChange(View v, boolean hasFocus) { + if (hasFocus) { + if (getLabelFocusAnimator().isStarted()) + getLabelFocusAnimator().reverse(); + else { + getLabelFocusAnimator().start(); + } + } else { + getLabelFocusAnimator().reverse(); + } + if (outerFocusChangeListener != null) { + outerFocusChangeListener.onFocusChange(v, hasFocus); + } + } + }; + super.setOnFocusChangeListener(interFocusChangeListener); + } + } + } + + @Override + public void setOnFocusChangeListener(OnFocusChangeListener listener) { + if (interFocusChangeListener == null) { + super.setOnFocusChangeListener(listener); + } else { + outerFocusChangeListener = listener; + } + } + + private ObjectAnimator getShowLabelAnimator() { + if (showLabelAnimator == null) { + showLabelAnimator = ObjectAnimator.ofFloat(this, "floatingLabelFraction", 0f, 1f); + } + return showLabelAnimator; + } + + private ObjectAnimator getLabelFocusAnimator() { + if (labelFocusAnimator == null) { + labelFocusAnimator = ObjectAnimator.ofFloat(this, "focusFraction", 0f, 1f); + } + return labelFocusAnimator; + } + + @Override + protected void onDraw(@NonNull Canvas canvas) { + // set the textSize + paint.setTextSize(floatingLabelTextSize); + + // draw the background + float lineStartY = getHeight() - getPaddingBottom() + getPixel(8); + if (hasFocus()) { + boolean exceedingMaxCharacters = false; + if (maxCharacters > 0 && getText() != null && maxCharacters < getText().length()) { + exceedingMaxCharacters = true; + paint.setColor(errorColor); + } else { + paint.setColor(mainColor); + } + canvas.drawRect(getScrollX(), lineStartY, getWidth() + getScrollX(), lineStartY + getPixel(2), paint); + + // draw the characters counter + if (maxCharacters > 0) { + if (!exceedingMaxCharacters) { + paint.setColor(getCurrentHintTextColor()); + } + String text = getText().length() + " / " + maxCharacters; + canvas.drawText(text, getWidth() + getScrollX() - paint.measureText(text), lineStartY + innerComponentsSpacing + floatingLabelTextSize, paint); + } + } else { + paint.setColor(darkTheme ? Color.WHITE : Color.BLACK); + canvas.drawRect(getScrollX(), lineStartY, getWidth() + getScrollX(), lineStartY + getPixel(1), paint); + } + + // draw the floating label + if (floatingLabelEnabled && !TextUtils.isEmpty(getHint())) { + // calculate the text color + paint.setColor((Integer) focusEvaluator.evaluate(focusFraction, getHintTextColors().getColorForState(EMPTY_STATE_SET, darkTheme ? Color.WHITE : Color.BLACK), mainColor)); + + // calculate the vertical position + int start = innerPaddingTop + floatingLabelTextSize + innerComponentsSpacing; + int distance = innerComponentsSpacing + getPixel(2); + int position = (int) (start - distance * floatingLabelFraction); + + // calculate the alpha + int alpha = (int) (floatingLabelFraction * 0xff * (0.74f * focusFraction + 0.26f)); + paint.setAlpha(alpha); + + // draw the floating label + canvas.drawText(getHint().toString(), innerPaddingLeft + getScrollX(), position, paint); + } + + // draw the bottom ellipsis + if (hasFocus() && bottomEllipsis && getScrollX() != 0) { + paint.setColor(mainColor); + float startY = lineStartY + innerComponentsSpacing; + canvas.drawCircle(getPixel(2) + getScrollX(), startY + getPixel(2), getPixel(2), paint); + canvas.drawCircle(getPixel(2 + 8) + getScrollX(), startY + getPixel(2), getPixel(2), paint); + canvas.drawCircle(getPixel(2 + 16) + getScrollX(), startY + getPixel(2), getPixel(2), paint); + } + + // draw the original things + super.onDraw(canvas); + } + + @Override + public boolean onTouchEvent(MotionEvent event) { + if (bottomEllipsis && getScrollX() > 0 && event.getAction() == MotionEvent.ACTION_DOWN && event.getX() < getPixel(4 * 5) && event.getY() > getHeight() - innerPaddingBottom * 2 + getPixel(4)) { + setSelection(0); + return false; + } + return super.onTouchEvent(event); + } +} \ No newline at end of file diff --git a/library/src/main/java/com/rengwuxian/materialedittext/util/NinePatchBitmapFactory.java b/library/src/main/java/com/rengwuxian/materialedittext/util/NinePatchBitmapFactory.java new file mode 100644 index 00000000..fc3403f1 --- /dev/null +++ b/library/src/main/java/com/rengwuxian/materialedittext/util/NinePatchBitmapFactory.java @@ -0,0 +1,49 @@ +package com.rengwuxian.materialedittext.util; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; + +/** + * thanks to briangriffey https://gist.github.com/briangriffey/4391807 + */ +public class NinePatchBitmapFactory { + + // The 9 patch segment is not a solid color. + private static final int NO_COLOR = 0x00000001; + + public static ByteBuffer getByteBuffer(int left, int top, int right, int bottom) { + //Docs check the NinePatchChunkFile + ByteBuffer buffer = ByteBuffer.allocate(56).order(ByteOrder.nativeOrder()); + //was translated + buffer.put((byte) 0x01); + //divx size + buffer.put((byte) 0x02); + //divy size + buffer.put((byte) 0x02); + //color size + buffer.put((byte) 0x02); + + //skip + buffer.putInt(0); + buffer.putInt(0); + + //padding + buffer.putInt(0); + buffer.putInt(0); + buffer.putInt(0); + buffer.putInt(0); + + //skip 4 bytes + buffer.putInt(0); + + buffer.putInt(left); + buffer.putInt(right); + buffer.putInt(top); + buffer.putInt(bottom); + buffer.putInt(NO_COLOR); + buffer.putInt(NO_COLOR); + + return buffer; + } + +} \ No newline at end of file diff --git a/library/src/main/res/drawable-hdpi/ic_launcher.png b/library/src/main/res/drawable-hdpi/ic_launcher.png new file mode 100644 index 00000000..96a442e5 Binary files /dev/null and b/library/src/main/res/drawable-hdpi/ic_launcher.png differ diff --git a/library/src/main/res/drawable-mdpi/ic_launcher.png b/library/src/main/res/drawable-mdpi/ic_launcher.png new file mode 100644 index 00000000..359047df Binary files /dev/null and b/library/src/main/res/drawable-mdpi/ic_launcher.png differ diff --git a/library/src/main/res/drawable-xhdpi/ic_launcher.png b/library/src/main/res/drawable-xhdpi/ic_launcher.png new file mode 100644 index 00000000..71c6d760 Binary files /dev/null and b/library/src/main/res/drawable-xhdpi/ic_launcher.png differ diff --git a/library/src/main/res/drawable-xxhdpi/edittext_bg_activated.9.png b/library/src/main/res/drawable-xxhdpi/edittext_bg_activated.9.png new file mode 100644 index 00000000..5340950d Binary files /dev/null and b/library/src/main/res/drawable-xxhdpi/edittext_bg_activated.9.png differ diff --git a/library/src/main/res/drawable-xxhdpi/edittext_bg_ellipsis_activated.9.png b/library/src/main/res/drawable-xxhdpi/edittext_bg_ellipsis_activated.9.png new file mode 100644 index 00000000..da05007f Binary files /dev/null and b/library/src/main/res/drawable-xxhdpi/edittext_bg_ellipsis_activated.9.png differ diff --git a/library/src/main/res/drawable-xxhdpi/edittext_bg_ellipsis_normal.9.png b/library/src/main/res/drawable-xxhdpi/edittext_bg_ellipsis_normal.9.png new file mode 100644 index 00000000..0ef8d717 Binary files /dev/null and b/library/src/main/res/drawable-xxhdpi/edittext_bg_ellipsis_normal.9.png differ diff --git a/library/src/main/res/drawable-xxhdpi/edittext_bg_max_characters_activated.9.png b/library/src/main/res/drawable-xxhdpi/edittext_bg_max_characters_activated.9.png new file mode 100644 index 00000000..92a16072 Binary files /dev/null and b/library/src/main/res/drawable-xxhdpi/edittext_bg_max_characters_activated.9.png differ diff --git a/library/src/main/res/drawable-xxhdpi/edittext_bg_max_characters_normal.9.png b/library/src/main/res/drawable-xxhdpi/edittext_bg_max_characters_normal.9.png new file mode 100644 index 00000000..ff0df9e0 Binary files /dev/null and b/library/src/main/res/drawable-xxhdpi/edittext_bg_max_characters_normal.9.png differ diff --git a/library/src/main/res/drawable-xxhdpi/edittext_bg_normal.9.png b/library/src/main/res/drawable-xxhdpi/edittext_bg_normal.9.png new file mode 100644 index 00000000..1e8d808f Binary files /dev/null and b/library/src/main/res/drawable-xxhdpi/edittext_bg_normal.9.png differ diff --git a/library/src/main/res/drawable-xxhdpi/ic_launcher.png b/library/src/main/res/drawable-xxhdpi/ic_launcher.png new file mode 100644 index 00000000..4df18946 Binary files /dev/null and b/library/src/main/res/drawable-xxhdpi/ic_launcher.png differ diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml new file mode 100644 index 00000000..8d14d3a7 --- /dev/null +++ b/library/src/main/res/values/attrs.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/library/src/main/res/values/colors.xml b/library/src/main/res/values/colors.xml new file mode 100644 index 00000000..dcda04ff --- /dev/null +++ b/library/src/main/res/values/colors.xml @@ -0,0 +1,15 @@ + + + #009688 + #00796b + #df000000 + #8a000000 + #44000000 + #dfffffff + #8affffff + #44ffffff + #1e000000 + #99ffffff + #FF4C00 + #E7796E + \ No newline at end of file diff --git a/library/src/main/res/values/dimens.xml b/library/src/main/res/values/dimens.xml new file mode 100644 index 00000000..4850aed2 --- /dev/null +++ b/library/src/main/res/values/dimens.xml @@ -0,0 +1,11 @@ + + + 16dp + 16dp + 0dp + 0dp + 12dp + 16dp + 8dp + 4dp + \ No newline at end of file diff --git a/sample/.gitignore b/sample/.gitignore new file mode 100644 index 00000000..796b96d1 --- /dev/null +++ b/sample/.gitignore @@ -0,0 +1 @@ +/build diff --git a/sample/build.gradle b/sample/build.gradle new file mode 100644 index 00000000..5b638aed --- /dev/null +++ b/sample/build.gradle @@ -0,0 +1,26 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 21 + buildToolsVersion "21.0.2" + + defaultConfig { + applicationId "com.rengwuxian.materialedittext.sample" + minSdkVersion 7 + targetSdkVersion 21 + versionCode 1 + versionName "1.0" + } + buildTypes { + release { + runProguard false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + compile fileTree(dir: 'libs', include: ['*.jar']) + compile 'com.android.support:appcompat-v7:21.0.0' + compile project(':library') +} diff --git a/sample/proguard-rules.pro b/sample/proguard-rules.pro new file mode 100644 index 00000000..41afecb1 --- /dev/null +++ b/sample/proguard-rules.pro @@ -0,0 +1,17 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in D:\Android\sdk/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} diff --git a/sample/sample.iml b/sample/sample.iml new file mode 100644 index 00000000..b0424afd --- /dev/null +++ b/sample/sample.iml @@ -0,0 +1,91 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sample/src/androidTest/java/com/rengwuxian/materialedittext/sample/ApplicationTest.java b/sample/src/androidTest/java/com/rengwuxian/materialedittext/sample/ApplicationTest.java new file mode 100644 index 00000000..fce18681 --- /dev/null +++ b/sample/src/androidTest/java/com/rengwuxian/materialedittext/sample/ApplicationTest.java @@ -0,0 +1,13 @@ +package com.rengwuxian.materialedittext.sample; + +import android.app.Application; +import android.test.ApplicationTestCase; + +/** + * Testing Fundamentals + */ +public class ApplicationTest extends ApplicationTestCase { + public ApplicationTest() { + super(Application.class); + } +} \ No newline at end of file diff --git a/sample/src/main/AndroidManifest.xml b/sample/src/main/AndroidManifest.xml new file mode 100644 index 00000000..6e864c14 --- /dev/null +++ b/sample/src/main/AndroidManifest.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/sample/src/main/java/com/rengwuxian/materialedittext/sample/MainActivity.java b/sample/src/main/java/com/rengwuxian/materialedittext/sample/MainActivity.java new file mode 100644 index 00000000..9aa09d00 --- /dev/null +++ b/sample/src/main/java/com/rengwuxian/materialedittext/sample/MainActivity.java @@ -0,0 +1,40 @@ +package com.rengwuxian.materialedittext.sample; + +import android.os.Bundle; +import android.support.v7.app.ActionBarActivity; +import android.view.Menu; +import android.view.MenuItem; + + +public class MainActivity extends ActionBarActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + } + + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + // Inflate the menu; this adds items to the action bar if it is present. + getMenuInflater().inflate(R.menu.menu_main, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + // Handle action bar item clicks here. The action bar will + // automatically handle clicks on the Home/Up button, so long + // as you specify a parent activity in AndroidManifest.xml. + int id = item.getItemId(); + + //noinspection SimplifiableIfStatement + if (id == R.id.action_settings) { + return true; + } + + return super.onOptionsItemSelected(item); + } +} diff --git a/sample/src/main/res/drawable-hdpi/ic_launcher.png b/sample/src/main/res/drawable-hdpi/ic_launcher.png new file mode 100644 index 00000000..96a442e5 Binary files /dev/null and b/sample/src/main/res/drawable-hdpi/ic_launcher.png differ diff --git a/sample/src/main/res/drawable-mdpi/ic_launcher.png b/sample/src/main/res/drawable-mdpi/ic_launcher.png new file mode 100644 index 00000000..359047df Binary files /dev/null and b/sample/src/main/res/drawable-mdpi/ic_launcher.png differ diff --git a/sample/src/main/res/drawable-xhdpi/ic_launcher.png b/sample/src/main/res/drawable-xhdpi/ic_launcher.png new file mode 100644 index 00000000..71c6d760 Binary files /dev/null and b/sample/src/main/res/drawable-xhdpi/ic_launcher.png differ diff --git a/sample/src/main/res/drawable-xxhdpi/ic_launcher.png b/sample/src/main/res/drawable-xxhdpi/ic_launcher.png new file mode 100644 index 00000000..4df18946 Binary files /dev/null and b/sample/src/main/res/drawable-xxhdpi/ic_launcher.png differ diff --git a/sample/src/main/res/layout/activity_main.xml b/sample/src/main/res/layout/activity_main.xml new file mode 100644 index 00000000..f597d073 --- /dev/null +++ b/sample/src/main/res/layout/activity_main.xml @@ -0,0 +1,62 @@ + + + + + + + + + + + + + + diff --git a/sample/src/main/res/menu/menu_main.xml b/sample/src/main/res/menu/menu_main.xml new file mode 100644 index 00000000..db961b95 --- /dev/null +++ b/sample/src/main/res/menu/menu_main.xml @@ -0,0 +1,8 @@ +

+ + diff --git a/sample/src/main/res/values-v21/styles.xml b/sample/src/main/res/values-v21/styles.xml new file mode 100644 index 00000000..f45c334d --- /dev/null +++ b/sample/src/main/res/values-v21/styles.xml @@ -0,0 +1,8 @@ + + + + diff --git a/sample/src/main/res/values-w820dp/dimens.xml b/sample/src/main/res/values-w820dp/dimens.xml new file mode 100644 index 00000000..63fc8164 --- /dev/null +++ b/sample/src/main/res/values-w820dp/dimens.xml @@ -0,0 +1,6 @@ + + + 64dp + diff --git a/sample/src/main/res/values/colors.xml b/sample/src/main/res/values/colors.xml new file mode 100644 index 00000000..b156404c --- /dev/null +++ b/sample/src/main/res/values/colors.xml @@ -0,0 +1,6 @@ + + + #3f51b5 + #303f9f + #00bcd4 + \ No newline at end of file diff --git a/sample/src/main/res/values/dimens.xml b/sample/src/main/res/values/dimens.xml new file mode 100644 index 00000000..acf94cc6 --- /dev/null +++ b/sample/src/main/res/values/dimens.xml @@ -0,0 +1,5 @@ + + + 16dp + 16dp + diff --git a/sample/src/main/res/values/strings.xml b/sample/src/main/res/values/strings.xml new file mode 100644 index 00000000..b3b67ae1 --- /dev/null +++ b/sample/src/main/res/values/strings.xml @@ -0,0 +1,12 @@ + + + + MaterialCompat + Hello world! + Settings + New Account + Description + Title + Material EditText + + diff --git a/sample/src/main/res/values/styles.xml b/sample/src/main/res/values/styles.xml new file mode 100644 index 00000000..41d3d766 --- /dev/null +++ b/sample/src/main/res/values/styles.xml @@ -0,0 +1,12 @@ + + + + + + diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 00000000..52baf7e6 --- /dev/null +++ b/settings.gradle @@ -0,0 +1 @@ +include ':sample', ':library'