Skip to content

Commit

Permalink
New options: floatingLabelAlwaysShown and helperTextAlwaysShown.
Browse files Browse the repository at this point in the history
  • Loading branch information
rengwuxian committed Jan 17, 2015
1 parent 83f1714 commit 7ab9124
Showing 4 changed files with 118 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -155,6 +155,16 @@ public class MaterialAutoCompleteTextView extends AutoCompleteTextView {
*/
private boolean singleLineEllipsis;

/**
* Always show the floating label, instead of animating it in/out. False by default.
*/
private boolean floatingLabelAlwaysShown;

/**
* Always show the helper text, no matter if the edit text is focused. False by default.
*/
private boolean helperTextAlwaysShown;

/**
* bottom ellipsis's height
*/
@@ -344,6 +354,9 @@ private void init(Context context, AttributeSet attrs) {
iconLeftBitmaps = generateIconBitmaps(typedArray.getResourceId(R.styleable.MaterialEditText_iconLeft, -1));
iconRightBitmaps = generateIconBitmaps(typedArray.getResourceId(R.styleable.MaterialEditText_iconRight, -1));
iconPadding = typedArray.getDimensionPixelSize(R.styleable.MaterialEditText_iconPadding, getPixel(8));
floatingLabelAlwaysShown = typedArray.getBoolean(R.styleable.MaterialEditText_floatingLabelAlwaysShown, false);
helperTextAlwaysShown = typedArray.getBoolean(R.styleable.MaterialEditText_helperTextAlwaysShown, false);
typedArray.recycle();

int[] paddings = new int[]{
android.R.attr.padding, // 0
@@ -358,7 +371,7 @@ private void init(Context context, AttributeSet attrs) {
innerPaddingTop = paddingsTypedArray.getDimensionPixelSize(2, padding);
innerPaddingRight = paddingsTypedArray.getDimensionPixelSize(3, padding);
innerPaddingBottom = paddingsTypedArray.getDimensionPixelSize(4, padding);
typedArray.recycle();
paddingsTypedArray.recycle();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
setBackground(null);
@@ -382,9 +395,12 @@ private void initText() {
if (!TextUtils.isEmpty(getText())) {
CharSequence text = getText();
setText(null);
setHintTextColor(baseColor & 0x00ffffff | 0x44000000);
setText(text);
floatingLabelFraction = 1;
floatingLabelShown = true;
} else {
setHintTextColor(baseColor & 0x00ffffff | 0x44000000);
}
}

@@ -518,6 +534,24 @@ public void setCurrentBottomLines(float currentBottomLines) {
initPadding();
}

public boolean getFloatingLabelAlwaysShown() {
return floatingLabelAlwaysShown;
}

public void setFloatingLabelAlwaysShown(boolean floatingLabelAlwaysShown) {
this.floatingLabelAlwaysShown = floatingLabelAlwaysShown;
invalidate();
}

public boolean getHelperTextAlwaysShown() {
return helperTextAlwaysShown;
}

public void setHelperText(boolean helperTextAlwaysShown) {
this.helperTextAlwaysShown = helperTextAlwaysShown;
invalidate();
}

@Nullable
public Typeface getAccentTypeface() {
return accentTypeface;
@@ -1076,7 +1110,7 @@ protected void onDraw(@NonNull Canvas canvas) {

// draw the bottom text
if (textLayout != null) {
if (tempErrorText != null || (hasFocus() && !TextUtils.isEmpty(helperText))) { // error text or helper text
if (tempErrorText != null || ((helperTextAlwaysShown || hasFocus()) && !TextUtils.isEmpty(helperText))) { // error text or helper text
textPaint.setColor(tempErrorText != null ? errorColor : helperTextColor != -1 ? helperTextColor : getCurrentHintTextColor());
canvas.save();
canvas.translate(startX + getBottomTextLeftOffset(), lineStartY + bottomSpacing - bottomTextPadding);
@@ -1105,10 +1139,10 @@ protected void onDraw(@NonNull Canvas canvas) {
// calculate the vertical position
int floatingLabelStartY = innerPaddingTop + floatingLabelTextSize + floatingLabelSpacing;
int distance = floatingLabelSpacing;
int position = (int) (floatingLabelStartY - distance * floatingLabelFraction);
int position = (int) (floatingLabelStartY - distance * (floatingLabelAlwaysShown ? 1 : floatingLabelFraction));

// calculate the alpha
int alpha = (int) (floatingLabelFraction * 0xff * (0.74f * focusFraction + 0.26f));
int alpha = (int) ((floatingLabelAlwaysShown ? 1 : floatingLabelFraction) * 0xff * (0.74f * focusFraction + 0.26f));
textPaint.setAlpha(alpha);

// draw the floating label
Original file line number Diff line number Diff line change
@@ -28,15 +28,12 @@
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.nineoldandroids.animation.ArgbEvaluator;
import com.nineoldandroids.animation.ObjectAnimator;
import com.rengwuxian.materialedittext.validation.METValidator;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
@@ -158,6 +155,16 @@ public class MaterialEditText extends EditText {
*/
private boolean singleLineEllipsis;

/**
* Always show the floating label, instead of animating it in/out. False by default.
*/
private boolean floatingLabelAlwaysShown;

/**
* Always show the helper text, no matter if the edit text is focused. False by default.
*/
private boolean helperTextAlwaysShown;

/**
* bottom ellipsis's height
*/
@@ -347,6 +354,9 @@ private void init(Context context, AttributeSet attrs) {
iconLeftBitmaps = generateIconBitmaps(typedArray.getResourceId(R.styleable.MaterialEditText_iconLeft, -1));
iconRightBitmaps = generateIconBitmaps(typedArray.getResourceId(R.styleable.MaterialEditText_iconRight, -1));
iconPadding = typedArray.getDimensionPixelSize(R.styleable.MaterialEditText_iconPadding, getPixel(8));
floatingLabelAlwaysShown = typedArray.getBoolean(R.styleable.MaterialEditText_floatingLabelAlwaysShown, false);
helperTextAlwaysShown = typedArray.getBoolean(R.styleable.MaterialEditText_helperTextAlwaysShown, false);
typedArray.recycle();

int[] paddings = new int[]{
android.R.attr.padding, // 0
@@ -361,7 +371,7 @@ private void init(Context context, AttributeSet attrs) {
innerPaddingTop = paddingsTypedArray.getDimensionPixelSize(2, padding);
innerPaddingRight = paddingsTypedArray.getDimensionPixelSize(3, padding);
innerPaddingBottom = paddingsTypedArray.getDimensionPixelSize(4, padding);
typedArray.recycle();
paddingsTypedArray.recycle();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
setBackground(null);
@@ -385,9 +395,12 @@ private void initText() {
if (!TextUtils.isEmpty(getText())) {
CharSequence text = getText();
setText(null);
setHintTextColor(baseColor & 0x00ffffff | 0x44000000);
setText(text);
floatingLabelFraction = 1;
floatingLabelShown = true;
} else {
setHintTextColor(baseColor & 0x00ffffff | 0x44000000);
}
}

@@ -521,6 +534,24 @@ public void setCurrentBottomLines(float currentBottomLines) {
initPadding();
}

public boolean getFloatingLabelAlwaysShown() {
return floatingLabelAlwaysShown;
}

public void setFloatingLabelAlwaysShown(boolean floatingLabelAlwaysShown) {
this.floatingLabelAlwaysShown = floatingLabelAlwaysShown;
invalidate();
}

public boolean getHelperTextAlwaysShown() {
return helperTextAlwaysShown;
}

public void setHelperText(boolean helperTextAlwaysShown) {
this.helperTextAlwaysShown = helperTextAlwaysShown;
invalidate();
}

@Nullable
public Typeface getAccentTypeface() {
return accentTypeface;
@@ -1079,7 +1110,7 @@ protected void onDraw(@NonNull Canvas canvas) {

// draw the bottom text
if (textLayout != null) {
if (tempErrorText != null || (hasFocus() && !TextUtils.isEmpty(helperText))) { // error text or helper text
if (tempErrorText != null || ((helperTextAlwaysShown || hasFocus()) && !TextUtils.isEmpty(helperText))) { // error text or helper text
textPaint.setColor(tempErrorText != null ? errorColor : helperTextColor != -1 ? helperTextColor : getCurrentHintTextColor());
canvas.save();
canvas.translate(startX + getBottomTextLeftOffset(), lineStartY + bottomSpacing - bottomTextPadding);
@@ -1108,10 +1139,10 @@ protected void onDraw(@NonNull Canvas canvas) {
// calculate the vertical position
int floatingLabelStartY = innerPaddingTop + floatingLabelTextSize + floatingLabelSpacing;
int distance = floatingLabelSpacing;
int position = (int) (floatingLabelStartY - distance * floatingLabelFraction);
int position = (int) (floatingLabelStartY - distance * (floatingLabelAlwaysShown ? 1 : floatingLabelFraction));

// calculate the alpha
int alpha = (int) (floatingLabelFraction * 0xff * (0.74f * focusFraction + 0.26f));
int alpha = (int) ((floatingLabelAlwaysShown ? 1 : floatingLabelFraction) * 0xff * (0.74f * focusFraction + 0.26f));
textPaint.setAlpha(alpha);

// draw the floating label
Original file line number Diff line number Diff line change
@@ -152,6 +152,16 @@ public class MaterialMultiAutoCompleteTextView extends MultiAutoCompleteTextView
*/
private boolean singleLineEllipsis;

/**
* Always show the floating label, instead of animating it in/out. False by default.
*/
private boolean floatingLabelAlwaysShown;

/**
* Always show the helper text, no matter if the edit text is focused. False by default.
*/
private boolean helperTextAlwaysShown;

/**
* bottom ellipsis's height
*/
@@ -341,6 +351,9 @@ private void init(Context context, AttributeSet attrs) {
iconLeftBitmaps = generateIconBitmaps(typedArray.getResourceId(R.styleable.MaterialEditText_iconLeft, -1));
iconRightBitmaps = generateIconBitmaps(typedArray.getResourceId(R.styleable.MaterialEditText_iconRight, -1));
iconPadding = typedArray.getDimensionPixelSize(R.styleable.MaterialEditText_iconPadding, getPixel(8));
floatingLabelAlwaysShown = typedArray.getBoolean(R.styleable.MaterialEditText_floatingLabelAlwaysShown, false);
helperTextAlwaysShown = typedArray.getBoolean(R.styleable.MaterialEditText_helperTextAlwaysShown, false);
typedArray.recycle();

int[] paddings = new int[]{
android.R.attr.padding, // 0
@@ -355,7 +368,7 @@ private void init(Context context, AttributeSet attrs) {
innerPaddingTop = paddingsTypedArray.getDimensionPixelSize(2, padding);
innerPaddingRight = paddingsTypedArray.getDimensionPixelSize(3, padding);
innerPaddingBottom = paddingsTypedArray.getDimensionPixelSize(4, padding);
typedArray.recycle();
paddingsTypedArray.recycle();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
setBackground(null);
@@ -379,9 +392,12 @@ private void initText() {
if (!TextUtils.isEmpty(getText())) {
CharSequence text = getText();
setText(null);
setHintTextColor(baseColor & 0x00ffffff | 0x44000000);
setText(text);
floatingLabelFraction = 1;
floatingLabelShown = true;
} else {
setHintTextColor(baseColor & 0x00ffffff | 0x44000000);
}
}

@@ -515,6 +531,24 @@ public void setCurrentBottomLines(float currentBottomLines) {
initPadding();
}

public boolean getFloatingLabelAlwaysShown() {
return floatingLabelAlwaysShown;
}

public void setFloatingLabelAlwaysShown(boolean floatingLabelAlwaysShown) {
this.floatingLabelAlwaysShown = floatingLabelAlwaysShown;
invalidate();
}

public boolean getHelperTextAlwaysShown() {
return helperTextAlwaysShown;
}

public void setHelperText(boolean helperTextAlwaysShown) {
this.helperTextAlwaysShown = helperTextAlwaysShown;
invalidate();
}

@Nullable
public Typeface getAccentTypeface() {
return accentTypeface;
@@ -1073,7 +1107,7 @@ protected void onDraw(@NonNull Canvas canvas) {

// draw the bottom text
if (textLayout != null) {
if (tempErrorText != null || (hasFocus() && !TextUtils.isEmpty(helperText))) { // error text or helper text
if (tempErrorText != null || ((helperTextAlwaysShown || hasFocus()) && !TextUtils.isEmpty(helperText))) { // error text or helper text
textPaint.setColor(tempErrorText != null ? errorColor : helperTextColor != -1 ? helperTextColor : getCurrentHintTextColor());
canvas.save();
canvas.translate(startX + getBottomTextLeftOffset(), lineStartY + bottomSpacing - bottomTextPadding);
@@ -1102,10 +1136,10 @@ protected void onDraw(@NonNull Canvas canvas) {
// calculate the vertical position
int floatingLabelStartY = innerPaddingTop + floatingLabelTextSize + floatingLabelSpacing;
int distance = floatingLabelSpacing;
int position = (int) (floatingLabelStartY - distance * floatingLabelFraction);
int position = (int) (floatingLabelStartY - distance * (floatingLabelAlwaysShown ? 1 : floatingLabelFraction));

// calculate the alpha
int alpha = (int) (floatingLabelFraction * 0xff * (0.74f * focusFraction + 0.26f));
int alpha = (int) ((floatingLabelAlwaysShown ? 1 : floatingLabelFraction) * 0xff * (0.74f * focusFraction + 0.26f));
textPaint.setAlpha(alpha);

// draw the floating label
4 changes: 4 additions & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -45,5 +45,9 @@
<attr name="floatingLabelTextSize" format="dimension" />
<!-- The bottom texts' size. 12sp by default. -->
<attr name="bottomTextSize" format="dimension" />
<!-- Always show the floating label, instead of animating it in/out. -->
<attr name="floatingLabelAlwaysShown" format="boolean" />
<!-- Always show the helper text, no matter if the edit text is focused. -->
<attr name="helperTextAlwaysShown" format="boolean" />
</declare-styleable>
</resources>

0 comments on commit 7ab9124

Please sign in to comment.