Sinbod User
Sinbod User

Reputation: 11

Android EditText, toggle password visiblity?

You may say this is duplicated but it's not. Actually I found solutions but none worked perfectly. Last solution was using TextInputLayout plus EditText inside it but is there a way to move the toggle button to other side of EditText? If not, so it's of no use for me.

 <android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="30dp"
    android:layout_marginRight="30dp"
    android:layout_marginTop="130dp"
    android:layoutDirection="rtl"
    app:passwordToggleEnabled="true">

    <EditText
        android:id="@+id/edlrPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Password"
        android:paddingRight="10dp"
        android:paddingLeft="10dp"
        android:layoutDirection="rtl"
        android:textAlignment="viewStart"
        android:inputType="textPassword"
        android:maxLines="1"
        android:textSize="14sp" />

</android.support.design.widget.TextInputLayout>

Again, problem is that the toggle button places on the EditText making part of it invisible.

I have tried these piece of code too:

if (b)
    editText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
else 
    editText.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);

Also I tried this code:

 if (b)
     editText.setInputType(InputType.TYPE_CLASS_TEXT);
 else
     editText.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);

Both work for the first change, and then the text inside EditText remains visible all the time. What should I do? Any help is appreciated.

UPDATE: So I just noticed I needed to add android:layoutDirection="rtl" to the TextInputLayout! Previously I had added it to just editText, causing a conflict (as TextInputLayout was ltr by default) which made toggle button cover the editText. By the way when I put my editText inside a TextInputLayout, then setTransformationMethod(new PasswordTransformationMethod()) also works perfectly though I don't need it anymore!

Thank all for participation.

Upvotes: 0

Views: 3949

Answers (3)

Amit Goswami
Amit Goswami

Reputation: 364

here is an another way to achieve that kind of you need to set drawable left and you need to listen to it's click event through edittext.setOnTouchListener after that follow this answer https://stackoverflow.com/a/3685867/10561246

Upvotes: 0

hamid
hamid

Reputation: 191

hi you can replace this code and enjoy him !

edtPassword -> your edit text (Password)

show_password -> imageView For show your password

public void setPassword_show() {
    if (!edtPassword.getText().toString().equals("")) {
        if (show_password.getTag().toString().equals(IS_ONE_CLICK)) {
            edtPassword.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
            if (Build.VERSION.SDK_INT >= 21)
                show_password.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_visibility_black_24dp));
            else
                show_password.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.ic_visibility_black_24dp,
                        null));
            show_password.setTag((Object) IS_TWO_CLICK);
        } else if (show_password.getTag().toString().equals(IS_TWO_CLICK)) {
            edtPassword.setInputType(129);
            if (Build.VERSION.SDK_INT >= 21)
                show_password.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_visibility_off_black_24dp));
            else
                show_password.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.ic_visibility_off_black_24dp, null));
            show_password.setTag((Object) IS_ONE_CLICK);
        }
    }
}

Upvotes: 0

Saurabh Bhandari
Saurabh Bhandari

Reputation: 2458

Add app:passwordToggleEnabled="true" in your EditText inside TextInputLayout. it will toggle password visibility and vice versa. remove your java code

Upvotes: 2

Related Questions