Search code examples
androidandroid-edittextandroid-inputtype

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?


Solution

  • 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());
            }
        }
    });