Skip to content

Commit

Permalink
Clear Button
Browse files Browse the repository at this point in the history
  • Loading branch information
HITGIF committed Sep 10, 2017
1 parent 4781d89 commit b859e21
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 48 deletions.
56 changes: 29 additions & 27 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,40 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<!--<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"-->
<!--xmlns:app="http://schemas.android.com/apk/res-auto"-->
<!--xmlns:tools="http://schemas.android.com/tools"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent"-->
<!--android:scrollbarStyle="outsideOverlay"-->
<!--tools:context=".MainActivity">-->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical">
android:scrollbarStyle="outsideOverlay"
tools:context=".MainActivity">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical">

<studio.carbonylgroup.textfieldboxes.TextFieldBoxes
android:id="@+id/tfb"
android:layout_width="250dp"
android:layout_height="wrap_content"
app:helperText="Helper is here"
app:labelText="Label"
app:hint="Hint"
app:iconSignifier="@drawable/ic_location_on_black_24dp"
app:hasFocus="true"
app:labelText="Label"
app:maxCharacters="10"
app:singleLine="true"
app:maxCharacters="10" />
app:hasClearButton="true"
app:text="Text" />

<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_marginTop="100dp"
android:layout_height="wrap_content"
android:text="\@scyscjhzcs hjzcs hzjcs zshjzshc " />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:text="\@scyscjhzcs hjzces hzjcs zshjzshc " />

</LinearLayout>
</LinearLayout>

<!--<LinearLayout-->
<!--android:layout_width="match_parent"-->
Expand Down Expand Up @@ -77,8 +79,8 @@
<!--android:layout_gravity="center_horizontal"-->
<!--android:layout_weight="2"-->
<!--app:hasFocus="true"-->
<!--app:labelText="Location (optional)"-->
<!--app:iconSignifier="@drawable/ic_location_on_black_24dp"-->
<!--app:labelText="Location (optional)"-->
<!--app:singleLine="true"-->
<!--app:text="" />-->

Expand All @@ -97,12 +99,12 @@
<!--app:text="Unique and rare dress from 1952. Made out of washed cotton with front pockets. Sleeveless with button closures." />-->

<!--<Button-->
<!--android:id="@+id/dark_button"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:id="@+id/dark_button"-->
<!--android:layout_marginTop="30dp"-->
<!--android:text="DARK SIDE"/>-->
<!--android:text="DARK SIDE" />-->

<!--</LinearLayout>-->

<!--</ScrollView>-->
</ScrollView>
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ protected void onDraw(Canvas canvas) {

canvas.getClipBounds(rect);
rectF.set(rect);
clipPath.reset();
clipPath.addRoundRect(rectF, cornerRadius, cornerRadius, Path.Direction.CW);
canvas.clipPath(clipPath);
super.onDraw(canvas);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.LayerDrawable;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatImageButton;
import android.support.v7.widget.AppCompatTextView;
import android.text.Editable;
import android.text.TextWatcher;
Expand Down Expand Up @@ -127,19 +127,25 @@ public class TextFieldBoxes extends FrameLayout {
*/
protected int iconSignifierResourceId;

/**
* whether to show the clear button at the end of the EditText. False by default.
*/
protected boolean hasClearButton;

/**
* whether the EditText is having the focus. False by default.
*/
protected boolean hasFocus;

protected View panel;
protected FrameLayout bottomLine;
protected View bottomLine;
protected ViewGroup editTextLayout;
protected ExtendedEditText editText;
protected AppCompatTextView helperLabel;
protected AppCompatTextView counterLabel;
protected AppCompatTextView floatingLabel;
protected AppCompatImageView iconImageView;
protected AppCompatImageButton clearButton;
protected AppCompatImageButton iconImageView;
protected InputMethodManager inputMethodManager;
protected RelativeLayout rightShell;
protected RelativeLayout upperPanel;
Expand Down Expand Up @@ -226,14 +232,17 @@ protected void onFinishInflate() {
this.upperPanel = findViewById(R.id.text_field_boxes_upper_panel);
this.bottomPart = findViewById(R.id.text_field_boxes_bottom);
this.labelColor = this.floatingLabel.getCurrentTextColor();
this.clearButton = findViewById(R.id.text_field_boxes_clear_button);
this.clearButton.setColorFilter(DEFAULT_TEXT_COLOR);
this.clearButton.setAlpha(0.35f);
this.helperLabel = findViewById(R.id.text_field_boxes_helper);
this.counterLabel = findViewById(R.id.text_field_boxes_counter);
this.iconImageView = findViewById(R.id.text_field_boxes_imageView);
this.editTextLayout = findViewById(R.id.text_field_boxes_editTextLayout);
this.labelTopMargin = RelativeLayout.LayoutParams.class
.cast(this.floatingLabel.getLayoutParams()).topMargin;

panel.setOnClickListener(new OnClickListener() {
this.panel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (!isActivated()) activate(true);
Expand All @@ -242,7 +251,7 @@ public void onClick(View v) {
}
});

iconImageView.setOnClickListener(new OnClickListener() {
this.iconImageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (!isActivated()) activate(true);
Expand All @@ -251,15 +260,15 @@ public void onClick(View v) {
}
});

editText.setOnFocusChangeListener(new OnFocusChangeListener() {
this.editText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
if (b) setHasFocus(true);
else setHasFocus(false);
}
});

editText.addTextChangedListener(new TextWatcher() {
this.editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
Expand All @@ -276,6 +285,13 @@ public void afterTextChanged(Editable editable) {
}
});

this.clearButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
setText("");
}
});

/* Texts */
setText(this.text);
setLabelText(this.labelText);
Expand All @@ -301,6 +317,7 @@ public void afterTextChanged(Editable editable) {
setSingleLine(this.singleLine);
setMaxLines(this.maxLines);
setIconSignifier(this.iconSignifierResourceId);
setHasClearButton(this.hasClearButton);
setHasFocus(this.hasFocus);
updateCounterText();
}
Expand All @@ -313,7 +330,15 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);

if (widthMode == MeasureSpec.AT_MOST) {
if (widthMode == MeasureSpec.EXACTLY) {

((RelativeLayout.LayoutParams) this.clearButton.getLayoutParams()).addRule(RelativeLayout.RIGHT_OF, 0);
((RelativeLayout.LayoutParams) this.clearButton.getLayoutParams()).addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
((RelativeLayout.LayoutParams) this.clearButton.getLayoutParams()).addRule(RelativeLayout.END_OF, 0);
((RelativeLayout.LayoutParams) this.clearButton.getLayoutParams()).addRule(RelativeLayout.ALIGN_PARENT_END);
((RelativeLayout.LayoutParams) this.editText.getLayoutParams()).addRule(RelativeLayout.LEFT_OF, R.id.text_field_boxes_clear_button);

} else if (widthMode == MeasureSpec.AT_MOST) {

/* wrap_content */
this.editText.getLayoutParams().width = ViewGroup.LayoutParams.WRAP_CONTENT;
Expand Down Expand Up @@ -383,6 +408,7 @@ protected void handleAttributes(Context context, AttributeSet attrs) {
this.maxLines = styledAttrs.getInt(R.styleable.TextFieldBoxes_maxLines, Integer.MAX_VALUE);
this.iconSignifierResourceId = styledAttrs.
getResourceId(R.styleable.TextFieldBoxes_iconSignifier, 0);
this.hasClearButton = styledAttrs.getBoolean(R.styleable.TextFieldBoxes_hasClearButton, false);
this.hasFocus = styledAttrs.getBoolean(R.styleable.TextFieldBoxes_hasFocus, false);

styledAttrs.recycle();
Expand Down Expand Up @@ -483,6 +509,11 @@ protected void setHighlightColor(int colorRes) {
*/
protected void updateCounterText() {

/* Show clear button if there is anything */
if (hasClearButton)
if (getText().length() == 0) showClearButton(false);
else showClearButton(true);

/* Don't Count Space & Line Feed */
int length = getText().replaceAll(" ", "").replaceAll("\n", "").length();
String lengthStr = Integer.toString(length) + " / ";
Expand All @@ -492,12 +523,8 @@ protected void updateCounterText() {
/* MAX & MIN */
this.counterLabel.setText(lengthStr + Integer.toString(this.minCharacters)
+ "-" + Integer.toString(this.maxCharacters));
if (length < this.minCharacters || length > this.maxCharacters) {
setCounterError();
}
else {
removeCounterError();
}
if (length < this.minCharacters || length > this.maxCharacters) setCounterError();
else removeCounterError();

} else {
/* MAX ONLY */
Expand Down Expand Up @@ -571,7 +598,14 @@ public void removeError() {
this.helperLabel.setText(this.helperText);
}

protected void showClearButton(boolean show) {

if (show) this.clearButton.setVisibility(View.VISIBLE);
else this.clearButton.setVisibility(View.GONE);
}

/* Text Setters */

/**
* set EditText text, raise the labelText floatingLabel if there is something
*
Expand Down Expand Up @@ -761,6 +795,10 @@ public void removeIconSignifier() {
this.iconImageView.setVisibility(View.GONE);
}

public void setHasClearButton(boolean hasClearButton) {
this.hasClearButton = hasClearButton;
}

/**
* set if the EditText is having focus
*
Expand All @@ -783,10 +821,6 @@ public void setHasFocus(boolean hasFocus) {
}

/* Text Getters */
public EditText getEditText() {
return this.editText;
}

public String getText() {
return this.text;
}
Expand Down Expand Up @@ -849,6 +883,35 @@ public int getMinCharacters() {
return this.minCharacters;
}

/* View Getters */
public View getPanel() {
return this.panel;
}

public View getBottomLine() {
return this.bottomLine;
}

public EditText getEditText() {
return this.editText;
}

public AppCompatTextView getHelperLabel() {
return this.helperLabel;
}

public AppCompatTextView getCounterLabel() {
return this.counterLabel;
}

public AppCompatTextView getFloatingLabel() {
return this.floatingLabel;
}

public AppCompatImageButton getIconImageView() {
return iconImageView;
}

/* Other Getters */
public boolean isActivated() {
return this.activated;
Expand All @@ -870,6 +933,10 @@ public int getIconSignifierResourceId() {
return this.iconSignifierResourceId;
}

public boolean getHasClearButton() {
return this.hasClearButton;
}

public boolean getHasFocus() {
return this.hasFocus;
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 17 additions & 3 deletions textfieldboxes/src/main/res/layout/text_field_boxes_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
android:layout_height="match_parent"
android:orientation="horizontal">

<android.support.v7.widget.AppCompatImageView
<android.support.v7.widget.AppCompatImageButton
android:id="@+id/text_field_boxes_imageView"
android:layout_width="24dp"
android:layout_height="24dp"
Expand Down Expand Up @@ -61,7 +61,7 @@
android:textSize="16sp"
tools:text="Label" />

<FrameLayout
<RelativeLayout
android:id="@+id/text_field_boxes_editTextLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand All @@ -78,7 +78,21 @@
android:text="@string/app_name"
android:textSize="16sp" />

</FrameLayout>
<android.support.v7.widget.AppCompatImageButton
android:id="@+id/text_field_boxes_clear_button"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginLeft="2dp"
android:layout_marginStart="2dp"
android:paddingTop="2dp"
android:layout_toEndOf="@+id/text_field_boxes_editText"
android:layout_toRightOf="@+id/text_field_boxes_editText"
android:layout_alignBottom="@+id/text_field_boxes_editText"
android:background="?selectableItemBackgroundBorderless"
android:src="@drawable/ic_clear_circle_black_24dp"
android:visibility="gone" />

</RelativeLayout>

</RelativeLayout>

Expand Down
1 change: 1 addition & 0 deletions textfieldboxes/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<attr name="singleLine" format="boolean" />
<attr name="maxLines" format="integer" />
<attr name="iconSignifier" format="reference" />
<attr name="hasClearButton" format="boolean" />
<attr name="hasFocus" format="boolean" />

</declare-styleable>
Expand Down

0 comments on commit b859e21

Please sign in to comment.