Skip to content

[TextInputLayout] set the endIconOnClickListener also with endIconMode #1581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ void initialize() {
AppCompatResources.getDrawable(context, R.drawable.mtrl_ic_cancel));
textInputLayout.setEndIconContentDescription(
textInputLayout.getResources().getText(R.string.clear_text_end_icon_content_description));
textInputLayout.setEndIconOnClickListener(
textInputLayout.internalSetEndIconOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
Expand All @@ -118,6 +118,7 @@ public void onClick(View v) {
}

textInputLayout.refreshEndIconDrawableState();
callOriginalEndIconOnClickListener(v);
}
});
textInputLayout.addOnEditTextAttachedListener(clearTextOnEditTextAttachedListener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class CustomEndIconDelegate extends EndIconDelegate {

@Override
void initialize() {
textInputLayout.setEndIconOnClickListener(null);
textInputLayout.internalSetEndIconOnClickListener(null);
textInputLayout.setEndIconOnLongClickListener(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,13 @@ void initialize() {
textInputLayout.setEndIconDrawable(AppCompatResources.getDrawable(context, drawableResId));
textInputLayout.setEndIconContentDescription(
textInputLayout.getResources().getText(R.string.exposed_dropdown_menu_content_description));
textInputLayout.setEndIconOnClickListener(
textInputLayout.internalSetEndIconOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
AutoCompleteTextView editText = (AutoCompleteTextView) textInputLayout.getEditText();
showHideDropdown(editText);
callOriginalEndIconOnClickListener(v);
}
});
textInputLayout.addOnEditTextAttachedListener(dropdownMenuOnEditTextAttachedListener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.android.material.textfield;

import android.content.Context;
import android.view.View;
import androidx.annotation.NonNull;
import com.google.android.material.internal.CheckableImageButton;
import com.google.android.material.textfield.TextInputLayout.BoxBackgroundMode;
Expand Down Expand Up @@ -68,4 +69,16 @@ boolean isBoxBackgroundModeSupported(@BoxBackgroundMode int boxBackgroundMode) {
* @param visible whether the icon should be set to visible
*/
void onSuffixVisibilityChanged(boolean visible) {}

/**
* This method allows to call the {@link TextInputLayout#setEndIconOnClickListener} original set on the TextInputLayout
*
* @param view The view that was clicked
*/
void callOriginalEndIconOnClickListener(View view){
if (textInputLayout.endIconOnClickListener != null) {
textInputLayout.endIconOnClickListener.onClick(view);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void initialize() {
AppCompatResources.getDrawable(context, R.drawable.design_password_eye));
textInputLayout.setEndIconContentDescription(
textInputLayout.getResources().getText(R.string.password_toggle_content_description));
textInputLayout.setEndIconOnClickListener(
textInputLayout.internalSetEndIconOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
Expand All @@ -102,6 +102,7 @@ public void onClick(View v) {
}

textInputLayout.refreshEndIconDrawableState();
callOriginalEndIconOnClickListener(v);
}
});
textInputLayout.addOnEditTextAttachedListener(onEditTextAttachedListener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ public interface OnEndIconChangedListener {
@Nullable private Drawable endDummyDrawable;
private int endDummyDrawableWidth;
private Drawable originalEditTextEndDrawable;
protected OnClickListener endIconOnClickListener;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this and where is it being used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is used in the EndIconDelegate#callOriginalEndIconOnClickListener method.

private OnLongClickListener endIconOnLongClickListener;
private OnLongClickListener errorIconOnLongClickListener;
@NonNull private final CheckableImageButton errorIconView;
Expand Down Expand Up @@ -3056,6 +3057,13 @@ public int getEndIconMode() {
* will have
*/
public void setEndIconOnClickListener(@Nullable OnClickListener endIconOnClickListener) {
this.endIconOnClickListener = endIconOnClickListener;
if (getEndIconMode() == END_ICON_NONE || getEndIconMode() == END_ICON_CUSTOM) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about other modes?

internalSetEndIconOnClickListener(endIconOnClickListener);
}
}

void internalSetEndIconOnClickListener(@Nullable OnClickListener endIconOnClickListener) {
setIconOnClickListener(endIconView, endIconOnClickListener, endIconOnLongClickListener);
}

Expand Down