Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions pinentry/src/main/java/me/philio/pinentry/PinEntryView.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public class PinEntryView extends ViewGroup {
private int digitSpacing;
private int digitTextSize;
private int digitTextColor;
private int digitPlaceholderTextColor;
private int digitElevation;

/**
Expand All @@ -85,6 +86,10 @@ public class PinEntryView extends ViewGroup {
*/
private String mask = "*";

/**
* String to store the placeholder, if any
*/
private String placeholder;
/**
* Edit text to handle input
*/
Expand Down Expand Up @@ -151,6 +156,10 @@ public PinEntryView(Context context, AttributeSet attrs, int defStyle) {
accentColor.resourceId > 0 ? getResources().getColor(accentColor.resourceId) :
accentColor.data);

// Placeholder Text color, currently defaults to material design secondary color
digitPlaceholderTextColor =
getResources().getColor(R.color.abc_secondary_text_material_light);

// Mask character
String maskCharacter = array.getString(R.styleable.PinEntryView_mask);
if (maskCharacter != null) {
Expand Down Expand Up @@ -180,7 +189,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int height = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);

// Measure children
for (int i = 0; i < getChildCount(); i ++) {
for (int i = 0; i < getChildCount(); i++) {
getChildAt(i).measure(width, height);
}
}
Expand Down Expand Up @@ -282,6 +291,20 @@ public void setText(CharSequence text) {
editText.setText(text);
}

/**
* Set a placeholder text to digitViews
*
* @param text
*/
public void setPlaceholderText(String text) {
placeholder = text;
for (int i = 0; i < digits; ++i) {
TextView view = (TextView) getChildAt(i);
view.setText(String.valueOf(placeholder.charAt(i)));
view.setTextColor(digitPlaceholderTextColor);
}
}

/**
* Clear pin input
*/
Expand All @@ -295,7 +318,7 @@ public void clearText() {
private void addViews() {
// Add a digit view for each digit
for (int i = 0; i < digits; i++) {
DigitView digitView = new DigitView(getContext() );
DigitView digitView = new DigitView(getContext());
digitView.setWidth(digitWidth);
digitView.setHeight(digitHeight);
digitView.setBackgroundResource(digitBackground);
Expand Down Expand Up @@ -353,8 +376,15 @@ public void afterTextChanged(Editable s) {
String mask = PinEntryView.this.mask == null || PinEntryView.this.mask.length() == 0 ?
String.valueOf(s.charAt(i)) : PinEntryView.this.mask;
((TextView) getChildAt(i)).setText(mask);
((TextView) getChildAt(i)).setTextColor(digitTextColor);
} else {
((TextView) getChildAt(i)).setText("");
if (placeholder == null) {
((TextView) getChildAt(i)).setText("");
} else {
((TextView) getChildAt(i))
.setText(String.valueOf(placeholder.charAt(i)));
((TextView) getChildAt(i)).setTextColor(digitPlaceholderTextColor);
}
}
if (editText.hasFocus()) {
getChildAt(i).setSelected(accentType == ACCENT_ALL ||
Expand Down