blob: 74f234ff1da5445bed23d4d8efac1d412ac0ec52 [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;
Alexandre Lision7f0bba52013-10-16 14:43:11 -040016import android.widget.TextView.OnEditorActionListener;
alisiond295ec22013-05-17 10:12:13 -040017
Alexandre Lision064e1e02013-10-01 16:18:42 -040018
alisiond295ec22013-05-17 10:12:13 -040019
20public class ClearableEditText extends RelativeLayout {
21 LayoutInflater inflater = null;
22 EditText edit_text;
23 Button btn_clear;
Alexandre Lision31f46fc2013-09-26 11:19:54 -040024 private TextWatcher watch = null;
alisiond295ec22013-05-17 10:12:13 -040025
26 public ClearableEditText(Context context, AttributeSet attrs, int defStyle) {
27 super(context, attrs, defStyle);
alisiond295ec22013-05-17 10:12:13 -040028 initViews();
29 }
30
31 public ClearableEditText(Context context, AttributeSet attrs) {
32 super(context, attrs);
alisiond295ec22013-05-17 10:12:13 -040033 initViews();
34 }
35
36 public ClearableEditText(Context context) {
37 super(context);
38 // TODO Auto-generated constructor stub
39 initViews();
40 }
41
42 void initViews() {
43 inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
44 inflater.inflate(R.layout.clearable_edit_text, this, true);
45 edit_text = (EditText) findViewById(R.id.clearable_edit);
Alexandre Lision573045c2013-09-11 17:20:25 -040046 edit_text.setSingleLine();
47 edit_text.setImeOptions(EditorInfo.IME_ACTION_DONE);
alisiond295ec22013-05-17 10:12:13 -040048 btn_clear = (Button) findViewById(R.id.clearable_button_clear);
49 btn_clear.setVisibility(RelativeLayout.INVISIBLE);
Alexandre Lision1d68ea52013-10-01 09:38:11 -040050
51 // Dummy listener to fix an sdk issue: https://code.google.com/p/android/issues/detail?id=21775
52 edit_text.setOnDragListener(new OnDragListener() {
53
54 @Override
55 public boolean onDrag(View v, DragEvent event) {
56 if (event.getAction() == DragEvent.ACTION_DROP)
57 return true;
58 else
59 return false;
60 }
61 });
alisiond295ec22013-05-17 10:12:13 -040062 clearText();
63 showHideClearButton();
64 }
65
66 void clearText() {
67 btn_clear.setOnClickListener(new OnClickListener() {
68 @Override
69 public void onClick(View v) {
alisiond295ec22013-05-17 10:12:13 -040070 edit_text.setText("");
71 }
72 });
73 }
74
75 void showHideClearButton() {
76 edit_text.addTextChangedListener(new TextWatcher() {
77
78 @Override
79 public void onTextChanged(CharSequence s, int start, int before, int count) {
alisiond295ec22013-05-17 10:12:13 -040080 if (s.length() > 0)
81 btn_clear.setVisibility(RelativeLayout.VISIBLE);
82 else
83 btn_clear.setVisibility(RelativeLayout.INVISIBLE);
84 }
85
86 @Override
87 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
alisiond295ec22013-05-17 10:12:13 -040088 }
89
90 @Override
91 public void afterTextChanged(Editable s) {
alisiond295ec22013-05-17 10:12:13 -040092 }
93 });
94 }
95
96 public Editable getText() {
97 Editable text = edit_text.getText();
98 return text;
99 }
alision9f7a6ec2013-05-24 16:26:26 -0400100
101 public void setInputType(int typeClassNumber) {
102 edit_text.setFocusableInTouchMode(true);
103 edit_text.requestFocus();
104 edit_text.setInputType(typeClassNumber);
105
106 }
107
108 public EditText getEdit_text() {
109 return edit_text;
110 }
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400111
112 public void setError(String string) {
Alexandre Lisione2839d52013-10-01 09:37:37 -0400113 edit_text.setError(string);
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400114 edit_text.requestFocus();
115 }
Alexandre Lisione2839d52013-10-01 09:37:37 -0400116
117 public void setTextWatcher(TextWatcher l) {
Alexandre Lision31f46fc2013-09-26 11:19:54 -0400118 watch = l;
119 edit_text.addTextChangedListener(watch);
120 }
Alexandre Lisione2839d52013-10-01 09:37:37 -0400121
122 public void unsetTextWatcher() {
Alexandre Lision31f46fc2013-09-26 11:19:54 -0400123 edit_text.removeTextChangedListener(watch);
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400124 }
Alexandre Lision7f0bba52013-10-16 14:43:11 -0400125
126 public void setOnEditorActionListener(OnEditorActionListener onEditorActionListener) {
127 edit_text.setOnEditorActionListener(onEditorActionListener);
128
129 }
alisiond295ec22013-05-17 10:12:13 -0400130}