blob: 4985b28b779085f2271fc1627f2bd1c6c628c91a [file] [log] [blame]
Alexandre Lisiona8b78722013-12-13 10:18:33 -05001/*
Alexandre Lisionc1024c02014-01-06 11:12:53 -05002 * Copyright (C) 2004-2014 Savoir-Faire Linux Inc.
Alexandre Lisiona8b78722013-12-13 10:18:33 -05003 *
4 * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 * Additional permission under GNU GPL version 3 section 7:
21 *
22 * If you modify this program, or any covered work, by linking or
23 * combining it with the OpenSSL project's OpenSSL library (or a
24 * modified version of that library), containing parts covered by the
25 * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
26 * grants you additional permission to convey the resulting work.
27 * Corresponding Source for a non-source form of such a combination
28 * shall include the source code for the parts of OpenSSL used as well
29 * as that of the covered work.
30 */
31
Alexandre Lision064e1e02013-10-01 16:18:42 -040032package org.sflphone.views;
33
34import org.sflphone.R;
alisiond295ec22013-05-17 10:12:13 -040035
36import android.content.Context;
37import android.text.Editable;
38import android.text.TextWatcher;
39import android.util.AttributeSet;
Alexandre Lisione2839d52013-10-01 09:37:37 -040040import android.view.DragEvent;
alisiond295ec22013-05-17 10:12:13 -040041import android.view.LayoutInflater;
42import android.view.View;
Alexandre Lision573045c2013-09-11 17:20:25 -040043import android.view.inputmethod.EditorInfo;
alisiond295ec22013-05-17 10:12:13 -040044import android.widget.Button;
45import android.widget.EditText;
46import android.widget.RelativeLayout;
Alexandre Lision7f0bba52013-10-16 14:43:11 -040047import android.widget.TextView.OnEditorActionListener;
alisiond295ec22013-05-17 10:12:13 -040048
Alexandre Lision064e1e02013-10-01 16:18:42 -040049
alisiond295ec22013-05-17 10:12:13 -040050
51public class ClearableEditText extends RelativeLayout {
52 LayoutInflater inflater = null;
53 EditText edit_text;
54 Button btn_clear;
Alexandre Lision31f46fc2013-09-26 11:19:54 -040055 private TextWatcher watch = null;
alisiond295ec22013-05-17 10:12:13 -040056
57 public ClearableEditText(Context context, AttributeSet attrs, int defStyle) {
58 super(context, attrs, defStyle);
alisiond295ec22013-05-17 10:12:13 -040059 initViews();
60 }
61
62 public ClearableEditText(Context context, AttributeSet attrs) {
63 super(context, attrs);
alisiond295ec22013-05-17 10:12:13 -040064 initViews();
65 }
66
67 public ClearableEditText(Context context) {
68 super(context);
69 // TODO Auto-generated constructor stub
70 initViews();
71 }
72
73 void initViews() {
74 inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
75 inflater.inflate(R.layout.clearable_edit_text, this, true);
76 edit_text = (EditText) findViewById(R.id.clearable_edit);
Alexandre Lision573045c2013-09-11 17:20:25 -040077 edit_text.setSingleLine();
78 edit_text.setImeOptions(EditorInfo.IME_ACTION_DONE);
alisiond295ec22013-05-17 10:12:13 -040079 btn_clear = (Button) findViewById(R.id.clearable_button_clear);
80 btn_clear.setVisibility(RelativeLayout.INVISIBLE);
Alexandre Lision1d68ea52013-10-01 09:38:11 -040081
82 // Dummy listener to fix an sdk issue: https://code.google.com/p/android/issues/detail?id=21775
83 edit_text.setOnDragListener(new OnDragListener() {
84
85 @Override
86 public boolean onDrag(View v, DragEvent event) {
87 if (event.getAction() == DragEvent.ACTION_DROP)
88 return true;
89 else
90 return false;
91 }
92 });
alisiond295ec22013-05-17 10:12:13 -040093 clearText();
94 showHideClearButton();
95 }
96
97 void clearText() {
98 btn_clear.setOnClickListener(new OnClickListener() {
99 @Override
100 public void onClick(View v) {
alisiond295ec22013-05-17 10:12:13 -0400101 edit_text.setText("");
102 }
103 });
104 }
105
106 void showHideClearButton() {
107 edit_text.addTextChangedListener(new TextWatcher() {
108
109 @Override
110 public void onTextChanged(CharSequence s, int start, int before, int count) {
alisiond295ec22013-05-17 10:12:13 -0400111 if (s.length() > 0)
112 btn_clear.setVisibility(RelativeLayout.VISIBLE);
113 else
114 btn_clear.setVisibility(RelativeLayout.INVISIBLE);
115 }
116
117 @Override
118 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
alisiond295ec22013-05-17 10:12:13 -0400119 }
120
121 @Override
122 public void afterTextChanged(Editable s) {
alisiond295ec22013-05-17 10:12:13 -0400123 }
124 });
125 }
126
127 public Editable getText() {
128 Editable text = edit_text.getText();
129 return text;
130 }
alision9f7a6ec2013-05-24 16:26:26 -0400131
132 public void setInputType(int typeClassNumber) {
133 edit_text.setFocusableInTouchMode(true);
134 edit_text.requestFocus();
135 edit_text.setInputType(typeClassNumber);
136
137 }
138
139 public EditText getEdit_text() {
140 return edit_text;
141 }
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400142
143 public void setError(String string) {
Alexandre Lisione2839d52013-10-01 09:37:37 -0400144 edit_text.setError(string);
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400145 edit_text.requestFocus();
146 }
Alexandre Lisione2839d52013-10-01 09:37:37 -0400147
148 public void setTextWatcher(TextWatcher l) {
Alexandre Lision31f46fc2013-09-26 11:19:54 -0400149 watch = l;
150 edit_text.addTextChangedListener(watch);
151 }
Alexandre Lisione2839d52013-10-01 09:37:37 -0400152
153 public void unsetTextWatcher() {
Alexandre Lision31f46fc2013-09-26 11:19:54 -0400154 edit_text.removeTextChangedListener(watch);
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400155 }
Alexandre Lision7f0bba52013-10-16 14:43:11 -0400156
157 public void setOnEditorActionListener(OnEditorActionListener onEditorActionListener) {
158 edit_text.setOnEditorActionListener(onEditorActionListener);
159
160 }
alisiond295ec22013-05-17 10:12:13 -0400161}