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
.Resources
;
9 import android
.content
.res
.TypedArray
;
10 import android
.graphics
.Canvas
;
11 import android
.graphics
.Color
;
12 import android
.graphics
.Paint
;
13 import android
.graphics
.Rect
;
14 import android
.text
.InputType
;
15 import android
.util
.AttributeSet
;
16 import android
.util
.Log
;
17 import android
.view
.MotionEvent
;
18 import android
.view
.View
;
19 import android
.widget
.CheckBox
;
20 import android
.widget
.EditText
;
21 import android
.widget
.TextView
;
23 public class ActionEditText
extends EditText
{
25 private String optionOneString
;
26 private int optionOneColor
;
27 private String optionTwoString
;
28 private int optionTwoColor
;
30 private String badgeClickCallback
;
31 private Rect btn_rect
;
33 public ActionEditText(Context context
, AttributeSet attrs
) {
34 super(context
, attrs
);
39 public ActionEditText(Context context
, AttributeSet attrs
, int defStyle
) {
40 super(context
, attrs
, defStyle
);
46 protected void onDraw(Canvas canvas
) {
51 Rect text_bounds
= new Rect();
53 p
.getTextBounds(s
, 0, s
.length(), text_bounds
);
58 r
.left
= (int)(getWidth() - text_bounds
.width() - 18);
59 r
.right
= getWidth() - 10;
62 if (s
.equals(optionOneString
))
63 p
.setColor(optionOneColor
);
65 p
.setColor(optionTwoColor
);
66 canvas
.drawRect(r
, p
);
67 p
.setColor(Color
.GRAY
);
69 canvas
.drawText(s
, r
.left
+ 3, r
.bottom
- (text_bounds
.height()/2), p
);
75 public boolean onTouchEvent(MotionEvent event
) {
76 int touchX
= (int) event
.getX();
77 int touchY
= (int) event
.getY();
78 boolean r
= super.onTouchEvent(event
);
79 if (event
.getAction() == MotionEvent
.ACTION_UP
) {
80 if (btn_rect
.contains(touchX
, touchY
)) {
81 if (s
.equals(optionTwoString
)) s
= optionOneString
;
82 else s
= optionTwoString
;
83 if (badgeClickCallback
!= null
) {
84 Class
[] paramtypes
= new Class
[2];
85 paramtypes
[0] = android
.view
.View
.class;
86 paramtypes
[1] = String
.class;
90 method
= getContext().getClass().getMethod(badgeClickCallback
, paramtypes
);
91 method
.invoke(getContext(), this, s
);
93 } catch (NoSuchMethodException e
) {
95 } catch (IllegalArgumentException e
) {
97 } catch (IllegalAccessException e
) {
99 } catch (InvocationTargetException e
) {
110 private void getAttrs(AttributeSet attr
) {
111 TypedArray a
= getContext().obtainStyledAttributes(attr
, R
.styleable
.ActionEditText
);
112 optionOneString
= a
.getString(R
.styleable
.ActionEditText_optionOneString
);
113 optionTwoString
= a
.getString(R
.styleable
.ActionEditText_optionTwoString
);
114 optionOneColor
= a
.getColor(R
.styleable
.ActionEditText_optionOneColor
, 0x00ff00);
115 optionTwoColor
= a
.getColor(R
.styleable
.ActionEditText_optionTwoColor
, 0xff0000);
116 badgeClickCallback
= a
.getString(R
.styleable
.ActionEditText_onBadgeClick
);