alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 1 | package com.savoirfairelinux.sflphone.views; |
| 2 | |
| 3 | import android.content.Context; |
| 4 | import android.text.Editable; |
| 5 | import android.text.TextWatcher; |
| 6 | import android.util.AttributeSet; |
| 7 | import android.view.LayoutInflater; |
| 8 | import android.view.View; |
Alexandre Lision | 573045c | 2013-09-11 17:20:25 -0400 | [diff] [blame] | 9 | import android.view.inputmethod.EditorInfo; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 10 | import android.widget.Button; |
| 11 | import android.widget.EditText; |
| 12 | import android.widget.RelativeLayout; |
| 13 | |
| 14 | import com.savoirfairelinux.sflphone.R; |
| 15 | |
| 16 | public class ClearableEditText extends RelativeLayout { |
| 17 | LayoutInflater inflater = null; |
| 18 | EditText edit_text; |
| 19 | Button btn_clear; |
Alexandre Lision | 31f46fc | 2013-09-26 11:19:54 -0400 | [diff] [blame] | 20 | private TextWatcher watch = null; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 21 | |
| 22 | public ClearableEditText(Context context, AttributeSet attrs, int defStyle) { |
| 23 | super(context, attrs, defStyle); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 24 | initViews(); |
| 25 | } |
| 26 | |
| 27 | public ClearableEditText(Context context, AttributeSet attrs) { |
| 28 | super(context, attrs); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 29 | initViews(); |
| 30 | } |
| 31 | |
| 32 | public ClearableEditText(Context context) { |
| 33 | super(context); |
| 34 | // TODO Auto-generated constructor stub |
| 35 | initViews(); |
| 36 | } |
| 37 | |
| 38 | void initViews() { |
| 39 | inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 40 | inflater.inflate(R.layout.clearable_edit_text, this, true); |
| 41 | edit_text = (EditText) findViewById(R.id.clearable_edit); |
Alexandre Lision | 573045c | 2013-09-11 17:20:25 -0400 | [diff] [blame] | 42 | edit_text.setSingleLine(); |
| 43 | edit_text.setImeOptions(EditorInfo.IME_ACTION_DONE); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 44 | btn_clear = (Button) findViewById(R.id.clearable_button_clear); |
| 45 | btn_clear.setVisibility(RelativeLayout.INVISIBLE); |
| 46 | clearText(); |
| 47 | showHideClearButton(); |
| 48 | } |
| 49 | |
| 50 | void clearText() { |
| 51 | btn_clear.setOnClickListener(new OnClickListener() { |
| 52 | @Override |
| 53 | public void onClick(View v) { |
| 54 | // TODO Auto-generated method stub |
| 55 | edit_text.setText(""); |
| 56 | } |
| 57 | }); |
| 58 | } |
| 59 | |
| 60 | void showHideClearButton() { |
| 61 | edit_text.addTextChangedListener(new TextWatcher() { |
| 62 | |
| 63 | @Override |
| 64 | public void onTextChanged(CharSequence s, int start, int before, int count) { |
| 65 | // TODO Auto-generated method stub |
| 66 | if (s.length() > 0) |
| 67 | btn_clear.setVisibility(RelativeLayout.VISIBLE); |
| 68 | else |
| 69 | btn_clear.setVisibility(RelativeLayout.INVISIBLE); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
| 74 | // TODO Auto-generated method stub |
| 75 | |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public void afterTextChanged(Editable s) { |
| 80 | // TODO Auto-generated method stub |
| 81 | |
| 82 | } |
| 83 | }); |
| 84 | } |
| 85 | |
| 86 | public Editable getText() { |
| 87 | Editable text = edit_text.getText(); |
| 88 | return text; |
| 89 | } |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 90 | |
| 91 | public void setInputType(int typeClassNumber) { |
| 92 | edit_text.setFocusableInTouchMode(true); |
| 93 | edit_text.requestFocus(); |
| 94 | edit_text.setInputType(typeClassNumber); |
| 95 | |
| 96 | } |
| 97 | |
| 98 | public EditText getEdit_text() { |
| 99 | return edit_text; |
| 100 | } |
Alexandre Lision | c51ccb1 | 2013-09-11 16:00:30 -0400 | [diff] [blame] | 101 | |
| 102 | public void setError(String string) { |
| 103 | edit_text.setError(string); |
| 104 | edit_text.requestFocus(); |
| 105 | } |
Alexandre Lision | 64dc8c0 | 2013-09-25 15:32:25 -0400 | [diff] [blame] | 106 | |
Alexandre Lision | 31f46fc | 2013-09-26 11:19:54 -0400 | [diff] [blame] | 107 | public void setTextWatcher(TextWatcher l){ |
| 108 | watch = l; |
| 109 | edit_text.addTextChangedListener(watch); |
| 110 | } |
| 111 | |
| 112 | public void unsetTextWatcher(){ |
| 113 | edit_text.removeTextChangedListener(watch); |
Alexandre Lision | 64dc8c0 | 2013-09-25 15:32:25 -0400 | [diff] [blame] | 114 | } |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 115 | } |