Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create EditText hint as Text with image in android

How to create EditText hint/placeholder with Text & image in android. I write like this

<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 image & text (hint) within edittext but image still visible even after user enters something in that field. My requirement is if there is no text in EditText show hint with image, if EditText is not empty hide hint text and image

like image 429
sandeepmaaram Avatar asked Nov 27 '13 18:11

sandeepmaaram


1 Answers

What you can do is set an drawableLeft as you did in your xml and add an addTextChangedListener to your EditText. When typing occurs this listener will be warned and you can check in the listener if the textsize is bigger than 0. If so, then just set the drawableLeft assigned in the 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);
    }
}
like image 179
Dion Segijn Avatar answered Nov 15 '22 06:11

Dion Segijn