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 W3Make Forum 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 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.

Sign InSign Up

Forum By W3make

Forum By W3make Logo Forum By W3make Logo

Forum By W3make Navigation

  • Home
  • About Us
  • Blog
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Questions Feed
  • Blog
  • Contact Us
Home/ Questions/Q 6349
Next
bhumitboraniya
  • 5
bhumitboraniyaBegginer
Asked: June 20, 20232023-06-20T13:02:06+05:30 2023-06-20T13:02:06+05:30In: Android Development

how to set onClickListener for Drawable Right of an EditText

  • 5

I have two events on EditText:

  1. Enter Password in EditText
  2. Eye Button in Drawable Right for Hide or Unhide The Password Text
androidandroid developmentandroid studioedittextjavaxml
  • 0 0 Answers
  • 437 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

0 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Aryan Atel Begginer
    2023-06-20T15:44:25+05:30Added an answer on June 20, 2023 at 3:44 pm

    The first thing you have to do is give the drawable an ID and get its reference in its respective java file, and after that, you have to setOnClickListner on it and implement two methods.

    drawableID.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
            yourEditText.setTransformationMethod(null);
    // To show password
    } });
    
    And In Xml ofEditText set android:inputType="numberPassword"
    
    
    

     

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Mohammed aleem hasan
    2023-06-23T16:35:54+05:30Added an answer on June 23, 2023 at 4:35 pm

     

    To set an OnClickListener for the drawable on the right side of an EditText, you can follow these steps:

    1. In your XML layout file, define the EditText and set the drawableRight attribute to the drawable you want to have clickable:

    xmlCopy code
    <EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:drawableRight="@drawable/my_drawable" />

    1. In your Java or Kotlin code, find the EditText view and set the OnClickListener on its drawableRight:

    javaCopy code
    EditText editText = findViewById(R.id.editText);
    editText.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
    final int DRAWABLE_RIGHT = 2;
    if (event.getAction() == MotionEvent.ACTION_UP) {
    if (event.getRawX() >= (editText.getRight() - editText.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
    // Click event on drawableRight
    // Perform your desired action here
    return true; // Consumes the touch event
    }
    }
    return false;
    }
    });

    Note: The code above uses an OnTouchListener to detect the touch event on the drawable. It checks if the touch event occurred within the bounds of the drawable on the right side (drawableRight). If it did, you can perform the desired action. Returning true from the OnTouchListener indicates that the touch event has been consumed and should not be propagated further.

    Remember to replace @drawable/my_drawable with the actual drawable resource you want to use.

    If you’re using Kotlin, the code will be similar, but with a few syntax differences. Here’s the Kotlin equivalent:

    kotlinCopy code
    val editText = findViewById<EditText>(R.id.editText)
    editText.setOnTouchListener { v, event ->
    val DRAWABLE_RIGHT = 2
    if (event.action == MotionEvent.ACTION_UP) {
    if (event.rawX >= (editText.right - editText.compoundDrawables[DRAWABLE_RIGHT].bounds.width())) {
    // Click event on drawableRight
    // Perform your desired action here
    return@setOnTouchListener true // Consumes the touch event
    }
    }
    false
    }

    By implementing the OnClickListener for the drawable right of the EditText, you can respond to clicks specifically on that drawable and execute your desired code accordingly.

     

    In your XML layout file, define the EditText and set the drawableRight attribute to the drawable you want to have clickable:

    xml

    Copy code

    <EditText

    android:id=”@+id/editText”

    android:layout_width=”match_parent”

    android:layout_height=”wrap_content”

    android:drawableRight=”@drawable/my_drawable” />

    In your Java or Kotlin code, find the EditText view and set the OnClickListener on its drawableRight:

    java

    Copy code

    EditText editText = findViewById(R.id.editText);

    editText.setOnTouchListener(new View.OnTouchListener() {

    @Override

    public boolean onTouch(View v, MotionEvent event) {

    final int DRAWABLE_RIGHT = 2;

    if (event.getAction() == MotionEvent.ACTION_UP) {

    if (event.getRawX() >= (editText.getRight() – editText.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {

    // Click event on drawableRight

    // Perform your desired action here

    return true; // Consumes the touch event

    }

    }

    return false;

    }

    });

    Note: The code above uses an OnTouchListener to detect the touch event on the drawable. It checks if the touch event occurred within the bounds of the drawable on the right side (drawableRight). If it did, you can perform the desired action. Returning true from the OnTouchListener indicates that the touch event has been consumed and should not be propagated further.

     

    Remember to replace @drawable/my_drawable with the actual drawable resource you want to use.

     

    If you’re using Kotlin, the code will be similar, but with a few syntax differences. Here’s the Kotlin equivalent:

     

    kotlin

    Copy code

    val editText = findViewById<EditText>(R.id.editText)

    editText.setOnTouchListener { v, event ->

    val DRAWABLE_RIGHT = 2

    if (event.action == MotionEvent.ACTION_UP) {

    if (event.rawX >= (editText.right – editText.compoundDrawables[DRAWABLE_RIGHT].bounds.width())) {

    // Click event on drawableRight

    // Perform your desired action here

    return@setOnTouchListener true // Consumes the touch event

    }

    }

    false

    }

    By implementing the OnClickListener for the drawable right of the EditText, you can respond to clicks specifically on that drawable and execute your desired code accordingly.

     

     

     

     

     

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 3k
  • Answers 2k
  • Best Answers 34
  • Users 5k
  • Popular
  • Answers
  • Mohammed aleem hasan

    What programming language is used for Android development?

    • 65 Answers
  • Manichandana

    Is this mandatory to write css code in making a ...

    • 58 Answers
  • vishakha_1713

    How to decide whether to use DELETE statement or DROP ...

    • 46 Answers
  • Jova0731
    Jova0731 added an answer Before I logged in I checked out Big Ass Sex… October 8, 2025 at 11:41 am
  • Jova0731
    Jova0731 added an answer If you're new to buying a custom sex doll, you… July 31, 2025 at 8:41 am
  • Jova0731
    Jova0731 added an answer Some of us with dark hearts want to design other… July 5, 2025 at 1:56 pm

Related Questions

  • LushKite

    How to Make Ganganzelli Trulala in Steal A Brainrot

    • 0 Answers
  • jean8

    u4gm Diablo 4 Season 10 Rogue Build for Efficient Farming

    • 0 Answers
  • Anonymous

    The Art of Escalation: How to Survive and Thrive in ...

    • 0 Answers

Top Members

saningh

saningh

  • 5 Questions
  • 116 Points
Pundit
Vishnu M

Vishnu M

  • 2 Questions
  • 96 Points
Teacher
Divya Sharma

Divya Sharma

  • 4 Questions
  • 68 Points
Teacher

Trending Tags

#css #html #questions 3d printing ai android android development android studio answer api app development c++ coding data Database developer development error flutter hacking help ios java javascript kotlin machine learning ml performance php plugin plugins poll programming python question security seo social media sql technology theme web web development website WordPress word press wordpress development wordpressdevelopment wordpress error wordpress errors

Explore

  • Recent Questions
  • Most Answered
  • Answers
  • Most Visited
  • Most Voted
  • No Answers
  • Feed
  • Favorites Questions

Footer

W3make forum is a social questions & Answers platform which will help you establis your community and connect with other people.

Legal Stuff

  • Privacy Policy
  • Terms and Conditions

Help

  • Questions Feed
  • Blog
  • Contact Us

Follow

© 2023 W3make.com | All Rights Reserved.

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.