blob: bf10a05332dbb27efd9580eea2e70a08d1d32627 [file] [log] [blame]
alisiond295ec22013-05-17 10:12:13 -04001package com.savoirfairelinux.sflphone.views;
2
3import android.content.Context;
4import android.text.Editable;
5import android.text.TextWatcher;
6import android.util.AttributeSet;
7import android.view.LayoutInflater;
8import android.view.View;
Alexandre Lision573045c2013-09-11 17:20:25 -04009import android.view.inputmethod.EditorInfo;
alisiond295ec22013-05-17 10:12:13 -040010import android.widget.Button;
11import android.widget.EditText;
12import android.widget.RelativeLayout;
13
14import com.savoirfairelinux.sflphone.R;
15
16public class ClearableEditText extends RelativeLayout {
17 LayoutInflater inflater = null;
18 EditText edit_text;
19 Button btn_clear;
Alexandre Lision31f46fc2013-09-26 11:19:54 -040020 private TextWatcher watch = null;
alisiond295ec22013-05-17 10:12:13 -040021
22 public ClearableEditText(Context context, AttributeSet attrs, int defStyle) {
23 super(context, attrs, defStyle);
alisiond295ec22013-05-17 10:12:13 -040024 initViews();
25 }
26
27 public ClearableEditText(Context context, AttributeSet attrs) {
28 super(context, attrs);
alisiond295ec22013-05-17 10:12:13 -040029 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 Lision573045c2013-09-11 17:20:25 -040042 edit_text.setSingleLine();
43 edit_text.setImeOptions(EditorInfo.IME_ACTION_DONE);
alisiond295ec22013-05-17 10:12:13 -040044 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 }
alision9f7a6ec2013-05-24 16:26:26 -040090
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 Lisionc51ccb12013-09-11 16:00:30 -0400101
102 public void setError(String string) {
103 edit_text.setError(string);
104 edit_text.requestFocus();
105 }
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400106
Alexandre Lision31f46fc2013-09-26 11:19:54 -0400107 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 Lision64dc8c02013-09-25 15:32:25 -0400114 }
alisiond295ec22013-05-17 10:12:13 -0400115}