Alexandre Lision | a8b7872 | 2013-12-13 10:18:33 -0500 | [diff] [blame] | 1 | /* |
Alexandre Lision | c1024c0 | 2014-01-06 11:12:53 -0500 | [diff] [blame] | 2 | * Copyright (C) 2004-2014 Savoir-Faire Linux Inc. |
Alexandre Lision | a8b7872 | 2013-12-13 10:18:33 -0500 | [diff] [blame] | 3 | * |
| 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 Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 32 | package org.sflphone.views; |
| 33 | |
| 34 | import org.sflphone.R; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 35 | |
| 36 | import android.content.Context; |
| 37 | import android.text.Editable; |
| 38 | import android.text.TextWatcher; |
| 39 | import android.util.AttributeSet; |
Alexandre Lision | e2839d5 | 2013-10-01 09:37:37 -0400 | [diff] [blame] | 40 | import android.view.DragEvent; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 41 | import android.view.LayoutInflater; |
| 42 | import android.view.View; |
Alexandre Lision | 573045c | 2013-09-11 17:20:25 -0400 | [diff] [blame] | 43 | import android.view.inputmethod.EditorInfo; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 44 | import android.widget.Button; |
| 45 | import android.widget.EditText; |
| 46 | import android.widget.RelativeLayout; |
Alexandre Lision | 7f0bba5 | 2013-10-16 14:43:11 -0400 | [diff] [blame] | 47 | import android.widget.TextView.OnEditorActionListener; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 48 | |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 49 | |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 50 | |
| 51 | public class ClearableEditText extends RelativeLayout { |
| 52 | LayoutInflater inflater = null; |
| 53 | EditText edit_text; |
| 54 | Button btn_clear; |
Alexandre Lision | 31f46fc | 2013-09-26 11:19:54 -0400 | [diff] [blame] | 55 | private TextWatcher watch = null; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 56 | |
| 57 | public ClearableEditText(Context context, AttributeSet attrs, int defStyle) { |
| 58 | super(context, attrs, defStyle); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 59 | initViews(); |
| 60 | } |
| 61 | |
| 62 | public ClearableEditText(Context context, AttributeSet attrs) { |
| 63 | super(context, attrs); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 64 | 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 Lision | 573045c | 2013-09-11 17:20:25 -0400 | [diff] [blame] | 77 | edit_text.setSingleLine(); |
| 78 | edit_text.setImeOptions(EditorInfo.IME_ACTION_DONE); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 79 | btn_clear = (Button) findViewById(R.id.clearable_button_clear); |
| 80 | btn_clear.setVisibility(RelativeLayout.INVISIBLE); |
Alexandre Lision | 1d68ea5 | 2013-10-01 09:38:11 -0400 | [diff] [blame] | 81 | |
| 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 | }); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 93 | clearText(); |
| 94 | showHideClearButton(); |
| 95 | } |
| 96 | |
| 97 | void clearText() { |
| 98 | btn_clear.setOnClickListener(new OnClickListener() { |
| 99 | @Override |
| 100 | public void onClick(View v) { |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 101 | 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) { |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 111 | 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) { |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public void afterTextChanged(Editable s) { |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 123 | } |
| 124 | }); |
| 125 | } |
| 126 | |
| 127 | public Editable getText() { |
| 128 | Editable text = edit_text.getText(); |
| 129 | return text; |
| 130 | } |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 131 | |
| 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 Lision | c51ccb1 | 2013-09-11 16:00:30 -0400 | [diff] [blame] | 142 | |
| 143 | public void setError(String string) { |
Alexandre Lision | e2839d5 | 2013-10-01 09:37:37 -0400 | [diff] [blame] | 144 | edit_text.setError(string); |
Alexandre Lision | c51ccb1 | 2013-09-11 16:00:30 -0400 | [diff] [blame] | 145 | edit_text.requestFocus(); |
| 146 | } |
Alexandre Lision | e2839d5 | 2013-10-01 09:37:37 -0400 | [diff] [blame] | 147 | |
| 148 | public void setTextWatcher(TextWatcher l) { |
Alexandre Lision | 31f46fc | 2013-09-26 11:19:54 -0400 | [diff] [blame] | 149 | watch = l; |
| 150 | edit_text.addTextChangedListener(watch); |
| 151 | } |
Alexandre Lision | e2839d5 | 2013-10-01 09:37:37 -0400 | [diff] [blame] | 152 | |
| 153 | public void unsetTextWatcher() { |
Alexandre Lision | 31f46fc | 2013-09-26 11:19:54 -0400 | [diff] [blame] | 154 | edit_text.removeTextChangedListener(watch); |
Alexandre Lision | 64dc8c0 | 2013-09-25 15:32:25 -0400 | [diff] [blame] | 155 | } |
Alexandre Lision | 7f0bba5 | 2013-10-16 14:43:11 -0400 | [diff] [blame] | 156 | |
| 157 | public void setOnEditorActionListener(OnEditorActionListener onEditorActionListener) { |
| 158 | edit_text.setOnEditorActionListener(onEditorActionListener); |
| 159 | |
| 160 | } |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 161 | } |