From a2a536f70aabdc4f9f7143c101443e1f99722bda Mon Sep 17 00:00:00 2001 From: Nicolas Carrasco-Stevenson Date: Fri, 15 Jan 2016 11:02:45 -0300 Subject: [PATCH 1/3] Add a method to set a placeholder for the pin A simple method to set text of the DigitViews before the user has introduced the pin. --- .../src/main/java/me/philio/pinentry/PinEntryView.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pinentry/src/main/java/me/philio/pinentry/PinEntryView.java b/pinentry/src/main/java/me/philio/pinentry/PinEntryView.java index 4f6902d..0b932aa 100644 --- a/pinentry/src/main/java/me/philio/pinentry/PinEntryView.java +++ b/pinentry/src/main/java/me/philio/pinentry/PinEntryView.java @@ -282,6 +282,16 @@ public void setText(CharSequence text) { editText.setText(text); } + /** + * Set a placeholder text to digitViews + * @param text + */ + public void setPlaceholderText (String text) { + for (int i = 0; i< digits; ++i){ + ((TextView) getChildAt(i)).setText(String.valueOf(text.charAt(i))); + } + } + /** * Clear pin input */ From 16daac9cce8fbea52c752067613d69ad92a9536e Mon Sep 17 00:00:00 2001 From: Nicolas Carrasco-Stevenson Date: Fri, 15 Jan 2016 11:11:35 -0300 Subject: [PATCH 2/3] Keep the placeholder visible while the user types --- .../java/me/philio/pinentry/PinEntryView.java | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/pinentry/src/main/java/me/philio/pinentry/PinEntryView.java b/pinentry/src/main/java/me/philio/pinentry/PinEntryView.java index 0b932aa..17970cc 100644 --- a/pinentry/src/main/java/me/philio/pinentry/PinEntryView.java +++ b/pinentry/src/main/java/me/philio/pinentry/PinEntryView.java @@ -85,6 +85,10 @@ public class PinEntryView extends ViewGroup { */ private String mask = "*"; + /** + * String to store the placeholder, if any + */ + private String placeholder; /** * Edit text to handle input */ @@ -180,7 +184,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); } } @@ -284,11 +288,13 @@ public void setText(CharSequence text) { /** * Set a placeholder text to digitViews + * * @param text */ - public void setPlaceholderText (String text) { - for (int i = 0; i< digits; ++i){ - ((TextView) getChildAt(i)).setText(String.valueOf(text.charAt(i))); + public void setPlaceholderText(String text) { + placeholder = text; + for (int i = 0; i < digits; ++i) { + ((TextView) getChildAt(i)).setText(String.valueOf(placeholder.charAt(i))); } } @@ -305,7 +311,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); @@ -364,7 +370,13 @@ public void afterTextChanged(Editable s) { String.valueOf(s.charAt(i)) : PinEntryView.this.mask; ((TextView) getChildAt(i)).setText(mask); } else { - ((TextView) getChildAt(i)).setText(""); + if (placeholder == null) { + ((TextView) getChildAt(i)).setText(""); + } + else { + ((TextView) getChildAt(i)) + .setText(String.valueOf(placeholder.charAt(i))); + } } if (editText.hasFocus()) { getChildAt(i).setSelected(accentType == ACCENT_ALL || From 745236f3e56d1d7dd388979c3d7bf99b95bd522d Mon Sep 17 00:00:00 2001 From: Nicolas Carrasco-Stevenson Date: Fri, 15 Jan 2016 11:45:57 -0300 Subject: [PATCH 3/3] Set a different color for the placeholder text This is a non-editable color for the time being, defaults to material light secondary text. --- .../main/java/me/philio/pinentry/PinEntryView.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/pinentry/src/main/java/me/philio/pinentry/PinEntryView.java b/pinentry/src/main/java/me/philio/pinentry/PinEntryView.java index 17970cc..71f5294 100644 --- a/pinentry/src/main/java/me/philio/pinentry/PinEntryView.java +++ b/pinentry/src/main/java/me/philio/pinentry/PinEntryView.java @@ -71,6 +71,7 @@ public class PinEntryView extends ViewGroup { private int digitSpacing; private int digitTextSize; private int digitTextColor; + private int digitPlaceholderTextColor; private int digitElevation; /** @@ -155,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) { @@ -294,7 +299,9 @@ public void setText(CharSequence text) { public void setPlaceholderText(String text) { placeholder = text; for (int i = 0; i < digits; ++i) { - ((TextView) getChildAt(i)).setText(String.valueOf(placeholder.charAt(i))); + TextView view = (TextView) getChildAt(i); + view.setText(String.valueOf(placeholder.charAt(i))); + view.setTextColor(digitPlaceholderTextColor); } } @@ -369,13 +376,14 @@ 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 { if (placeholder == null) { ((TextView) getChildAt(i)).setText(""); - } - else { + } else { ((TextView) getChildAt(i)) .setText(String.valueOf(placeholder.charAt(i))); + ((TextView) getChildAt(i)).setTextColor(digitPlaceholderTextColor); } } if (editText.hasFocus()) {