Search code examples
androidandroid-edittextandroid-drawable

EditText, how to hide DrawableRight


I want to have DrawableRight in my text area, but when i start typing then it should disappear. So i have code for EditText and function that knows when to hide drawable, but i don't know how to call that function. Can you help me?

//XML CODE
<EditText
        android:id="@+id/textt"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="center_vertical"
        android:ems="10"
        android:paddingLeft="2dp"
        android:hint="Wpisz wiadomość"
        android:background="@android:color/transparent"
        android:maxLines="4" />

//JAVA CODE
public void camera(View v)
{
    EditText textArea=(EditText) findViewById(R.id.textt);
    if(textArea.getText()==null)
    {
        textArea.setCompoundDrawables(null, null, ContextCompat.getDrawable(this,R.drawable.ic_camera_alt_black_18dp), null);
    }
    else
        textArea.setCompoundDrawables(null,null,null,null);
}

SOLUTION: I handled with it! First of all i changed "this" to "MainActivity.this". Second was very important - set bounds for drawable! Done.


Solution

  • You should implement TextWatcher to your Edittext.
    Pls. see an example here.

    In onTextChanged you can check - if CharSequence s length is > 0, then you have some text entered and should hide drawable.
    CharSequence s is a param that you receive in onTextChanged method.