MAY3AM
MAY3AM

Reputation: 1212

Change EditText inputType after enter first character by user

I have an EditText like this in my xml file:

<EditText
    android:id="@+id/InputPass"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:layout_marginBottom="20dp"
    android:layout_marginLeft="40dp"
    android:layout_marginRight="40dp"
    android:drawableLeft="@drawable/ic_lock"
    android:gravity="right"
    android:hint="رمز عبور"
    android:singleLine="true"
    android:textSize="18sp" />

For some reason I want to change its inputType after user entered first Character. I try this way:

Pass.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int before, int count) {

                if(start == 0){
                    Pass.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

But it does not work! Is anybody there that know any solution for that?

Upvotes: 0

Views: 725

Answers (3)

MAY3AM
MAY3AM

Reputation: 1212

I achieved to the best result by this one:

Pass.setOnFocusChangeListener(new OnFocusChangeListener() {

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if(hasFocus){
            Pass.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }
    }
});

Upvotes: 0

Human
Human

Reputation: 10815

Change :

Pass.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);

To :

Pass.setTransformationMethod(PasswordTransformationMethod.getInstance());

Upvotes: 1

karvoynistas
karvoynistas

Reputation: 1285

Inside the

afterTextChanged

method, try to to the following :

  @Override
        public void afterTextChanged(Editable s) {
            System.out.println("Editable: "+s);
            if(s.getText().toString().length ==1)
               Pass.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        }

Upvotes: 0

Related Questions