Issue
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) -->
Solution
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);
}
Answered By - starkshang
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.