1 package eu
.alefzero
.owncloud
.widgets
;
3 import java
.lang
.reflect
.InvocationTargetException
;
4 import java
.lang
.reflect
.Method
;
6 import eu
.alefzero
.owncloud
.R
;
7 import android
.content
.Context
;
8 import android
.content
.res
.TypedArray
;
9 import android
.graphics
.Canvas
;
10 import android
.graphics
.Color
;
11 import android
.graphics
.Paint
;
12 import android
.graphics
.Rect
;
13 import android
.util
.AttributeSet
;
14 import android
.view
.MotionEvent
;
15 import android
.widget
.EditText
;
17 public class ActionEditText
extends EditText
{
19 private String optionOneString
;
20 private int optionOneColor
;
21 private String optionTwoString
;
22 private int optionTwoColor
;
23 private Rect mTextBounds
, mButtonRect
;
25 private String badgeClickCallback
;
26 private Rect btn_rect
;
28 public ActionEditText(Context context
, AttributeSet attrs
) {
29 super(context
, attrs
);
32 mTextBounds
= new Rect();
33 mButtonRect
= new Rect();
36 public ActionEditText(Context context
, AttributeSet attrs
, int defStyle
) {
37 super(context
, attrs
, defStyle
);
40 mTextBounds
= new Rect();
41 mButtonRect
= new Rect();
45 protected void onDraw(Canvas canvas
) {
50 p
.getTextBounds(s
, 0, s
.length(), mTextBounds
);
52 getDrawingRect(mButtonRect
);
53 mButtonRect
.top
+= 10;
54 mButtonRect
.bottom
-= 10;
55 mButtonRect
.left
= (int)(getWidth() - mTextBounds
.width() - 18);
56 mButtonRect
.right
= getWidth() - 10;
57 btn_rect
= mButtonRect
;
59 if (s
.equals(optionOneString
))
60 p
.setColor(optionOneColor
);
62 p
.setColor(optionTwoColor
);
63 canvas
.drawRect(mButtonRect
, p
);
64 p
.setColor(Color
.GRAY
);
66 canvas
.drawText(s
, mButtonRect
.left
+ 3, mButtonRect
.bottom
- (mTextBounds
.height()/2), p
);
72 public boolean onTouchEvent(MotionEvent event
) {
73 int touchX
= (int) event
.getX();
74 int touchY
= (int) event
.getY();
75 boolean r
= super.onTouchEvent(event
);
76 if (event
.getAction() == MotionEvent
.ACTION_UP
) {
77 if (btn_rect
.contains(touchX
, touchY
)) {
78 if (s
.equals(optionTwoString
)) s
= optionOneString
;
79 else s
= optionTwoString
;
80 if (badgeClickCallback
!= null
) {
81 @SuppressWarnings("rawtypes")
82 Class
[] paramtypes
= new Class
[2];
83 paramtypes
[0] = android
.view
.View
.class;
84 paramtypes
[1] = String
.class;
88 method
= getContext().getClass().getMethod(badgeClickCallback
, paramtypes
);
89 method
.invoke(getContext(), this, s
);
91 } catch (NoSuchMethodException e
) {
93 } catch (IllegalArgumentException e
) {
95 } catch (IllegalAccessException e
) {
97 } catch (InvocationTargetException e
) {
108 private void getAttrs(AttributeSet attr
) {
109 TypedArray a
= getContext().obtainStyledAttributes(attr
, R
.styleable
.ActionEditText
);
110 optionOneString
= a
.getString(R
.styleable
.ActionEditText_optionOneString
);
111 optionTwoString
= a
.getString(R
.styleable
.ActionEditText_optionTwoString
);
112 optionOneColor
= a
.getColor(R
.styleable
.ActionEditText_optionOneColor
, 0x00ff00);
113 optionTwoColor
= a
.getColor(R
.styleable
.ActionEditText_optionTwoColor
, 0xff0000);
114 badgeClickCallback
= a
.getString(R
.styleable
.ActionEditText_onBadgeClick
);