Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8772707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:06:02+00:00 2026-06-13T18:06:02+00:00

In my app I have a EditText with a search Icon on the right

  • 0

In my app I have a EditText with a search Icon on the right side. I used the code given below.

 <EditText
        android:id="@+id/search"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="4dip"
        android:layout_weight="1"
        android:background="@drawable/textfield_search1"
        android:drawableLeft="@drawable/logo"
        android:drawableRight="@drawable/search_icon"
        android:hint="Search Anything..."
        android:padding="4dip"
        android:singleLine="true" />

I want to set the onClickListener for the search icon image assigned to the right drawable
of EditText. How is it possible?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:06:03+00:00Added an answer on June 13, 2026 at 6:06 pm
    public class CustomEditText extends androidx.appcompat.widget.AppCompatEditText {
    
        private Drawable drawableRight;
        private Drawable drawableLeft;
        private Drawable drawableTop;
        private Drawable drawableBottom;
    
        int actionX, actionY;
    
        private DrawableClickListener clickListener;
    
        public CustomEditText (Context context, AttributeSet attrs) {
            super(context, attrs);
            // this Contructure required when you are using this view in xml
        }
    
        public CustomEditText(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);        
        }
    
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
    
        }
    
        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
        }
    
        @Override
        public void setCompoundDrawables(Drawable left, Drawable top,
                Drawable right, Drawable bottom) {
            if (left != null) {
                drawableLeft = left;
            }
            if (right != null) {
                drawableRight = right;
            }
            if (top != null) {
                drawableTop = top;
            }
            if (bottom != null) {
                drawableBottom = bottom;
            }
            super.setCompoundDrawables(left, top, right, bottom);
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            Rect bounds;
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                actionX = (int) event.getX();
                actionY = (int) event.getY();
                if (drawableBottom != null
                        && drawableBottom.getBounds().contains(actionX, actionY)) {
                    clickListener.onClick(DrawablePosition.BOTTOM);
                    return super.onTouchEvent(event);
                }
    
                if (drawableTop != null
                        && drawableTop.getBounds().contains(actionX, actionY)) {
                    clickListener.onClick(DrawablePosition.TOP);
                    return super.onTouchEvent(event);
                }
    
                // this works for left since container shares 0,0 origin with bounds
                if (drawableLeft != null) {
                    bounds = null;
                    bounds = drawableLeft.getBounds();
    
                    int x, y;
                    int extraTapArea = (int) (13 * getResources().getDisplayMetrics().density  + 0.5);
    
                    x = actionX;
                    y = actionY;
    
                    if (!bounds.contains(actionX, actionY)) {
                        /** Gives the +20 area for tapping. */
                        x = (int) (actionX - extraTapArea);
                        y = (int) (actionY - extraTapArea);
    
                        if (x <= 0)
                            x = actionX;
                        if (y <= 0)
                            y = actionY;
    
                        /** Creates square from the smallest value */
                        if (x < y) {
                            y = x;
                        }
                    }
    
                    if (bounds.contains(x, y) && clickListener != null) {
                        clickListener
                                .onClick(DrawableClickListener.DrawablePosition.LEFT);
                        event.setAction(MotionEvent.ACTION_CANCEL);
                        return false;
    
                    }
                }
    
                if (drawableRight != null) {
    
                    bounds = null;
                    bounds = drawableRight.getBounds();
    
                    int x, y;
                    int extraTapArea = 13;
    
                    /**
                     * IF USER CLICKS JUST OUT SIDE THE RECTANGLE OF THE DRAWABLE
                     * THAN ADD X AND SUBTRACT THE Y WITH SOME VALUE SO THAT AFTER
                     * CALCULATING X AND Y CO-ORDINATE LIES INTO THE DRAWBABLE
                     * BOUND. - this process help to increase the tappable area of
                     * the rectangle.
                     */
                    x = (int) (actionX + extraTapArea);
                    y = (int) (actionY - extraTapArea);
    
                    /**Since this is right drawable subtract the value of x from the width 
                    * of view. so that width - tappedarea will result in x co-ordinate in drawable bound. 
                    */
                    x = getWidth() - x;
                    
                     /*x can be negative if user taps at x co-ordinate just near the width.
                     * e.g views width = 300 and user taps 290. Then as per previous calculation
                     * 290 + 13 = 303. So subtract X from getWidth() will result in negative value.
                     * So to avoid this add the value previous added when x goes negative.
                     */
                     
                    if(x <= 0){
                        x += extraTapArea;
                    }
                    
                     /* If result after calculating for extra tappable area is negative.
                     * assign the original value so that after subtracting
                     * extratapping area value doesn't go into negative value.
                     */               
                     
                    if (y <= 0)
                        y = actionY;                
    
                    /**If drawble bounds contains the x and y points then move ahead.*/
                    if (bounds.contains(x, y) && clickListener != null) {
                        clickListener
                                .onClick(DrawableClickListener.DrawablePosition.RIGHT);
                        event.setAction(MotionEvent.ACTION_CANCEL);
                        return false;
                    }
                    return super.onTouchEvent(event);
                }           
    
            }
            return super.onTouchEvent(event);
        }
    
        @Override
        protected void finalize() throws Throwable {
            drawableRight = null;
            drawableBottom = null;
            drawableLeft = null;
            drawableTop = null;
            super.finalize();
        }
    
        public void setDrawableClickListener(DrawableClickListener listener) {
            this.clickListener = listener;
        }
    
    }
    

    Also Create an Interface with

    public interface DrawableClickListener {
    
        public static enum DrawablePosition { TOP, BOTTOM, LEFT, RIGHT };
        public void onClick(DrawablePosition target); 
        }
    

    Still if u need any help, comment

    Also set the drawableClickListener on the view in activity file.

    editText.setDrawableClickListener(new DrawableClickListener() {
            
             
            public void onClick(DrawablePosition target) {
                switch (target) {
                case LEFT:
                    //Do something here
                    break;
    
                default:
                    break;
                }
            }
            
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developping an android app , and i have an EditText and a two
I have a simple EditText over a ListView defined below. When the app runs,
I have an EditText view in my Android app. I need inner links in
I am writing a simple android app which have a edittext and a button.
I am working on a android app and I have an EditText where user
In my android app I have a login screen. A graphic designer has given
well the calculation in my app goes like this.. have a edittext box where
I have a Button/Edittext to reset my App-Settings.... If a press the button(no long
I have the following app that has a textview and an edittext. When the
In my app i have an editText . If i click on that then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.