blob: 8f1cb8a45e86684fb95e637b254d8dad9395fd09 [file] [log] [blame]
Alexandre Lision064e1e02013-10-01 16:18:42 -04001package org.sflphone.views;
2
3import org.sflphone.R;
alisiond295ec22013-05-17 10:12:13 -04004
5import android.content.Context;
6import android.text.Editable;
7import android.text.TextWatcher;
8import android.util.AttributeSet;
Alexandre Lisione2839d52013-10-01 09:37:37 -04009import android.view.DragEvent;
alisiond295ec22013-05-17 10:12:13 -040010import android.view.LayoutInflater;
11import android.view.View;
Alexandre Lision573045c2013-09-11 17:20:25 -040012import android.view.inputmethod.EditorInfo;
alisiond295ec22013-05-17 10:12:13 -040013import android.widget.Button;
14import android.widget.EditText;
15import android.widget.RelativeLayout;
16
Alexandre Lision064e1e02013-10-01 16:18:42 -040017
alisiond295ec22013-05-17 10:12:13 -040018
19public class ClearableEditText extends RelativeLayout {
20 LayoutInflater inflater = null;
21 EditText edit_text;
22 Button btn_clear;
Alexandre Lision31f46fc2013-09-26 11:19:54 -040023 private TextWatcher watch = null;
alisiond295ec22013-05-17 10:12:13 -040024
25 public ClearableEditText(Context context, AttributeSet attrs, int defStyle) {
26 super(context, attrs, defStyle);
alisiond295ec22013-05-17 10:12:13 -040027 initViews();
28 }
29
30 public ClearableEditText(Context context, AttributeSet attrs) {
31 super(context, attrs);
alisiond295ec22013-05-17 10:12:13 -040032 initViews();
33 }
34
35 public ClearableEditText(Context context) {
36 super(context);
37 // TODO Auto-generated constructor stub
38 initViews();
39 }
40
41 void initViews() {
42 inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
43 inflater.inflate(R.layout.clearable_edit_text, this, true);
44 edit_text = (EditText) findViewById(R.id.clearable_edit);
Alexandre Lision573045c2013-09-11 17:20:25 -040045 edit_text.setSingleLine();
46 edit_text.setImeOptions(EditorInfo.IME_ACTION_DONE);
alisiond295ec22013-05-17 10:12:13 -040047 btn_clear = (Button) findViewById(R.id.clearable_button_clear);
48 btn_clear.setVisibility(RelativeLayout.INVISIBLE);
Alexandre Lision1d68ea52013-10-01 09:38:11 -040049
50 // Dummy listener to fix an sdk issue: https://code.google.com/p/android/issues/detail?id=21775
51 edit_text.setOnDragListener(new OnDragListener() {
52
53 @Override
54 public boolean onDrag(View v, DragEvent event) {
55 if (event.getAction() == DragEvent.ACTION_DROP)
56 return true;
57 else
58 return false;
59 }
60 });
alisiond295ec22013-05-17 10:12:13 -040061 clearText();
62 showHideClearButton();
63 }
64
65 void clearText() {
66 btn_clear.setOnClickListener(new OnClickListener() {
67 @Override
68 public void onClick(View v) {
alisiond295ec22013-05-17 10:12:13 -040069 edit_text.setText("");
70 }
71 });
72 }
73
74 void showHideClearButton() {
75 edit_text.addTextChangedListener(new TextWatcher() {
76
77 @Override
78 public void onTextChanged(CharSequence s, int start, int before, int count) {
alisiond295ec22013-05-17 10:12:13 -040079 if (s.length() > 0)
80 btn_clear.setVisibility(RelativeLayout.VISIBLE);
81 else
82 btn_clear.setVisibility(RelativeLayout.INVISIBLE);
83 }
84
85 @Override
86 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
alisiond295ec22013-05-17 10:12:13 -040087 }
88
89 @Override
90 public void afterTextChanged(Editable s) {
alisiond295ec22013-05-17 10:12:13 -040091 }
92 });
93 }
94
95 public Editable getText() {
96 Editable text = edit_text.getText();
97 return text;
98 }
alision9f7a6ec2013-05-24 16:26:26 -040099
100 public void setInputType(int typeClassNumber) {
101 edit_text.setFocusableInTouchMode(true);
102 edit_text.requestFocus();
103 edit_text.setInputType(typeClassNumber);
104
105 }
106
107 public EditText getEdit_text() {
108 return edit_text;
109 }
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400110
111 public void setError(String string) {
Alexandre Lisione2839d52013-10-01 09:37:37 -0400112 edit_text.setError(string);
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400113 edit_text.requestFocus();
114 }
Alexandre Lisione2839d52013-10-01 09:37:37 -0400115
116 public void setTextWatcher(TextWatcher l) {
Alexandre Lision31f46fc2013-09-26 11:19:54 -0400117 watch = l;
118 edit_text.addTextChangedListener(watch);
119 }
Alexandre Lisione2839d52013-10-01 09:37:37 -0400120
121 public void unsetTextWatcher() {
Alexandre Lision31f46fc2013-09-26 11:19:54 -0400122 edit_text.removeTextChangedListener(watch);
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400123 }
alisiond295ec22013-05-17 10:12:13 -0400124}