Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Cleber Henriques committed Nov 8, 2017
2 parents ecaae81 + 5a05369 commit 2c4e397
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 14 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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
*/
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<enum name="none" value="0" />
<enum name="normal" value="1" />
<enum name="highlight" value="2" />
<enum name="bottom" value="3" />
</attr>
<!-- The color for when something is wrong.(e.g. exceeding max characters) -->
<attr name="met_errorColor" format="color" />
Expand Down
8 changes: 5 additions & 3 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"/>

<Button
android:id="@+id/setErrorBt"
Expand Down Expand Up @@ -329,9 +331,8 @@
android:hint="Custom Colors"
app:met_baseColor="#007688"
app:met_errorColor="#ddaa00"
app:met_floatingLabel="normal"
app:met_floatingLabel="bottom"
app:met_floatingLabelTextColor="#8805ad"
app:met_helperText="Helper Text"
app:met_helperTextColor="#795548"
app:met_maxCharacters="5"
app:met_primaryColor="#2196F3"
Expand Down Expand Up @@ -361,6 +362,7 @@
app:met_accentTypeface="fonts/Roboto-LightItalic.ttf"
app:met_floatingLabel="normal"
app:met_helperText="Helper Text"
android:layout_gravity="center_horizontal"
app:met_maxCharacters="5"
app:met_typeface="fonts/Roboto-LightItalic.ttf" />

Expand Down

0 comments on commit 2c4e397

Please sign in to comment.