0

I created alert dialog with TextInputLayout programmatically in fragment. I need to hide password by default, TextInputLayout has toggle button and it is working as expected hide/show on click. I tried to keep password hidden by default by settingsingleline to true and other hack i got from other SO answers but i still not able to get desired result. Any idea why i am not getting desired result?

TextInputLayout passwordParent = new TextInputLayout(getActivity());
        passwordParent = new TextInputLayout(getActivity());

        TextInputEditText passWord= new TextInputEditText(getActivity());
        passWord.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
        passWord.setBackgroundResource(R.drawable.background);
        passWord.setBackgroundColor(getResources().getColor(R.color.white));

        passwordParent.setEndIconMode(TextInputLayout.END_ICON_PASSWORD_TOGGLE);
        passwordParent.setEndIconDrawable(R.drawable.show_password_selector);
        passwordParent.addView(passWord);

Background XML Selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_remove_red_eye_black_24dp" android:state_checked="true"/> // Visible Eye Icon 
<item android:drawable="@drawable/ic_visibility_off_black_24dp"/> // Hide Eye Icon
</selector>

enter image description here

Edit: It's working by adding InputType.TYPE_CLASS_TEXT into input type like this passWord.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT); but the icon still stay the same, its still showing wrong eye icon.

2
  • did you try to set password transformation as transformation method? Commented Aug 3, 2022 at 23:47
  • @PeerNet i tried but still will can try again Commented Aug 4, 2022 at 1:23

1 Answer 1

0

Try moving addView(passWord) up by two lines, like this :

passwordParent.addView(passWord);
passwordParent.setEndIconMode(TextInputLayout.END_ICON_PASSWORD_TOGGLE);
passwordParent.setEndIconDrawable(R.drawable.show_password_selector);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.