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