Skip to content

Commit

Permalink
End Icon
Browse files Browse the repository at this point in the history
  • Loading branch information
HITGIF committed Sep 10, 2017
1 parent b859e21 commit c2eb24f
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,22 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// final Button darkButton = findViewById(R.id.dark_button);
// if (dark) darkButton.setText("LIGHT SIDE");
// else darkButton.setText("DARK SIDE");
// darkButton.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// if (dark) {
// sharedPreferences.edit().putBoolean("dark", false).apply();
// restart();
// }
// else {
// sharedPreferences.edit().putBoolean("dark", true).apply();
// restart();
// }
// }
// });
final Button darkButton = findViewById(R.id.dark_button);
if (dark) darkButton.setText("LIGHT SIDE");
else darkButton.setText("DARK SIDE");
darkButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (dark) {
sharedPreferences.edit().putBoolean("dark", false).apply();
restart();
}
else {
sharedPreferences.edit().putBoolean("dark", true).apply();
restart();
}
}
});
}

public void restart() {
Expand Down
16 changes: 11 additions & 5 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
android:orientation="vertical">

<studio.carbonylgroup.textfieldboxes.TextFieldBoxes
android:id="@+id/tfb"
android:id="@+id/text_field_boxes"
android:layout_width="250dp"
android:layout_height="wrap_content"
app:helperText="Helper is here"
Expand All @@ -26,15 +26,21 @@
app:labelText="Label"
app:maxCharacters="10"
app:singleLine="true"
app:hasClearButton="true"
app:endIcon="@drawable/ic_mic_black_24dp"
app:text="Text" />

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

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

</LinearLayout>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ public class TextFieldBoxes extends FrameLayout {
*/
protected int iconSignifierResourceId;

/**
* the resource ID of the icon at the end. 0 by default.
*/
protected int endIconResourceId;

/**
* whether to show the clear button at the end of the EditText. False by default.
*/
Expand All @@ -145,7 +150,8 @@ public class TextFieldBoxes extends FrameLayout {
protected AppCompatTextView counterLabel;
protected AppCompatTextView floatingLabel;
protected AppCompatImageButton clearButton;
protected AppCompatImageButton iconImageView;
protected AppCompatImageButton iconImageButton;
protected AppCompatImageButton endIconImageButton;
protected InputMethodManager inputMethodManager;
protected RelativeLayout rightShell;
protected RelativeLayout upperPanel;
Expand Down Expand Up @@ -235,9 +241,12 @@ protected void onFinishInflate() {
this.clearButton = findViewById(R.id.text_field_boxes_clear_button);
this.clearButton.setColorFilter(DEFAULT_TEXT_COLOR);
this.clearButton.setAlpha(0.35f);
this.endIconImageButton = findViewById(R.id.text_field_boxes_end_icon_button);
this.endIconImageButton.setColorFilter(DEFAULT_TEXT_COLOR);
this.endIconImageButton.setAlpha(0.54f);
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.iconImageButton = 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;
Expand All @@ -251,7 +260,7 @@ public void onClick(View v) {
}
});

this.iconImageView.setOnClickListener(new OnClickListener() {
this.iconImageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (!isActivated()) activate(true);
Expand Down Expand Up @@ -317,6 +326,7 @@ public void onClick(View view) {
setSingleLine(this.singleLine);
setMaxLines(this.maxLines);
setIconSignifier(this.iconSignifierResourceId);
setEndIcon(this.endIconResourceId);
setHasClearButton(this.hasClearButton);
setHasFocus(this.hasFocus);
updateCounterText();
Expand All @@ -332,11 +342,56 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

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);
if (this.endIconImageButton.getVisibility() == View.VISIBLE) {

((RelativeLayout.LayoutParams) this.clearButton.getLayoutParams())
.addRule(RelativeLayout.RIGHT_OF, 0);
((RelativeLayout.LayoutParams) this.clearButton.getLayoutParams())
.addRule(RelativeLayout.LEFT_OF, R.id.text_field_boxes_end_icon_button);

if (android.os.Build.VERSION.SDK_INT >= 17) {
((RelativeLayout.LayoutParams) this.clearButton.getLayoutParams())
.addRule(RelativeLayout.END_OF, 0);
((RelativeLayout.LayoutParams) this.clearButton.getLayoutParams())
.addRule(RelativeLayout.START_OF, R.id.text_field_boxes_end_icon_button);
}

((RelativeLayout.LayoutParams) this.endIconImageButton.getLayoutParams())
.addRule(RelativeLayout.RIGHT_OF, 0);
((RelativeLayout.LayoutParams) this.endIconImageButton.getLayoutParams())
.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

if (android.os.Build.VERSION.SDK_INT >= 17) {
((RelativeLayout.LayoutParams) this.endIconImageButton.getLayoutParams())
.addRule(RelativeLayout.END_OF, 0);
((RelativeLayout.LayoutParams) this.endIconImageButton.getLayoutParams())
.addRule(RelativeLayout.ALIGN_PARENT_END);
}

if (this.hasClearButton)
((RelativeLayout.LayoutParams) this.editText.getLayoutParams())
.addRule(RelativeLayout.LEFT_OF, R.id.text_field_boxes_clear_button);
else
((RelativeLayout.LayoutParams) this.editText.getLayoutParams())
.addRule(RelativeLayout.LEFT_OF, R.id.text_field_boxes_end_icon_button);

} else {

((RelativeLayout.LayoutParams) this.clearButton.getLayoutParams())
.addRule(RelativeLayout.RIGHT_OF, 0);
((RelativeLayout.LayoutParams) this.clearButton.getLayoutParams())
.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

if (android.os.Build.VERSION.SDK_INT >= 17) {
((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) {

Expand All @@ -349,11 +404,16 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (heightMode == MeasureSpec.EXACTLY) {

/* match_parent or specific value */
((RelativeLayout.LayoutParams) this.bottomPart.getLayoutParams()).addRule(RelativeLayout.BELOW, 0);
((RelativeLayout.LayoutParams) this.bottomLine.getLayoutParams()).addRule(RelativeLayout.BELOW, 0);
((RelativeLayout.LayoutParams) this.bottomPart.getLayoutParams()).addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
((RelativeLayout.LayoutParams) this.bottomLine.getLayoutParams()).addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
((RelativeLayout.LayoutParams) this.panel.getLayoutParams()).addRule(RelativeLayout.ABOVE, R.id.text_field_boxes_bottom);
((RelativeLayout.LayoutParams) this.bottomPart.getLayoutParams())
.addRule(RelativeLayout.BELOW, 0);
((RelativeLayout.LayoutParams) this.bottomLine.getLayoutParams())
.addRule(RelativeLayout.BELOW, 0);
((RelativeLayout.LayoutParams) this.bottomPart.getLayoutParams())
.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
((RelativeLayout.LayoutParams) this.bottomLine.getLayoutParams())
.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
((RelativeLayout.LayoutParams) this.panel.getLayoutParams())
.addRule(RelativeLayout.ABOVE, R.id.text_field_boxes_bottom);

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

Expand Down Expand Up @@ -408,6 +468,8 @@ 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.endIconResourceId = styledAttrs.
getResourceId(R.styleable.TextFieldBoxes_endIcon, 0);
this.hasClearButton = styledAttrs.getBoolean(R.styleable.TextFieldBoxes_hasClearButton, false);
this.hasFocus = styledAttrs.getBoolean(R.styleable.TextFieldBoxes_hasFocus, false);

Expand Down Expand Up @@ -491,10 +553,10 @@ protected void setHighlightColor(int colorRes) {
this.floatingLabel.setTextColor(colorRes);
Utils.setCursorDrawableColor(this.editText, colorRes);

this.iconImageView.setColorFilter(colorRes);
if (colorRes == DEFAULT_TEXT_COLOR) this.iconImageView.setAlpha(0.54f);
else this.iconImageView.setAlpha(1f);
if (colorRes == DEFAULT_DISABLED_TEXT_COLOR) this.iconImageView.setAlpha(0.35f);
this.iconImageButton.setColorFilter(colorRes);
if (colorRes == DEFAULT_TEXT_COLOR) this.iconImageButton.setAlpha(0.54f);
else this.iconImageButton.setAlpha(1f);
if (colorRes == DEFAULT_DISABLED_TEXT_COLOR) this.iconImageButton.setAlpha(0.35f);

this.bottomLine.setBackgroundColor(colorRes);
}
Expand Down Expand Up @@ -724,8 +786,8 @@ public void setEnabled(boolean enabled) {
this.counterLabel.setVisibility(View.VISIBLE);
this.bottomLine.setVisibility(View.VISIBLE);
this.panel.setEnabled(true);
this.iconImageView.setEnabled(true);
this.iconImageView.setClickable(true);
this.iconImageButton.setEnabled(true);
this.iconImageButton.setClickable(true);
setHighlightColor(DEFAULT_TEXT_COLOR);
updateCounterText();

Expand All @@ -735,8 +797,8 @@ public void setEnabled(boolean enabled) {
this.editText.setEnabled(false);
this.editText.setFocusableInTouchMode(false);
this.editText.setFocusable(false);
this.iconImageView.setClickable(false);
this.iconImageView.setEnabled(false);
this.iconImageButton.setClickable(false);
this.iconImageButton.setEnabled(false);
this.helperLabel.setVisibility(View.INVISIBLE);
this.counterLabel.setVisibility(View.INVISIBLE);
this.bottomLine.setVisibility(View.INVISIBLE);
Expand Down Expand Up @@ -781,8 +843,8 @@ public void setIconSignifier(int resourceID) {

this.iconSignifierResourceId = resourceID;
if (this.iconSignifierResourceId != 0) {
this.iconImageView.setImageResource(this.iconSignifierResourceId);
this.iconImageView.setVisibility(View.VISIBLE);
this.iconImageButton.setImageResource(this.iconSignifierResourceId);
this.iconImageButton.setVisibility(View.VISIBLE);
} else removeIconSignifier();
}

Expand All @@ -792,7 +854,25 @@ public void setIconSignifier(int resourceID) {
public void removeIconSignifier() {

this.iconSignifierResourceId = 0;
this.iconImageView.setVisibility(View.GONE);
this.iconImageButton.setVisibility(View.GONE);
}

public void setEndIcon(int resourceID) {

this.endIconResourceId = resourceID;
if (this.endIconResourceId != 0) {
this.endIconImageButton.setImageResource(this.endIconResourceId);
this.endIconImageButton.setVisibility(View.VISIBLE);
} else removeEndIcon();
}

/**
* remove the end icon by setting the visibility of the end image view to View.GONE
*/
public void removeEndIcon() {

this.endIconResourceId = 0;
this.endIconImageButton.setVisibility(View.GONE);
}

public void setHasClearButton(boolean hasClearButton) {
Expand Down Expand Up @@ -908,8 +988,12 @@ public AppCompatTextView getFloatingLabel() {
return this.floatingLabel;
}

public AppCompatImageButton getIconImageView() {
return iconImageView;
public AppCompatImageButton getIconImageButton() {
return this.iconImageButton;
}

public AppCompatImageButton getEndIconImageButton() {
return this.endIconImageButton;
}

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

public int getEndIconResourceId() {
return this.endIconResourceId;
}

public boolean getHasClearButton() {
return this.hasClearButton;
}
Expand All @@ -941,4 +1029,3 @@ public boolean getHasFocus() {
return this.hasFocus;
}
}

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.
13 changes: 13 additions & 0 deletions textfieldboxes/src/main/res/layout/text_field_boxes_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@
android:src="@drawable/ic_clear_circle_black_24dp"
android:visibility="gone" />

<android.support.v7.widget.AppCompatImageButton
android:id="@+id/text_field_boxes_end_icon_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_clear_button"
android:layout_toRightOf="@+id/text_field_boxes_clear_button"
android:layout_alignBottom="@+id/text_field_boxes_editText"
android:background="?selectableItemBackgroundBorderless"
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="endIcon" format="reference" />
<attr name="hasClearButton" format="boolean" />
<attr name="hasFocus" format="boolean" />

Expand Down

0 comments on commit c2eb24f

Please sign in to comment.