0

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.

Sinbod User
  • 11
  • 1
  • 6

3 Answers3

2

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

Saurabh Bhandari
  • 2,438
  • 4
  • 26
  • 33
  • Yeah I had done it and it worked. But surprisingly I couldn't find a way to place the visibility toggle button in my desired position. (In my case, left side of my `EditText`) – Sinbod User Jan 02 '19 at 06:50
  • 1
    @SinbodUser: by default password toggle replaces end drawable, so you won't be able to move it to right, you will need to set drawable left and handle button click manually. – karan Jan 02 '19 at 06:59
  • @SinbodUser if you are using arabic texts then you can change layout direction into rtl – Saurabh Bhandari Jan 02 '19 at 07:01
0

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);
        }
    }
}
hamid
  • 171
  • 1
  • 1
  • 9
0

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

Amit Goswami
  • 314
  • 2
  • 11