1

I have an EditText in which I'm setting drawables to the right of the EditText. I'm switching those drawables with different scenarios. I have clearText and refreshIcon as my drawables. These both are changing correctly but I'm not able to get separate events for both of my drawables. Here is what I'm doing for clearing text from the EditText:

String value = "";      

    final Drawable x = getResources().getDrawable(R.drawable.clear_text);
    x.setBounds(0, 0, x.getIntrinsicWidth(), x.getIntrinsicHeight());
    getUrl.setCompoundDrawables(null, null, value.equals("") ? null : x,
            null);
    getUrl.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (getUrl.getCompoundDrawables()[2] == null) {
                return false;
            }
            if (event.getAction() != MotionEvent.ACTION_UP) {
                return false;
            }
            if (event.getX() > getUrl.getWidth() - getUrl.getPaddingRight()
                    - x.getIntrinsicWidth()) {
                getUrl.setText("");
                getUrl.setCompoundDrawables(null, null, null, null);
            }
            return false;
        }
    });

When I click clearText drawable it clears the text. It was nice and easy.

Now, I have another drawable i.e. refreshIcon, when on EditText refreshIcon is showing up it is not getting particular event for refresh purpose.

I have used the same code as of clearText for refreshIcon. I'm not able to print the Log when refreshIcon is clicked/touched.

What am I doing worong? Any kind of help will be appreciated.

Anupam
  • 3,742
  • 18
  • 55
  • 87

1 Answers1

0

You can combine 2 drawables into LevelListDrawable. Then use setLevel function to switch between them. You always can find out which layer (drawable) is visible via getLevel method and do appropriate action.

Leonidos
  • 10,482
  • 2
  • 28
  • 37
  • Can you please explain me how. I have not heard about layerDrawable before. If you can explain me using code that will be really helpful. – Anupam Jul 22 '13 at 07:22
  • I've a mistake ) You need [LevelListDrawable](http://developer.android.com/guide/topics/resources/drawable-resource.html#LevelList). – Leonidos Jul 22 '13 at 07:27
  • I think `LayerDrawable` will not work for me. See this statement `A Drawable that manages an array of other Drawables. These are drawn in array order, so the element with the largest index is be drawn on top`. Largest index number, will have to change a lot for this. Can't we use the same method for two different `drawables`. – Anupam Jul 22 '13 at 07:29
  • LevelListDrawable is what you need ) It allows to switch between drawables. Try it ) – Leonidos Jul 22 '13 at 08:01