@@ -6,22 +6,23 @@ import android.graphics.drawable.Drawable
66import android.util.AttributeSet
77import android.view.MotionEvent
88import android.widget.EditText
9+ import kotlin.math.abs
910
1011class EditDrawableText : EditText {
11-
12+
1213 private var drawableRight: Drawable ? = null
1314 private var drawableLeft: Drawable ? = null
1415 private var drawableTop: Drawable ? = null
1516 private var drawableBottom: Drawable ? = null
1617 private var positionX: Int = 0
1718 private var positionY: Int = 0
18-
19- private var onDrawableClickListener: onDrawableClickListener ? = null
20-
19+
20+ private var onDrawableClickListener: OnDrawableClickListener ? = null
21+
2122 constructor (context: Context , attrs: AttributeSet ) : super (context, attrs)
22-
23+
2324 constructor (context: Context , attrs: AttributeSet , defStyle: Int ) : super (context, attrs, defStyle)
24-
25+
2526 override fun setCompoundDrawables (leftDrawable : Drawable ? ,
2627 topDrawable : Drawable ? ,
2728 rightDrawable : Drawable ? ,
@@ -43,34 +44,34 @@ class EditDrawableText : EditText {
4344 // this works for left since container shares 0,0 origin with bounds
4445 if (drawableLeft != null ) {
4546 bounds = drawableLeft!! .bounds
46-
47+
4748 var xClickPosition: Int
4849 var yClickPosition: Int
4950 /*
5051 * @return pixels into dp
5152 */
5253 val extraClickArea = (13 * resources.displayMetrics.density + 0.5 ).toInt()
53-
54+
5455 xClickPosition = positionX
5556 yClickPosition = positionY
56-
57+
5758 if (! bounds!! .contains(positionX, positionY)) {
5859 /* * Gives some extra space for tapping. */
5960 xClickPosition = positionX - extraClickArea
6061 yClickPosition = positionY - extraClickArea
61-
62+
6263 if (xClickPosition <= 0 ) xClickPosition = positionX
6364 if (yClickPosition <= 0 ) yClickPosition = positionY
64-
65+
6566 /* * Creates square from the smallest value from x or y*/
6667 if (xClickPosition < yClickPosition) yClickPosition = xClickPosition
6768 }
68-
69+
6970 if (bounds.contains(xClickPosition, yClickPosition) && onDrawableClickListener != null ) {
7071 onDrawableClickListener!! .onClick(DrawablePosition .LEFT )
7172 event.action = MotionEvent .ACTION_CANCEL
7273 return false
73-
74+
7475 }
7576 }
7677
@@ -104,14 +105,37 @@ class EditDrawableText : EditText {
104105 }
105106 return super .onTouchEvent(event)
106107 }
107-
108+
109+ if (drawableTop != null ) {
110+ bounds = drawableTop!! .bounds
111+ val extraClickingArea = 13
112+ if (abs((width - paddingLeft - paddingRight) / 2 + paddingLeft - positionX) <= bounds.width() / 2 + extraClickingArea) {
113+ onDrawableClickListener!! .onClick(DrawablePosition .TOP )
114+ event.action = MotionEvent .ACTION_CANCEL
115+ return false
116+ }
117+ }
118+
119+ if (drawableBottom!= null )
120+ {
121+ bounds = drawableBottom!! .bounds
122+ val extraClickingArea = 13
123+
124+ if (abs((width - paddingLeft - paddingRight) / 2 + paddingLeft - positionX) <= bounds.width() / 2 + extraClickingArea) {
125+ onDrawableClickListener!! .onClick(DrawablePosition .BOTTOM )
126+ event.action = MotionEvent .ACTION_CANCEL
127+ return false
128+ }
129+ }
130+
131+
108132 }
109133 return super .onTouchEvent(event)
110134 }
111-
112-
113- fun setDrawableClickListener (onDrawableClickListener : onDrawableClickListener ) {
114- this .onDrawableClickListener = onDrawableClickListener
135+
136+
137+ fun setDrawableClickListener (OnDrawableClickListener : OnDrawableClickListener ) {
138+ this .onDrawableClickListener = OnDrawableClickListener
115139 }
116-
140+
117141}
0 commit comments