Skip to content

Commit

Permalink
refactor: changing InputType of KeyListener whenever the InputType is…
Browse files Browse the repository at this point in the history
… changed
  • Loading branch information
igoralvess committed May 12, 2021
1 parent 8a2cf22 commit b1f262d
Showing 1 changed file with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.method.DigitsKeyListener;
import android.text.method.KeyListener;
Expand Down Expand Up @@ -33,6 +34,7 @@ public class TextInput extends ConstraintLayout {
private TextInputLayout inputLayout;
private ImageView icon;
private KeyListener inputKeyListener;
private int inputType;

public TextInput(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
Expand All @@ -44,6 +46,8 @@ public TextInput(Context context, @Nullable AttributeSet attrs) {
input = findViewById(R.id.et_input);
inputLayout = findViewById(R.id.input);
icon = findViewById(R.id.iv_selected_icon);
inputKeyListener = input.getKeyListener();
inputType = inputKeyListener.getInputType();

setupAttrs(context, attrs);
setOnClickListener(onClickListener());
Expand Down Expand Up @@ -119,6 +123,7 @@ private void setupAttrs(Context context, @Nullable AttributeSet attrs) {
.getInt(attr, EditorInfo.TYPE_TEXT_VARIATION_NORMAL);

input.setInputType(inputType);
this.inputType = inputType;

} else if (attr == R.styleable.TextInput_android_selectAllOnFocus) {
boolean selectAllOnFocus = styledAttributes.getBoolean(attr, false);
Expand Down Expand Up @@ -160,20 +165,20 @@ private void setupAttrs(Context context, @Nullable AttributeSet attrs) {
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
this.input.setEnabled(enabled);
saveInputKeyListener();
setEllipsized(!enabled);

}

private void saveInputKeyListener() {
if(input.getKeyListener() != null)
inputKeyListener = input.getKeyListener();
}

public void setSelectorEnabled(boolean enabled) {
super.setEnabled(enabled);
}

public void setInputType(int inputType) {
this.inputType = inputType;
input.setInputType(inputType);
}

private void setEllipsized(boolean enabled) {
this.input.setCursorVisible(!enabled);
if (enabled) {
Expand All @@ -185,6 +190,7 @@ private void setEllipsized(boolean enabled) {

this.input.setEllipsize(null);
this.input.setKeyListener(inputKeyListener);
this.input.setInputType(inputType);
}

private OnClickListener onClickListener() {
Expand Down

0 comments on commit b1f262d

Please sign in to comment.