b0d550d234d6e13b3bddba7f38f654ebb7cbf809
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / widgets / ActionEditText.java
1 package eu.alefzero.owncloud.widgets;
2
3 import java.lang.reflect.InvocationTargetException;
4 import java.lang.reflect.Method;
5
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;
22
23 public class ActionEditText extends EditText {
24 private String s;
25 private String optionOneString;
26 private int optionOneColor;
27 private String optionTwoString;
28 private int optionTwoColor;
29
30 private String badgeClickCallback;
31 private Rect btn_rect;
32
33 public ActionEditText(Context context, AttributeSet attrs) {
34 super(context, attrs);
35 getAttrs(attrs);
36 s = optionOneString;
37 }
38
39 public ActionEditText(Context context, AttributeSet attrs, int defStyle) {
40 super(context, attrs, defStyle);
41 getAttrs(attrs);
42 s = optionOneString;
43 }
44
45 @Override
46 protected void onDraw(Canvas canvas) {
47 super.onDraw(canvas);
48 Rect r = new Rect();
49
50 Paint p = getPaint();
51 Rect text_bounds = new Rect();
52
53 p.getTextBounds(s, 0, s.length(), text_bounds);
54
55 getDrawingRect(r);
56 r.top += 10;
57 r.bottom -= 10;
58 r.left = (int)(getWidth() - text_bounds.width() - 18);
59 r.right = getWidth() - 10;
60 btn_rect = r;
61
62 if (s.equals(optionOneString))
63 p.setColor(optionOneColor);
64 else
65 p.setColor(optionTwoColor);
66 canvas.drawRect(r, p);
67 p.setColor(Color.GRAY);
68
69 canvas.drawText(s, r.left + 3, r.bottom - (text_bounds.height()/2), p);
70
71 invalidate();
72 }
73
74 @Override
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;
87 Method method;
88 try {
89
90 method = getContext().getClass().getMethod(badgeClickCallback, paramtypes);
91 method.invoke(getContext(), this, s);
92
93 } catch (NoSuchMethodException e) {
94 e.printStackTrace();
95 } catch (IllegalArgumentException e) {
96 e.printStackTrace();
97 } catch (IllegalAccessException e) {
98 e.printStackTrace();
99 } catch (InvocationTargetException e) {
100 e.printStackTrace();
101 }
102
103 invalidate();
104 }
105 }
106 }
107 return r;
108 }
109
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);
117 }
118
119 }