3

I have EditText and icon inside it

<EditText
    android:id="@+id/myedittext"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:drawableRight="@mipmap/microphone"/>

I Set onClickListener for the Drawable right of an EditText

myeditText.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            final int DRAWABLE_LEFT = 0;
            final int DRAWABLE_TOP = 1;
            final int DRAWABLE_RIGHT = 2;
            final int DRAWABLE_BOTTOM = 3;

            if(event.getAction() == MotionEvent.ACTION_UP) {
                if(event.getRawX() >= (myeditText.getRight() - myeditText
      .getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {  
                    // your action here
                    Toast.makeText(getApplicationContext(),
                            "speak",Toast.LENGTH_SHORT).show();
                    return true;
                }
            }
            return false;
        }
    });

when I click to icon right of EditText Toast show me and work, but show me paste option on EditText too. how can I remove paste when icon in right clicked?

Cœur
  • 37,241
  • 25
  • 195
  • 267

4 Answers4

2

Well, it's already about 3 years. However, for someone like me that might face this challenge: The solution is to also return true in ACTION_DOWN but only call the listener on ACTION_DOWN, returning true as well. In this case, you first check the region clicked corresponds with that of the icon, before proceeding to check action up or down Something like:

@Override
public boolean onTouch(View v, MotionEvent event) {
        final int DRAWABLE_LEFT = 0;
        final int DRAWABLE_TOP = 1;
        final int DRAWABLE_RIGHT = 2;
        final int DRAWABLE_BOTTOM = 3;


        if (event.getRawX() >= (myeditText.getRight() - myeditText
                .getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                // your action here
                listener.onClick(v);
                return true;
            }

            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                return true;
            }
        }

        return false;
    }
odifek
  • 180
  • 13
1

In my case changing from ACTION_UP to ACTION_DOWN gave effect. Of course, behaviour sligtly changed. And cursor still jumps to the end of a text.

CoolMind
  • 26,736
  • 15
  • 188
  • 224
1

Just set android:longClickable="false" to editbox.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Dayanand Waghmare
  • 2,979
  • 1
  • 22
  • 27
0

You can take a look at this question and see how to disable the paste.

But I'd suggest something else, instead of disabling paste, you can probably do this in a different way. Looking at whatsapp's microphone button + EditText placement, I notice that they're two different elements, and this is what you may do. Wrap your EditText in a horizontal LinearLayout and place a button with a microphone at the right end. I hope you can manage to make it work, and that you understand my idea.

Community
  • 1
  • 1
Vucko
  • 7,371
  • 2
  • 27
  • 45