blob: 652a9205abaca5a951a3c276ef009e8f3d09b795 [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;
20
21 public ClearableEditText(Context context, AttributeSet attrs, int defStyle) {
22 super(context, attrs, defStyle);
alisiond295ec22013-05-17 10:12:13 -040023 initViews();
24 }
25
26 public ClearableEditText(Context context, AttributeSet attrs) {
27 super(context, attrs);
alisiond295ec22013-05-17 10:12:13 -040028 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 Lision573045c2013-09-11 17:20:25 -040041 edit_text.setSingleLine();
42 edit_text.setImeOptions(EditorInfo.IME_ACTION_DONE);
alisiond295ec22013-05-17 10:12:13 -040043 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 }
alision9f7a6ec2013-05-24 16:26:26 -040089
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 Lisionc51ccb12013-09-11 16:00:30 -0400100
101 public void setError(String string) {
102 edit_text.setError(string);
103 edit_text.requestFocus();
104 }
alisiond295ec22013-05-17 10:12:13 -0400105}