diff --git a/build.gradle b/build.gradle index 87c964dc..538be696 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:1.2.0' + classpath 'com.android.tools.build:gradle:3.0.0' } } diff --git a/gradle.properties b/gradle.properties index 696ad761..1af6033c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -14,5 +14,5 @@ POM_DEVELOPER_ID=rengwuxian POM_DEVELOPER_NAME=Kai Zhu ANDROID_BUILD_TARGET_SDK_VERSION=22 -ANDROID_BUILD_TOOLS_VERSION=22.0.1 +ANDROID_BUILD_TOOLS_VERSION=26.0.2 ANDROID_BUILD_SDK_VERSION=22 \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index c8208cfb..03ccbcda 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip diff --git a/library/src/main/java/com/rengwuxian/materialedittext/MaterialEditText.java b/library/src/main/java/com/rengwuxian/materialedittext/MaterialEditText.java index 51d9d7c8..b41ec4a1 100755 --- a/library/src/main/java/com/rengwuxian/materialedittext/MaterialEditText.java +++ b/library/src/main/java/com/rengwuxian/materialedittext/MaterialEditText.java @@ -18,7 +18,6 @@ import android.support.annotation.IntDef; import android.support.annotation.NonNull; import android.support.annotation.Nullable; -import android.support.v7.widget.AppCompatEditText; import android.text.Editable; import android.text.Layout; import android.text.StaticLayout; @@ -57,6 +56,7 @@ public class MaterialEditText extends MaterialBaseEditText { public static final int FLOATING_LABEL_NONE = 0; public static final int FLOATING_LABEL_NORMAL = 1; public static final int FLOATING_LABEL_HIGHLIGHT = 2; + public static final int FLOATING_LABEL_BOTTOM = 3; final int DRAWABLE_LEFT = 0; final int DRAWABLE_TOP = 1; @@ -92,6 +92,11 @@ public class MaterialEditText extends MaterialBaseEditText { */ private int floatingLabelTextSize; + /** + * the floating label's text size. + */ + private int floatingLabelMode; + /** * the floating label's text color. */ @@ -387,7 +392,8 @@ private void init(Context context, AttributeSet attrs) { } primaryColor = typedArray.getColor(R.styleable.MaterialEditText_met_primaryColor, defaultPrimaryColor); - setFloatingLabelInternal(typedArray.getInt(R.styleable.MaterialEditText_met_floatingLabel, 0)); + floatingLabelMode = typedArray.getInt(R.styleable.MaterialEditText_met_floatingLabel, 0); + setFloatingLabelInternal(floatingLabelMode); errorColor = typedArray.getColor(R.styleable.MaterialEditText_met_errorColor, Color.parseColor("#e7492E")); disabledUnderlineColor = typedArray.getColor(R.styleable.MaterialEditText_met_disabledBottomLineColor, -1); minCharacters = typedArray.getInt(R.styleable.MaterialEditText_met_minCharacters, 0); @@ -828,11 +834,11 @@ private boolean adjustBottomLines() { } int destBottomLines; textPaint.setTextSize(bottomTextSize); - if (tempErrorText != null || helperText != null) { - Layout.Alignment alignment = (getGravity() & Gravity.RIGHT) == Gravity.RIGHT || isRTL() ? - Layout.Alignment.ALIGN_OPPOSITE : (getGravity() & Gravity.LEFT) == Gravity.LEFT ? + if (getBottomText() != null) { + Layout.Alignment alignment = (getGravity() & Gravity.END) == Gravity.END || isRTL() ? + Layout.Alignment.ALIGN_OPPOSITE : (getGravity() & Gravity.START) == Gravity.START ? Layout.Alignment.ALIGN_NORMAL : Layout.Alignment.ALIGN_CENTER; - textLayout = new StaticLayout(tempErrorText != null ? tempErrorText : helperText, textPaint, getWidth() - getBottomTextLeftOffset() - getBottomTextRightOffset() - getPaddingLeft() - getPaddingRight(), alignment, 1.0f, 0.0f, true); + textLayout = new StaticLayout(getBottomText(), textPaint, getWidth() - getBottomTextLeftOffset() - getBottomTextRightOffset() - getPaddingLeft() - getPaddingRight(), alignment, 1.0f, 0.0f, true); destBottomLines = Math.max(textLayout.getLineCount(), minBottomTextLines); } else { destBottomLines = minBottomLines; @@ -844,6 +850,19 @@ private boolean adjustBottomLines() { return true; } + private CharSequence getBottomText() { + if (tempErrorText != null) + return tempErrorText; + + if (floatingLabelMode == FLOATING_LABEL_BOTTOM && getHint() != null) + return getHint(); + + if (helperText != null) + return helperText; + + return null; + } + /** * get inner top padding, not the real paddingTop */ @@ -1355,8 +1374,8 @@ protected void onDraw(@NonNull Canvas canvas) { // draw the bottom text if (textLayout != null) { - if (tempErrorText != null || ((helperTextAlwaysShown || hasFocus()) && !TextUtils.isEmpty(helperText))) { // error text or helper text - textPaint.setColor(tempErrorText != null ? errorColor : helperTextColor != -1 ? helperTextColor : (baseColor & 0x00ffffff | 0x44000000)); + if (shouldDrawBottomText()) { // error text or helper text + textPaint.setColor(getBottomTextColor()); canvas.save(); if (isRTL()) { canvas.translate(endX - textLayout.getWidth(), lineStartY + bottomSpacing - bottomTextPadding); @@ -1417,6 +1436,39 @@ protected void onDraw(@NonNull Canvas canvas) { super.onDraw(canvas); } + private boolean shouldDrawBottomText() { + + if (tempErrorText != null) { + return true; + } + + if (((helperTextAlwaysShown || hasFocus()) && !TextUtils.isEmpty(helperText))) { + return true; + } + + if (floatingLabelMode == FLOATING_LABEL_BOTTOM && !TextUtils.isEmpty(getText())) { + return true; + } + + return false; + } + + private Integer getBottomTextColor() { + if (tempErrorText != null) { + return errorColor; + } + + if (floatingLabelMode == FLOATING_LABEL_BOTTOM) { + return floatingLabelTextColor; + } + + if (helperTextColor != -1) { + return helperTextColor; + } else { + return (baseColor & 0x00ffffff | 0x44000000); + } + } + private void showClearButton() { setCompoundDrawablesWithIntrinsicBounds(0, 0, clearButtonDrawable, 0); } diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml index 4e48ba19..ac380868 100644 --- a/library/src/main/res/values/attrs.xml +++ b/library/src/main/res/values/attrs.xml @@ -10,6 +10,7 @@ + diff --git a/sample/src/main/res/layout/activity_main.xml b/sample/src/main/res/layout/activity_main.xml index ade204ed..ef55f9d9 100644 --- a/sample/src/main/res/layout/activity_main.xml +++ b/sample/src/main/res/layout/activity_main.xml @@ -246,7 +246,9 @@ android:layout_height="wrap_content" android:layout_weight="1" android:hint="Helper/Error Text" - app:met_helperText="helper is here" /> + app:met_floatingLabel="bottom" + app:met_floatingLabelTextColor="@android:color/black" + android:gravity="center"/>