1

so I have editText with closes and success icons and maximum password 6, the first thing I do is if the password is not maximum 6, the icon remains closes like I drew, if the password is maximum 6 then the closes icon changes to the success icon, my problem how to change the closes icon to a success icon if the maximum 6 conditions are met

editTest image

this is my code

 <com.google.android.material.textfield.TextInputLayout
    android:id="@+id/textInputPassword"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="20dp"
    android:layout_marginTop="10dp"
    android:layout_marginEnd="20dp"
    android:hint="Password"
    app:counterEnabled="true"
    app:counterMaxLength="6"
    app:endIconMode="clear_text"
    app:endIconDrawable="@drawable/ic_canceles"
    app:errorEnabled="true"
    app:endIconTint="#DF0000"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textInputEmail">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/editTextPassword"
        android:layout_width="match_parent"
        android:maxLines="1"
        android:layout_height="wrap_content" />

</com.google.android.material.textfield.TextInputLayout>
hashtag
  • 83
  • 2
  • 8

2 Answers2

1

Try this in your on create method

TextInputLayout editText = findViewById(R.id.textInputPassword);


                editText.getEditText().addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                        if (s.toString().length() > 6) {
                            editText.setEndIconDrawable(R.drawable.success);
                        } else {
                            editText.setEndIconDrawable(R.drawable.ic_canceles);
                        }
                    }

                    @Override
                    public void afterTextChanged(Editable s) {

                    }
                });
NattoShaw
  • 38
  • 8
  • i will try it @NattoShaw – hashtag Apr 02 '20 at 20:09
  • error code in kotlin, Unresolved reference: activity_sign_in_with_email -> @NattoShaw – hashtag Apr 02 '20 at 20:32
  • Could I possibly see a screenshot please? – NattoShaw Apr 02 '20 at 21:03
  • You need to put the code outside the submit button click listener – NattoShaw Apr 02 '20 at 21:43
  • ok i have done it and if it is updated then hide icon before retyping, i include the video -> https://ibb.co/vwDG3hx @NattoShaw – hashtag Apr 02 '20 at 21:51
  • i use -> setErrorIconDrawable – hashtag Apr 02 '20 at 21:53
  • Type textinputPassword.setErrorEnabled(true); in the on text change method – NattoShaw Apr 02 '20 at 22:16
  • it can't be the same, if you press again on the textview it will hiden and tapil if typed, like the view that I uploaded @NattoShaw – hashtag Apr 02 '20 at 22:20
  • Ok I think I know what the problem is, try doing this instead, In your on text changed type in: ``` if (s.toString() > 6){ textInputPassword.endIconMode = TextInputLayout.END_ICON_CUSTOM; textInputPassword.endIconDrawable = ContextCompat.getDrawable(context, R.drawable.ic_success); } else { textInputPassword.endIconMode = TextInputLayout.END_ICON_CUSTOM; textInputPassword.endIconDrawable = ContextCompat.getDrawable(context, R.drawable.ic_canceless); } ``` – NattoShaw Apr 02 '20 at 22:39
  • ok, better than before, thank you, for helping me, may I ask one more time and can you help me? @NattoShaw – hashtag Apr 02 '20 at 23:37
  • I want to make validation to display regular expression characters, if it doesn't add the regex icon it still fails, when added it will be a success, how to make it @NattoShaw – hashtag Apr 03 '20 at 00:00
  • Try creating a method returning boolean and has a string paramter, check for its length and check for characters like so: public boolean isValid(String s) { Pattern regex = Pattern.compile("[$&+,:;=\\\\?@#|/'<>.^*()%!-]"); if (regex.matcher(s).find() && s.length() > 6) { return true; } return false; } Then on the text changed type in if (isValid(s.toString())) { editText.setEndIconDrawable(R.drawable.success); } else { editText.setEndIconDrawable(R.drawable.ic_canceles); } – NattoShaw Apr 03 '20 at 06:30
  • The method isValid should be out side like the onCreate method – NattoShaw Apr 03 '20 at 07:08
  • The code should be:fun isValid(s: String) : Boolean { val regex = Pattern.compile("[$&+,:;=\\\\?@#|/'<>.^*()%!-]']") if (regex.matcher(s).find() && s.length > 6) { return true } else { return false } } – NattoShaw Apr 03 '20 at 15:06
  • Then in your onTextchanged method type in: if (isValid(s.toString())) { textInputPassword.setEndIconDrawable(R.drawable.ic_success) } else { textInputPassword.setEndIconDrawable(R.drawable.ic_canceles) } – NattoShaw Apr 03 '20 at 15:06
  • I've made it and it works, but I gave a limit of 6, if passing from 6, the success icon still appears and I want to make it if more than 6 then display the closes icon, I've tried it with a statement like this does not work, I have a problem other than using regex but if I enter code other than uppercase letters, lowercase letters and numbers will produce a successful icon, if I add _ * # $ this will produce a successful icon, this is code me -> https://pastebin.com/mNAxyShY – hashtag Apr 03 '20 at 15:16
  • You're not putting it in the listener it should be exactly like this ->https://pastebin.com/FR6CXQVs – NattoShaw Apr 03 '20 at 17:09
0

You can do this :

         textInputPassword.seticon(R.drawable.image);
Shimaa Yasser
  • 587
  • 4
  • 12
  • I put it on the statement to change the icon, I want without I press the button, the icon automatically changes @Shimaa Yasser – hashtag Apr 02 '20 at 20:34