3

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.

Kuba Stonka
  • 117
  • 2
  • 9

2 Answers2

2

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.

Community
  • 1
  • 1
Goltsev Eugene
  • 3,325
  • 6
  • 25
  • 48
  • Almost done. Now i have problem with argument "this" in this code textArea.setCompoundDrawables(null, null, ContextCompat.getDrawable(this,R.drawable.ic_camera_alt_black_18dp), null); : Wrong 1st argument type. Found: 'android.text.TextWatcher', required: 'android.content.Context' – Kuba Stonka May 16 '17 at 19:42
  • If this code is inside your activity (`MainActivity`, for example), place `MainActivity.this` instead of `this`. If it's inside a fragment, put `getActivity()`. This is happening because your code now is inside `TextWatcher`, so `this` points to `TextWatcher` now. – Goltsev Eugene May 18 '17 at 04:19
0

For hide textArea's drabwable, you might try this:

Drawable[] drawables = textArea.getCompoundDrawables();
for (Drawable d: drawables) {
    if (d != null) {
        d.setAlpha(0);
    }
}
Haiyuan
  • 103
  • 2
  • 5