Skip to content

Commit

Permalink
Added clear text drawable parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Aug 17, 2017
1 parent ee0b68c commit aec203c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ public class MaterialEditText extends MaterialBaseEditText {
/**
* Clear Button
*/
private Bitmap[] clearButtonBitmaps;
private int clearButtonDrawable;

/**
* Auto validate when focus lost.
Expand Down Expand Up @@ -422,7 +422,7 @@ private void init(Context context, AttributeSet attrs) {
iconLeftBitmaps = generateIconBitmaps(typedArray.getResourceId(R.styleable.MaterialEditText_met_iconLeft, -1));
iconRightBitmaps = generateIconBitmaps(typedArray.getResourceId(R.styleable.MaterialEditText_met_iconRight, -1));
showClearButton = typedArray.getBoolean(R.styleable.MaterialEditText_met_clearButton, false);
clearButtonBitmaps = generateIconBitmaps(R.drawable.met_ic_clear);
clearButtonDrawable = typedArray.getResourceId(R.styleable.MaterialEditText_met_clearIconDrawable, R.drawable.met_ic_clear);
iconPadding = typedArray.getDimensionPixelSize(R.styleable.MaterialEditText_met_iconPadding, getPixel(16));
floatingLabelAlwaysShown = typedArray.getBoolean(R.styleable.MaterialEditText_met_floatingLabelAlwaysShown, false);
helperTextAlwaysShown = typedArray.getBoolean(R.styleable.MaterialEditText_met_helperTextAlwaysShown, false);
Expand Down Expand Up @@ -496,6 +496,9 @@ public void afterTextChanged(Editable s) {
} else {
setError(null);
}
if (s.length() == 0 && showClearButton) {
hideClearButton();
}
postInvalidate();
}
});
Expand Down Expand Up @@ -1304,7 +1307,7 @@ protected void onDraw(@NonNull Canvas canvas) {

// draw the clear button
if (hasFocus() && showClearButton && !TextUtils.isEmpty(getText()) && isEnabled()) {
setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.met_ic_clear, 0);
showClearButton();
}

// draw the underline
Expand Down Expand Up @@ -1414,6 +1417,10 @@ protected void onDraw(@NonNull Canvas canvas) {
super.onDraw(canvas);
}

private void showClearButton() {
setCompoundDrawablesWithIntrinsicBounds(0, 0, clearButtonDrawable, 0);
}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private boolean isRTL() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
Expand Down Expand Up @@ -1495,7 +1502,7 @@ public boolean onTouchEvent(MotionEvent event) {
if (clearButtonClicking) {
if (!TextUtils.isEmpty(getText())) {
setText(null);
setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
hideClearButton();
}
clearButtonClicking = false;
}
Expand All @@ -1522,4 +1529,9 @@ private int checkLength(CharSequence text) {
if (lengthChecker == null) return text.length();
return lengthChecker.getLength(text);
}

private void hideClearButton() {
setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
}

}
2 changes: 2 additions & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
<attr name="met_validateOnFocusLost" format="boolean"/>
<!-- Whether check the characters count at the beginning it's shown. True by default. -->
<attr name="met_checkCharactersCountAtBeginning" format="boolean"/>
<!-- Clear icon -->
<attr name="met_clearIconDrawable" format="reference" />

</declare-styleable>
</resources>

0 comments on commit aec203c

Please sign in to comment.