From b800a464721cbad55330ae9fc8c876859f37435a Mon Sep 17 00:00:00 2001 From: Aziz Ahmed Date: Tue, 18 Jun 2019 21:27:02 +0600 Subject: [PATCH] [Add] onAccepted and onRejected on PinViewEventListener. [Add] different background for accepted and rejected. --- README.md | 30 +++++++++++++++- .../pinview/example/MainActivity.java | 19 +++++++++- .../drawable/example_accepted_drawable.xml | 9 +++++ .../drawable/example_rejected_drawable.xml | 9 +++++ example/src/main/res/layout/activity_main.xml | 10 ++++++ .../java/com/goodiebag/pinview/Pinview.java | 35 ++++++++++++++++++- .../drawable/sample_accepted_background.xml | 9 +++++ .../drawable/sample_rejected_background.xml | 9 +++++ pinview/src/main/res/values/attrs.xml | 2 ++ 9 files changed, 129 insertions(+), 3 deletions(-) create mode 100644 example/src/main/res/drawable/example_accepted_drawable.xml create mode 100644 example/src/main/res/drawable/example_rejected_drawable.xml create mode 100644 pinview/src/main/res/drawable/sample_accepted_background.xml create mode 100644 pinview/src/main/res/drawable/sample_rejected_background.xml diff --git a/README.md b/README.md index effea67..897a775 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,8 @@ Sync the gradle and that's it! :+1: +```app:pinAcceptedBackground``` : Sets the pin box's background when treat as accepted, accepts a drawable or a selector drawable. When a ```selector``` is used, the focused pin box is highlighted.
+```app:pinRejectedBackground``` : Sets the pin box's background when treat as rejected, accepts a drawable or a selector drawable. When a ```selector``` is used, the focused pin box is highlighted.
```app:pinWidth``` and ```app:pinHeight``` : Sets the width and height of the pinbox.
```app:pinLength``` : number of pin boxes to be displayed.
```app:forceKeyboard``` : forces the keyboard when the pinview is activity/fragment is opened. @@ -74,6 +78,8 @@ Or reference it from findViewById ```java pin = (Pinview) findViewById(R.id.pinview); pin.setPinBackgroundRes(R.drawable.sample_background); +pin.setPinAcceptedBackground(R.drawable.sample_background); +pin.setPinRejectedBackground(R.drawable.sample_background); pin.setPinHeight(40); pin.setPinWidth(40); pin.setInputType(Pinview.InputType.NUMBER); @@ -87,11 +93,33 @@ There is an event listener which is triggered when the user is done entering the pinview.setPinViewEventListener(new Pinview.PinViewEventListener() { @Override public void onDataEntered(Pinview pinview, boolean fromUser) { - //Make api calls here or what not Toast.makeText(MainActivity.this, pinview.getValue(), Toast.LENGTH_SHORT).show(); + + //test or check accepted rejected + if(pinview1.getValue().equals("1234")){ + pinview1.setAccepted(); + } else { + pinview1.setRejected(); + } + } + + @Override + public void onAccepted() { + //do something on accepted + } + + @Override + public void onRejected() { + //do something on rejected } }); ``` + +```java +pinview.setAccepted(); //on Aceept + +pinview.setRejected(); //on Reject +``` #### Note : This library cannot be assured to work on 3rd party keyboards (especially when the cursor is off). It works as expected on google keyboards. We will be adding a work-around in the future releases. diff --git a/example/src/main/java/com/goodiebag/pinview/example/MainActivity.java b/example/src/main/java/com/goodiebag/pinview/example/MainActivity.java index 715de7d..e5f0946 100644 --- a/example/src/main/java/com/goodiebag/pinview/example/MainActivity.java +++ b/example/src/main/java/com/goodiebag/pinview/example/MainActivity.java @@ -13,11 +13,28 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - Pinview pinview1 = findViewById(R.id.pinview1); + final Pinview pinview1 = findViewById(R.id.pinview1); pinview1.setPinViewEventListener(new Pinview.PinViewEventListener() { @Override public void onDataEntered(Pinview pinview, boolean fromUser) { Toast.makeText(MainActivity.this, pinview.getValue(), Toast.LENGTH_SHORT).show(); + + //test or check accepted rejected + if(pinview1.getValue().equals("1234")){ + pinview1.setAccepted(); + } else { + pinview1.setRejected(); + } + } + + @Override + public void onAccepted() { + + } + + @Override + public void onRejected() { + } }); diff --git a/example/src/main/res/drawable/example_accepted_drawable.xml b/example/src/main/res/drawable/example_accepted_drawable.xml new file mode 100644 index 0000000..e6ce738 --- /dev/null +++ b/example/src/main/res/drawable/example_accepted_drawable.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file diff --git a/example/src/main/res/drawable/example_rejected_drawable.xml b/example/src/main/res/drawable/example_rejected_drawable.xml new file mode 100644 index 0000000..74ebfce --- /dev/null +++ b/example/src/main/res/drawable/example_rejected_drawable.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file diff --git a/example/src/main/res/layout/activity_main.xml b/example/src/main/res/layout/activity_main.xml index 254eaf0..f99970f 100644 --- a/example/src/main/res/layout/activity_main.xml +++ b/example/src/main/res/layout/activity_main.xml @@ -26,6 +26,8 @@ app:inputType="number" app:password="false" app:pinBackground="@drawable/example_drawable_with_grey_disabled" + app:pinAcceptedBackground="@drawable/example_accepted_drawable" + app:pinRejectedBackground="@drawable/example_rejected_drawable" app:pinHeight="40dp" app:pinLength="4" app:pinWidth="40dp"/> @@ -45,6 +47,8 @@ app:inputType="text" app:password="false" app:pinBackground="@drawable/example_drawable" + app:pinAcceptedBackground="@drawable/example_accepted_drawable" + app:pinRejectedBackground="@drawable/example_rejected_drawable" app:pinHeight="40dp" app:pinLength="4" app:pinWidth="40dp"/> @@ -63,6 +67,8 @@ app:inputType="number" app:password="true" app:pinBackground="@drawable/example_drawable" + app:pinAcceptedBackground="@drawable/example_accepted_drawable" + app:pinRejectedBackground="@drawable/example_rejected_drawable" app:pinHeight="40dp" app:pinLength="4" app:pinWidth="40dp"/> @@ -81,6 +87,8 @@ app:inputType="text" app:password="true" app:pinBackground="@drawable/example_drawable" + app:pinAcceptedBackground="@drawable/example_accepted_drawable" + app:pinRejectedBackground="@drawable/example_rejected_drawable" app:pinHeight="40dp" app:pinLength="4" app:pinWidth="40dp"/> @@ -94,6 +102,8 @@ android:id="@+id/pinview5" android:layout_width="match_parent" android:layout_height="wrap_content" + app:pinAcceptedBackground="@drawable/example_accepted_drawable" + app:pinRejectedBackground="@drawable/example_rejected_drawable" app:inputType="text" app:pinHeight="40dp" app:pinLength="4" diff --git a/pinview/src/main/java/com/goodiebag/pinview/Pinview.java b/pinview/src/main/java/com/goodiebag/pinview/Pinview.java index de6054a..3d20e00 100644 --- a/pinview/src/main/java/com/goodiebag/pinview/Pinview.java +++ b/pinview/src/main/java/com/goodiebag/pinview/Pinview.java @@ -66,6 +66,7 @@ of this software and associated documentation files (the "Software"), to deal * @author Krishanu * @author Pavan * @author Koushik + * @author aziz */ public class Pinview extends LinearLayout implements TextWatcher, View.OnFocusChangeListener, View.OnKeyListener { private final float DENSITY = getContext().getResources().getDisplayMetrics().density; @@ -82,6 +83,10 @@ public class Pinview extends LinearLayout implements TextWatcher, View.OnFocusCh private boolean mDelPressed = false; @DrawableRes private int mPinBackground = R.drawable.sample_background; + @DrawableRes + private int mPinAcceptedBackground = R.drawable.sample_accepted_background; + @DrawableRes + private int mPinRejectedBackground = R.drawable.sample_rejected_background; private boolean mPassword = false; private String mHint = ""; private InputType inputType = InputType.TEXT; @@ -95,11 +100,15 @@ public enum InputType { } /** - * Interface for onDataEntered event. + * Interface for onDataEntered, onAccepted and onRejected event. */ public interface PinViewEventListener { void onDataEntered(Pinview pinview, boolean fromUser); + + void onAccepted(); + + void onRejected(); } OnClickListener mClickListener; @@ -202,6 +211,8 @@ private void initAttributes(Context context, AttributeSet attrs, int defStyleAtt if (attrs != null) { final TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.Pinview, defStyleAttr, 0); mPinBackground = array.getResourceId(R.styleable.Pinview_pinBackground, mPinBackground); + mPinAcceptedBackground = array.getResourceId(R.styleable.Pinview_pinAcceptedBackground, mPinAcceptedBackground); + mPinRejectedBackground = array.getResourceId(R.styleable.Pinview_pinRejectedBackground, mPinRejectedBackground); mPinLength = array.getInt(R.styleable.Pinview_pinLength, mPinLength); mPinHeight = (int) array.getDimension(R.styleable.Pinview_pinHeight, mPinHeight); mPinWidth = (int) array.getDimension(R.styleable.Pinview_pinWidth, mPinWidth); @@ -638,6 +649,18 @@ public void setPinBackgroundRes(@DrawableRes int res) { editText.setBackgroundResource(res); } + public void setPinAcceptedBackground(@DrawableRes int res){ + this.mPinAcceptedBackground = res; + for (EditText editText : editTextList) + editText.setBackgroundResource(res); + } + + public void setPinRejectedBackground(@DrawableRes int res){ + this.mPinRejectedBackground = res; + for (EditText editText : editTextList) + editText.setBackgroundResource(res); + } + @Override public void setOnClickListener(OnClickListener l) { mClickListener = l; @@ -740,4 +763,14 @@ private void setCursorColor(EditText view, @ColorInt int color) { } catch (Exception ignored) { } } + + public void setRejected(){ + setPinBackgroundRes(mPinRejectedBackground); + if(mListener != null) mListener.onRejected(); + } + + public void setAccepted(){ + setPinAcceptedBackground(mPinAcceptedBackground); + if(mListener != null) mListener.onAccepted(); + } } diff --git a/pinview/src/main/res/drawable/sample_accepted_background.xml b/pinview/src/main/res/drawable/sample_accepted_background.xml new file mode 100644 index 0000000..e6ce738 --- /dev/null +++ b/pinview/src/main/res/drawable/sample_accepted_background.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file diff --git a/pinview/src/main/res/drawable/sample_rejected_background.xml b/pinview/src/main/res/drawable/sample_rejected_background.xml new file mode 100644 index 0000000..74ebfce --- /dev/null +++ b/pinview/src/main/res/drawable/sample_rejected_background.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file diff --git a/pinview/src/main/res/values/attrs.xml b/pinview/src/main/res/values/attrs.xml index 67169de..8ae8938 100644 --- a/pinview/src/main/res/values/attrs.xml +++ b/pinview/src/main/res/values/attrs.xml @@ -2,6 +2,8 @@ + +