1

I have an Icon that defaults to the color white, using an ImageView and drawable. I want the color to change to black when pressed. When an EditText line gains text (because the user enters some data on the line), I'd like the icon to change to the color gray. Currently the icon changes to black when the length test is >0 and nothing changes when the icon is pressed (because the color is already black). And when the length test is then made to == 0, the color stays black when it should revert back to the default color white. What am I missing here?

Activity file:

protected void OnCreate...
fEditText = (EditText) findViewById(R.id.FEditText); 
@Override
        public void afterTextChanged(Editable s) {
            fTextInputLayout.setHint("Due Date");
            if (fEditText.getText().length() > 0) {
               imgcan = (ImageView) findViewById(R.id.imageView1);
               imgcan.setImageResource(R.drawable.ic_delete_54per24);
               <!-- "ic_delete_54per24 is the the gray icon drawable -->
            }
        }

Xml Layout file:

...
<ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:onClick="onClickClearDate"
        android:clickable="true"
        android:src="@drawable/trash_can"  />

trash_can drawable file:

...
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true"
    android:drawable="@drawable/ic_delete_black_24dp" />
    <!-- black color when pressed -->
<item android:drawable="@drawable/ic_delete_white_24dp" />
    <!-- white color when normal (default) -->

AJW
  • 1,578
  • 3
  • 36
  • 77

2 Answers2

1

Maybe you missed to reset color image when text is empty,like this:

 if (fEditText.getText().length() > 0) {
       imgcan = (ImageView) findViewById(R.id.imageView1);
       imgcan.setImageResource(R.drawable.ic_delete_54per24);
  }else{ //add this
      imgcan = (ImageView) findViewById(R.id.imageView1);
      imgcan.setImageResource(R.drawabale.ic_delete_white_24dp);
  }
starkshang
  • 8,228
  • 6
  • 41
  • 52
  • Ok great. That gets the icon back to the default white when the EditText is empty. Now I just need to figure out why the gray icon does not appear. Any ideas? – AJW Oct 29 '15 at 03:22
  • afterTextChanged will be invoked when you stop edited a while.and to check why it won't work,please add log on afterTextChanged and if (fEditText.getText().length() > 0)'s condition is true. – starkshang Oct 29 '15 at 03:26
  • I changed the gray color to a much lighter version and confirmed all is working properly. Thank you, answer accepted and upvoted. Cheers. – AJW Oct 29 '15 at 03:35
1

Try Textwatcher ?

editText.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(TextUtils.isEmpty(s)){
        imageView.setImageResource(R.drawable.ic_delete_54per24);
    }

}

@Override
public void afterTextChanged(Editable s) {

}
});
Dhinakaran Thennarasu
  • 3,336
  • 4
  • 26
  • 39
  • Yes, I show above in my Actvity file: "public void afterTextChanged(Editable s)...". – AJW Oct 29 '15 at 03:59