Listen for android:drawableLeft and android:drawableRight click events

Listen for android:drawableLeft and android:drawableRight click events


Android officially does not provide listeners for android:drawableLeft and android:drawableRight click events, but in some cases, such as the search bar below,


The search event needs to be triggered after the user enters a character and clicks the search icon on the left, and this search icon is added through android:drawableLeft. At this time, it is necessary to monitor the event of the icon on the android:drawableLeft. I wrote a tool DrawableUtil class that listens to android:drawableLeft and android:drawableRight.

import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

public class DrawableUtil {

    /**
     * The serial number of the drawable around the TextView.
     * 0 left,  1 top, 2 right, 3 bottom
     */
    private final int LEFT = 0;
    private final int RIGHT = 2;

    private OnDrawableListener listener;
    private TextView mTextView;

    public DrawableUtil(TextView textView, OnDrawableListener l) {
        mTextView = textView;
        mTextView.setOnTouchListener(mOnTouchListener);
        listener = l;
    }

    public interface OnDrawableListener {
        public void onLeft(View v, Drawable left);

        public void onRight(View v, Drawable right);
    }

    private View.OnTouchListener mOnTouchListener = new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_UP:
                    if (listener != null) {
                        Drawable drawableLeft = mTextView.getCompoundDrawables()[LEFT];
                        if (drawableLeft != null && event.getRawX() <= (mTextView.getLeft() + drawableLeft.getBounds().width())) {
                            listener.onLeft(v, drawableLeft);
                            return true;
                        }

                        Drawable drawableRight = mTextView.getCompoundDrawables()[RIGHT];
                        if (drawableRight != null && event.getRawX() >= (mTextView.getRight() - drawableRight.getBounds().width())) {
                            listener.onRight(v, drawableRight);
                            return true;
                        }
                    }

                    break;
            }

            return false;
        }

    };
}



Use DrawableUtil to complete the monitoring by passing an OnDrawableListener during construction, such as:
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class DrawableActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView(R.layout.drawable_activity);
        TextView textView = findViewById(R.id.text);

        DrawableUtil drawableUtil = new DrawableUtil(textView, new DrawableUtil.OnDrawableListener() {
            @Override
            public void onLeft(View v, Drawable left) {
                Toast.makeText(getApplicationContext(), "left", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onRight(View v, Drawable right) {
                Toast.makeText(getApplicationContext(), "right", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Note that the event property to be added to the TextView in the xml layout is true:

android:clickable="true"

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325389017&siteId=291194637