Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 1 | package org.sflphone.views; |
| 2 | |
| 3 | import org.sflphone.R; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 4 | |
| 5 | import android.content.Context; |
| 6 | import android.text.Editable; |
| 7 | import android.text.TextWatcher; |
| 8 | import android.util.AttributeSet; |
Alexandre Lision | e2839d5 | 2013-10-01 09:37:37 -0400 | [diff] [blame] | 9 | import android.view.DragEvent; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 10 | import android.view.LayoutInflater; |
| 11 | import android.view.View; |
Alexandre Lision | 573045c | 2013-09-11 17:20:25 -0400 | [diff] [blame] | 12 | import android.view.inputmethod.EditorInfo; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 13 | import android.widget.Button; |
| 14 | import android.widget.EditText; |
| 15 | import android.widget.RelativeLayout; |
Alexandre Lision | 7f0bba5 | 2013-10-16 14:43:11 -0400 | [diff] [blame] | 16 | import android.widget.TextView.OnEditorActionListener; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 17 | |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 18 | |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 19 | |
| 20 | public class ClearableEditText extends RelativeLayout { |
| 21 | LayoutInflater inflater = null; |
| 22 | EditText edit_text; |
| 23 | Button btn_clear; |
Alexandre Lision | 31f46fc | 2013-09-26 11:19:54 -0400 | [diff] [blame] | 24 | private TextWatcher watch = null; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 25 | |
| 26 | public ClearableEditText(Context context, AttributeSet attrs, int defStyle) { |
| 27 | super(context, attrs, defStyle); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 28 | initViews(); |
| 29 | } |
| 30 | |
| 31 | public ClearableEditText(Context context, AttributeSet attrs) { |
| 32 | super(context, attrs); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 33 | 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 Lision | 573045c | 2013-09-11 17:20:25 -0400 | [diff] [blame] | 46 | edit_text.setSingleLine(); |
| 47 | edit_text.setImeOptions(EditorInfo.IME_ACTION_DONE); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 48 | btn_clear = (Button) findViewById(R.id.clearable_button_clear); |
| 49 | btn_clear.setVisibility(RelativeLayout.INVISIBLE); |
Alexandre Lision | 1d68ea5 | 2013-10-01 09:38:11 -0400 | [diff] [blame] | 50 | |
| 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 | }); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 62 | clearText(); |
| 63 | showHideClearButton(); |
| 64 | } |
| 65 | |
| 66 | void clearText() { |
| 67 | btn_clear.setOnClickListener(new OnClickListener() { |
| 68 | @Override |
| 69 | public void onClick(View v) { |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 70 | 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) { |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 80 | 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) { |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public void afterTextChanged(Editable s) { |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 92 | } |
| 93 | }); |
| 94 | } |
| 95 | |
| 96 | public Editable getText() { |
| 97 | Editable text = edit_text.getText(); |
| 98 | return text; |
| 99 | } |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 100 | |
| 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 Lision | c51ccb1 | 2013-09-11 16:00:30 -0400 | [diff] [blame] | 111 | |
| 112 | public void setError(String string) { |
Alexandre Lision | e2839d5 | 2013-10-01 09:37:37 -0400 | [diff] [blame] | 113 | edit_text.setError(string); |
Alexandre Lision | c51ccb1 | 2013-09-11 16:00:30 -0400 | [diff] [blame] | 114 | edit_text.requestFocus(); |
| 115 | } |
Alexandre Lision | e2839d5 | 2013-10-01 09:37:37 -0400 | [diff] [blame] | 116 | |
| 117 | public void setTextWatcher(TextWatcher l) { |
Alexandre Lision | 31f46fc | 2013-09-26 11:19:54 -0400 | [diff] [blame] | 118 | watch = l; |
| 119 | edit_text.addTextChangedListener(watch); |
| 120 | } |
Alexandre Lision | e2839d5 | 2013-10-01 09:37:37 -0400 | [diff] [blame] | 121 | |
| 122 | public void unsetTextWatcher() { |
Alexandre Lision | 31f46fc | 2013-09-26 11:19:54 -0400 | [diff] [blame] | 123 | edit_text.removeTextChangedListener(watch); |
Alexandre Lision | 64dc8c0 | 2013-09-25 15:32:25 -0400 | [diff] [blame] | 124 | } |
Alexandre Lision | 7f0bba5 | 2013-10-16 14:43:11 -0400 | [diff] [blame] | 125 | |
| 126 | public void setOnEditorActionListener(OnEditorActionListener onEditorActionListener) { |
| 127 | edit_text.setOnEditorActionListener(onEditorActionListener); |
| 128 | |
| 129 | } |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 130 | } |