-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathCreditCardView.java
More file actions
130 lines (104 loc) · 3.98 KB
/
CreditCardView.java
File metadata and controls
130 lines (104 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.flutterwave.raveandroid.card;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.text.InputType;
import android.util.AttributeSet;
import android.util.Log;
import android.util.SparseArray;
import com.flutterwave.raveandroid.R;
import com.google.android.material.textfield.TextInputEditText;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by hamzafetuga on 24/07/2017.
*/
public class CreditCardView extends TextInputEditText {
private SparseArray<Pattern> mCCPatterns = null;
//default credit card image
private final int mDefaultDrawableResId = R.drawable.ic_credit_card;
private int mCurrentDrawableResId = 0;
private Drawable mCurrentDrawable;
String lastFormattedText;
public CreditCardView(Context context) {
super(context);
init();
}
public CreditCardView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CreditCardView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
lastFormattedText = "";
if (mCCPatterns == null) {
mCCPatterns = new SparseArray<>();
// With spaces for credit card masking
mCCPatterns.put(R.drawable.ic_visa, Pattern.compile(
"^4[0-9]{2,12}(?:[0-9]{3})?$"));
mCCPatterns.put(R.drawable.ic_master_card, Pattern.compile(
"^5[1-5][0-9]{1,14}$"));
mCCPatterns.put(R.drawable.ic_american_express, Pattern.compile(
"^3[47][0-9]{1,13}$"));
///^([506]{3})([0-9]{1,16})$/
mCCPatterns.put(R.drawable.ic_verve_logo, Pattern.compile(
"^([506]{3})([0-9]{1,16})$"
));
}
setInputType(InputType.TYPE_CLASS_PHONE);
addTextChangedListener(new CreditCardTextWatcher());
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mCurrentDrawable == null) {
return;
}
int rightOffset = 0;
if (getError() != null && getError().length() > 0) {
rightOffset = (int) getResources().getDisplayMetrics().density * 32;
}
int right = getWidth() - getPaddingRight() - rightOffset;
int top = getPaddingTop();
int bottom = getHeight() - getPaddingBottom();
float ratio = (float) mCurrentDrawable.getIntrinsicWidth() / (float) mCurrentDrawable.getIntrinsicHeight();
//int left = right - mCurrentDrawable.getIntrinsicWidth(); //If images are correct size.
int left = (int) (right - ((bottom - top) * ratio)); //scale image depeding on height available.
mCurrentDrawable.setBounds(left, top, right, bottom);
mCurrentDrawable.draw(canvas);
}
@Override
protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
if (mCCPatterns == null) {
init();
}
//
String actualText = text.toString().replaceAll("\\s", "");
Log.d("actual", actualText);
if (lengthBefore == actualText.length()) {
return;
}
int mDrawableResId = 0;
for (int i = 0; i < mCCPatterns.size(); i++) {
int key = mCCPatterns.keyAt(i);
// get the object by the key.
Pattern p = mCCPatterns.get(key);
Matcher m = p.matcher(actualText);
if (m.find()) {
mDrawableResId = key;
break;
}
}
if (mDrawableResId > 0 && mDrawableResId !=
mCurrentDrawableResId) {
mCurrentDrawableResId = mDrawableResId;
} else if (mDrawableResId == 0) {
mCurrentDrawableResId = mDefaultDrawableResId;
}
mCurrentDrawable = getResources()
.getDrawable(mCurrentDrawableResId);
}
}