Skip to content

Commit 2c4e397

Browse files
author
Cleber Henriques
committed
2 parents ecaae81 + 5a05369 commit 2c4e397

File tree

6 files changed

+69
-14
lines changed

6 files changed

+69
-14
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ buildscript {
44
jcenter()
55
}
66
dependencies {
7-
classpath 'com.android.tools.build:gradle:1.2.0'
7+
classpath 'com.android.tools.build:gradle:3.0.0'
88
}
99
}
1010

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ POM_DEVELOPER_ID=rengwuxian
1414
POM_DEVELOPER_NAME=Kai Zhu
1515

1616
ANDROID_BUILD_TARGET_SDK_VERSION=22
17-
ANDROID_BUILD_TOOLS_VERSION=22.0.1
17+
ANDROID_BUILD_TOOLS_VERSION=26.0.2
1818
ANDROID_BUILD_SDK_VERSION=22

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip

library/src/main/java/com/rengwuxian/materialedittext/MaterialEditText.java

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import android.support.annotation.IntDef;
1919
import android.support.annotation.NonNull;
2020
import android.support.annotation.Nullable;
21-
import android.support.v7.widget.AppCompatEditText;
2221
import android.text.Editable;
2322
import android.text.Layout;
2423
import android.text.StaticLayout;
@@ -57,6 +56,7 @@ public class MaterialEditText extends MaterialBaseEditText {
5756
public static final int FLOATING_LABEL_NONE = 0;
5857
public static final int FLOATING_LABEL_NORMAL = 1;
5958
public static final int FLOATING_LABEL_HIGHLIGHT = 2;
59+
public static final int FLOATING_LABEL_BOTTOM = 3;
6060

6161
final int DRAWABLE_LEFT = 0;
6262
final int DRAWABLE_TOP = 1;
@@ -92,6 +92,11 @@ public class MaterialEditText extends MaterialBaseEditText {
9292
*/
9393
private int floatingLabelTextSize;
9494

95+
/**
96+
* the floating label's text size.
97+
*/
98+
private int floatingLabelMode;
99+
95100
/**
96101
* the floating label's text color.
97102
*/
@@ -387,7 +392,8 @@ private void init(Context context, AttributeSet attrs) {
387392
}
388393

389394
primaryColor = typedArray.getColor(R.styleable.MaterialEditText_met_primaryColor, defaultPrimaryColor);
390-
setFloatingLabelInternal(typedArray.getInt(R.styleable.MaterialEditText_met_floatingLabel, 0));
395+
floatingLabelMode = typedArray.getInt(R.styleable.MaterialEditText_met_floatingLabel, 0);
396+
setFloatingLabelInternal(floatingLabelMode);
391397
errorColor = typedArray.getColor(R.styleable.MaterialEditText_met_errorColor, Color.parseColor("#e7492E"));
392398
disabledUnderlineColor = typedArray.getColor(R.styleable.MaterialEditText_met_disabledBottomLineColor, -1);
393399
minCharacters = typedArray.getInt(R.styleable.MaterialEditText_met_minCharacters, 0);
@@ -828,11 +834,11 @@ private boolean adjustBottomLines() {
828834
}
829835
int destBottomLines;
830836
textPaint.setTextSize(bottomTextSize);
831-
if (tempErrorText != null || helperText != null) {
832-
Layout.Alignment alignment = (getGravity() & Gravity.RIGHT) == Gravity.RIGHT || isRTL() ?
833-
Layout.Alignment.ALIGN_OPPOSITE : (getGravity() & Gravity.LEFT) == Gravity.LEFT ?
837+
if (getBottomText() != null) {
838+
Layout.Alignment alignment = (getGravity() & Gravity.END) == Gravity.END || isRTL() ?
839+
Layout.Alignment.ALIGN_OPPOSITE : (getGravity() & Gravity.START) == Gravity.START ?
834840
Layout.Alignment.ALIGN_NORMAL : Layout.Alignment.ALIGN_CENTER;
835-
textLayout = new StaticLayout(tempErrorText != null ? tempErrorText : helperText, textPaint, getWidth() - getBottomTextLeftOffset() - getBottomTextRightOffset() - getPaddingLeft() - getPaddingRight(), alignment, 1.0f, 0.0f, true);
841+
textLayout = new StaticLayout(getBottomText(), textPaint, getWidth() - getBottomTextLeftOffset() - getBottomTextRightOffset() - getPaddingLeft() - getPaddingRight(), alignment, 1.0f, 0.0f, true);
836842
destBottomLines = Math.max(textLayout.getLineCount(), minBottomTextLines);
837843
} else {
838844
destBottomLines = minBottomLines;
@@ -844,6 +850,19 @@ private boolean adjustBottomLines() {
844850
return true;
845851
}
846852

853+
private CharSequence getBottomText() {
854+
if (tempErrorText != null)
855+
return tempErrorText;
856+
857+
if (floatingLabelMode == FLOATING_LABEL_BOTTOM && getHint() != null)
858+
return getHint();
859+
860+
if (helperText != null)
861+
return helperText;
862+
863+
return null;
864+
}
865+
847866
/**
848867
* get inner top padding, not the real paddingTop
849868
*/
@@ -1355,8 +1374,8 @@ protected void onDraw(@NonNull Canvas canvas) {
13551374

13561375
// draw the bottom text
13571376
if (textLayout != null) {
1358-
if (tempErrorText != null || ((helperTextAlwaysShown || hasFocus()) && !TextUtils.isEmpty(helperText))) { // error text or helper text
1359-
textPaint.setColor(tempErrorText != null ? errorColor : helperTextColor != -1 ? helperTextColor : (baseColor & 0x00ffffff | 0x44000000));
1377+
if (shouldDrawBottomText()) { // error text or helper text
1378+
textPaint.setColor(getBottomTextColor());
13601379
canvas.save();
13611380
if (isRTL()) {
13621381
canvas.translate(endX - textLayout.getWidth(), lineStartY + bottomSpacing - bottomTextPadding);
@@ -1417,6 +1436,39 @@ protected void onDraw(@NonNull Canvas canvas) {
14171436
super.onDraw(canvas);
14181437
}
14191438

1439+
private boolean shouldDrawBottomText() {
1440+
1441+
if (tempErrorText != null) {
1442+
return true;
1443+
}
1444+
1445+
if (((helperTextAlwaysShown || hasFocus()) && !TextUtils.isEmpty(helperText))) {
1446+
return true;
1447+
}
1448+
1449+
if (floatingLabelMode == FLOATING_LABEL_BOTTOM && !TextUtils.isEmpty(getText())) {
1450+
return true;
1451+
}
1452+
1453+
return false;
1454+
}
1455+
1456+
private Integer getBottomTextColor() {
1457+
if (tempErrorText != null) {
1458+
return errorColor;
1459+
}
1460+
1461+
if (floatingLabelMode == FLOATING_LABEL_BOTTOM) {
1462+
return floatingLabelTextColor;
1463+
}
1464+
1465+
if (helperTextColor != -1) {
1466+
return helperTextColor;
1467+
} else {
1468+
return (baseColor & 0x00ffffff | 0x44000000);
1469+
}
1470+
}
1471+
14201472
private void showClearButton() {
14211473
setCompoundDrawablesWithIntrinsicBounds(0, 0, clearButtonDrawable, 0);
14221474
}

library/src/main/res/values/attrs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<enum name="none" value="0" />
1111
<enum name="normal" value="1" />
1212
<enum name="highlight" value="2" />
13+
<enum name="bottom" value="3" />
1314
</attr>
1415
<!-- The color for when something is wrong.(e.g. exceeding max characters) -->
1516
<attr name="met_errorColor" format="color" />

sample/src/main/res/layout/activity_main.xml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,9 @@
246246
android:layout_height="wrap_content"
247247
android:layout_weight="1"
248248
android:hint="Helper/Error Text"
249-
app:met_helperText="helper is here" />
249+
app:met_floatingLabel="bottom"
250+
app:met_floatingLabelTextColor="@android:color/black"
251+
android:gravity="center"/>
250252

251253
<Button
252254
android:id="@+id/setErrorBt"
@@ -329,9 +331,8 @@
329331
android:hint="Custom Colors"
330332
app:met_baseColor="#007688"
331333
app:met_errorColor="#ddaa00"
332-
app:met_floatingLabel="normal"
334+
app:met_floatingLabel="bottom"
333335
app:met_floatingLabelTextColor="#8805ad"
334-
app:met_helperText="Helper Text"
335336
app:met_helperTextColor="#795548"
336337
app:met_maxCharacters="5"
337338
app:met_primaryColor="#2196F3"
@@ -361,6 +362,7 @@
361362
app:met_accentTypeface="fonts/Roboto-LightItalic.ttf"
362363
app:met_floatingLabel="normal"
363364
app:met_helperText="Helper Text"
365+
android:layout_gravity="center_horizontal"
364366
app:met_maxCharacters="5"
365367
app:met_typeface="fonts/Roboto-LightItalic.ttf" />
366368

0 commit comments

Comments
 (0)