MCPcopy Create free account
hub / github.com/Morxander/EditCard / EditCard

Class EditCard

EditCard/src/main/java/morxander/editcard/EditCard.java:19–137  ·  view source on GitHub ↗

Created by morxander on 11/19/16.

Source from the content-addressed store, hash-verified

17 */
18
19public class EditCard extends EditText {
20
21 String type = "UNKNOWN";
22
23 public EditCard(Context context) {
24 super(context);
25 addMagic();
26 }
27
28 public EditCard(Context context, AttributeSet attrs) {
29 super(context, attrs);
30 addMagic();
31 }
32
33 public EditCard(Context context, AttributeSet attrs, int defStyleAttr) {
34 super(context, attrs, defStyleAttr);
35 addMagic();
36 }
37
38 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
39 public EditCard(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
40 super(context, attrs, defStyleAttr, defStyleRes);
41 addMagic();
42 }
43
44 private void addMagic() {
45 // Changing the icon when it's empty
46 changeIcon();
47 // Adding the TextWatcher
48 addTextChangedListener(new TextWatcher() {
49 @Override
50 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
51
52 }
53
54 @Override
55 public void onTextChanged(CharSequence s, int position, int before, int action) {
56 if (action == 1) {
57 if (type.equals("UNKNOWN") || type.equals("Visa") || type.equals("Discover") || type.equals("JCB")) {
58 if (position == 3 || position == 8 || position == 13) {
59 if (!s.toString().endsWith("-")) {
60 append("-");
61 }
62 }
63 } else if (type.equals("American_Express") || type.equals("Diners_Club")) {
64 if (position == 3 || position == 10) {
65 if (!s.toString().endsWith("-")) {
66 append("-");
67 }
68 }
69 }
70 }
71 }
72
73 @Override
74 public void afterTextChanged(Editable editable) {
75 changeIcon();
76 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected