blob: b8c92c99b7285d89b6e2f34f921184601c8b4bab [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;
9import android.widget.Button;
10import android.widget.EditText;
11import android.widget.RelativeLayout;
12
13import com.savoirfairelinux.sflphone.R;
14
15public class ClearableEditText extends RelativeLayout {
16 LayoutInflater inflater = null;
17 EditText edit_text;
18 Button btn_clear;
19
20 public ClearableEditText(Context context, AttributeSet attrs, int defStyle) {
21 super(context, attrs, defStyle);
alisiond295ec22013-05-17 10:12:13 -040022 initViews();
23 }
24
25 public ClearableEditText(Context context, AttributeSet attrs) {
26 super(context, attrs);
alisiond295ec22013-05-17 10:12:13 -040027 initViews();
28 }
29
30 public ClearableEditText(Context context) {
31 super(context);
32 // TODO Auto-generated constructor stub
33 initViews();
34 }
35
36 void initViews() {
37 inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
38 inflater.inflate(R.layout.clearable_edit_text, this, true);
39 edit_text = (EditText) findViewById(R.id.clearable_edit);
40 btn_clear = (Button) findViewById(R.id.clearable_button_clear);
41 btn_clear.setVisibility(RelativeLayout.INVISIBLE);
42 clearText();
43 showHideClearButton();
44 }
45
46 void clearText() {
47 btn_clear.setOnClickListener(new OnClickListener() {
48 @Override
49 public void onClick(View v) {
50 // TODO Auto-generated method stub
51 edit_text.setText("");
52 }
53 });
54 }
55
56 void showHideClearButton() {
57 edit_text.addTextChangedListener(new TextWatcher() {
58
59 @Override
60 public void onTextChanged(CharSequence s, int start, int before, int count) {
61 // TODO Auto-generated method stub
62 if (s.length() > 0)
63 btn_clear.setVisibility(RelativeLayout.VISIBLE);
64 else
65 btn_clear.setVisibility(RelativeLayout.INVISIBLE);
66 }
67
68 @Override
69 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
70 // TODO Auto-generated method stub
71
72 }
73
74 @Override
75 public void afterTextChanged(Editable s) {
76 // TODO Auto-generated method stub
77
78 }
79 });
80 }
81
82 public Editable getText() {
83 Editable text = edit_text.getText();
84 return text;
85 }
alision9f7a6ec2013-05-24 16:26:26 -040086
87 public void setInputType(int typeClassNumber) {
88 edit_text.setFocusableInTouchMode(true);
89 edit_text.requestFocus();
90 edit_text.setInputType(typeClassNumber);
91
92 }
93
94 public EditText getEdit_text() {
95 return edit_text;
96 }
alisiond295ec22013-05-17 10:12:13 -040097}