codebreaker / RightDrawableOnTouchListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
public abstract class RightDrawableOnTouchListener implements OnTouchListener < |
Drawable drawable; |
private int fuzz = 10 ; |
/** |
* @param keyword |
*/ |
public RightDrawableOnTouchListener ( TextView view ) < |
super (); |
final Drawable [] drawables = view . getCompoundDrawables(); |
if (drawables != null && drawables . length == 4 ) |
this . drawable = drawables[ 2 ]; |
> |
/* |
* (non-Javadoc) |
* |
* @see android.view.View.OnTouchListener#onTouch(android.view.View, android.view.MotionEvent) |
*/ |
@Override |
public boolean onTouch ( final View v , final MotionEvent event ) < |
if (event . getAction() == MotionEvent . ACTION_DOWN && drawable != null ) < |
final int x = ( int ) event . getX(); |
final int y = ( int ) event . getY(); |
final Rect bounds = drawable . getBounds(); |
if (x >= (v . getRight() — bounds . width() — fuzz) && x (v . getRight() — v . getPaddingRight() + fuzz) |
&& y >= (v . getPaddingTop() — fuzz) && y (v . getHeight() — v . getPaddingBottom()) + fuzz) < |
return onDrawableTouch(event); |
> |
> |
return false ; |
> |
public abstract boolean onDrawableTouch ( final MotionEvent event ); |
> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
this . keyword = ( EditText ) findViewById( R . id . search); |
this . keyword . setOnTouchListener( new RightDrawableOnTouchListener (keyword) < |
@Override |
public boolean onDrawableTouch ( final MotionEvent event ) < |
return onClickSearch(keyword); |
> |
>); |
private boolean onClickSearch( final View view) < |
// do something |
event . setAction( MotionEvent . ACTION_CANCEL ); |
return false ; |
> |
This comment has been minimized.
Copy link Quote reply
rcgroot commented Feb 24, 2014
This code confuses the coordinate system of the screen (getRight() usage) with those of the View getX() and getY().
Don’t use it without fixing the bugs in the code.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Источник
Android edittext drawable clicklistener
EditDrawableText — An EditText which makes your Drawable Clickable
Preview of EditDrawableText
Overview of EditDrawableText library
- EditDrawableText can be used to Show/Hide Password
- Left/Right Drawables can be clicked to make custom events like Request OTP etc.
- All type of EditText Properties are possible in EditDrawableText
Using EditDrawableText Library in your Android application
- Add it in your root build.gradle at the end of repositories:
- Add this in your app’s build.gradle
- Make the drawable clickable in Activity file,
- You can also add an option so that the drawable is hidden by default and only shows when there is some text available in EditDrawableText
When the value is false, then the drawable is hidden by default and vice versa
If this library helps you in anyway, show your love ❤️ by putting a ⭐ on this project ✌️
Источник