How to create EditText tooltip as text with image in android

How to create EditText tooltip / placeholder with text and image in android. I write like that

<EditText android:id="@+id/name" android:layout_height="wrap_content" android:layout_width="wrap_content" android:lines="1" ................................ ............................... android:drwableLeft="@drawable/image1" android:hint="enter name here"/> 

Here we can see the image and text (tooltip) inside the edittext, but the image is still visible even after the user enters something in this field. My requirement is that there is no text with an image in the text of the EditText, if the EditText is not empty, hide the text and the help image

+6
source share
2 answers

What you can do is set drawableLeft as it was in your xml and add addTextChangedListener to your EditText. When typing, this listener will be warned, and you can check the listener if the text is greater than 0. If so, just set the drawableLeft , assigned in xml programmatically, to null .

 @Override public void afterTextChanged(Editable s) { if(s.toString().length() > 0) { editText.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); } else { //Assign your image again to the view, otherwise it will always be gone even if the text is 0 again. editText.setCompoundDrawablesWithIntrinsicBounds(R.drawable.image, 0, 0, 0); } } 
+9
source

I did this by placing my EditText inside FrameLayout and then also including the TextView inside FrameLayout. TextView serves as tooltip text. I have a drawableStart attribute in a TextView. When the user starts typing, the TextView hides using code similar to the one adopted by Dion.

0
source

Source: https://habr.com/ru/post/958943/


All Articles