Created
September 10, 2020 18:02
-
-
Save MasoudFallahpour/2959afd8b38b3844c25e90dce30d4e93 to your computer and use it in GitHub Desktop.
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
private fun setDrawablesListener() { | |
setOnTouchListener(OnTouchListener { view, event -> | |
view.performClick() | |
if (event.action == MotionEvent.ACTION_UP) { | |
when { | |
rightDrawableClicked(event) -> { | |
setText("") | |
return@OnTouchListener true | |
} | |
leftDrawableClicked(event) -> { | |
queryTextListener?.onQueryTextSubmit(text.toString()) | |
return@OnTouchListener true | |
} | |
else -> { | |
return@OnTouchListener false | |
} | |
} | |
} | |
false | |
}) | |
} | |
private fun rightDrawableClicked(event: MotionEvent): Boolean { | |
val rightDrawable = compoundDrawables[DRAWABLE_RIGHT_INDEX] | |
return if (rightDrawable == null) { | |
false | |
} else { | |
val startOfDrawable = width - rightDrawable.bounds.width() - paddingRight | |
val endOfDrawable = startOfDrawable + rightDrawable.bounds.width() | |
startOfDrawable <= event.x && event.x <= endOfDrawable | |
} | |
} | |
private fun leftDrawableClicked(event: MotionEvent): Boolean { | |
val leftDrawable = compoundDrawables[DRAWABLE_LEFT_INDEX] | |
return if (leftDrawable == null) { | |
false | |
} else { | |
val startOfDrawable = paddingLeft | |
val endOfDrawable = startOfDrawable + leftDrawable.bounds.width() | |
startOfDrawable <= event.x && event.x <= endOfDrawable | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment