0

When the EditText line is blank (the default), I'd like the ImageView icon color to be white.

When the icon is pressed, I'd like the color to be black and then revert back to white because the press clears the EditText line, so it would then again be empty.

With the ImageView, I first set src equal to a white drawable. Any thoughts on how to switch the color state based on the length test of the EditText line?

AJW
  • 1,578
  • 3
  • 36
  • 77
  • for edittext you need to implement textwatcher to listen to the textchanges. http://stackoverflow.com/questions/8699569/implementing-text-watcher-for-edittext – karan Oct 28 '15 at 04:22
  • so add code after onTextChanged that will change the setBackgroundColor? – AJW Oct 28 '15 at 04:30
  • better do it in `afterTextChanged` see the 2nd answer in link provided. not the accepted answer. and to change color on button click, you just need to set selector. so that when its state changes, relative drawable will be selected by itself. see this link http://stackoverflow.com/questions/14023886/android-button-selector – karan Oct 28 '15 at 04:35
  • ok thanks I'll take a look. – AJW Oct 28 '15 at 04:38

2 Answers2

0

I'm not too exactly sure what the question is asking for, I got confused when you brought in ImageView and hover because I don't believe phones support the onHoverListener as well.. Do you mean changing the EditText accent color when you click on it to type something? If so it will be under the colors.xml-->colorAccent

Also if you mean to change an images background color

 public void onClick(View v) {
                imageView.setBackgroundColor(Color.parseColor(EditText.getText().toString()));
        }

Hopefully I provided you with some help to continue your project.

  • Sorry for confusion. I guess for hover I was thinking of mouse cursor since I've been developing with an emulator. I will edit my answer to remove hover reference. For the color change white to black and then back to white, should I be removing the "android:src="drawable/..." line from my xml layout file and then setBackgroundColor as you suggest programmatically in the Activity file? – AJW Oct 28 '15 at 04:10
  • What I am imagining is that you have an ImageView and a EditText, if you have an emty EditText the ImageView would be white. When the EditText is clicked on the ImageView would change to black? – Ram Dettmer Oct 28 '15 at 04:36
  • Almost. EditText line starts out empty and icon is white. When the user inputs data onto the EditText line then the line is not longer blank, so I'd like the ImageView to change to the color black. – AJW Oct 28 '15 at 04:40
0

To update icon if textview empty, check Android: How can I validate EditText input? with addTextChangedListener() then

 public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                Log.i("Text", charSequence + "");
                if(charSequence.length()==0)
                    ib.setImageResource(R.drawable.button_unpressed);
                else
                    ib.setImageResource(R.drawable.button_pressed);
            }

and to change imagebutton background follow its state How to set image button backgroundimage for different state? with "create an xml in your drawable"

If you want to keep color of icon remain black after pressed, just override onClick() by

ib.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ib.setImageResource(R.drawable.button_pressed);
            }
        });
Community
  • 1
  • 1
Trung NT Nguyen
  • 403
  • 2
  • 14
  • Would those answers apply to an ImageView as well as the button you suggest? – AJW Oct 28 '15 at 04:31
  • No for ImageView. ImageView and ImageButton just difference by onClick action basically. You should use ImageButton/Button in this case – Trung NT Nguyen Oct 28 '15 at 04:44
  • Ok I will research the onClick difference but why are you recommending using an ImageButton rather than an ImageView? – AJW Oct 28 '15 at 04:54
  • Because of "When the icon is pressed"...ImageButton supported it and you could customize its drawable via xml also – Trung NT Nguyen Oct 28 '15 at 04:55