Skip to content
Open
Show file tree
Hide file tree
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
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ Sync the gradle and that's it! :+1:
<com.goodiebag.pinview.Pinview
android:id="@+id/pinview"
app:pinBackground="@drawable/example_drawable"
app:pinAcceptedBackground="@drawable/example_accepted_drawable"
app:pinRejectedBackground="@drawable/example_rejected_drawable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:pinWidth="40dp"
Expand All @@ -55,6 +57,8 @@ This can be referenced in the java class by the ```findViewById``` method.
##### Available xml attributes and explanations :

```app:pinBackground``` : Sets the pin box's background, accepts a drawable or a selector drawable. When a ```selector``` is used, the focused pin box is highlighted. <br />
```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. <br />
```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. <br />
```app:pinWidth``` and ```app:pinHeight``` : Sets the width and height of the pinbox. <br />
```app:pinLength``` : number of pin boxes to be displayed.<br />
```app:forceKeyboard``` : forces the keyboard when the pinview is activity/fragment is opened.
Expand All @@ -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);
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

}
});

Expand Down
9 changes: 9 additions & 0 deletions example/src/main/res/drawable/example_accepted_drawable.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<stroke android:color="#000" android:width="0.5dp" />
<solid android:color="#4CAF50" />
<corners
android:radius="4dp"
/>
</shape>
9 changes: 9 additions & 0 deletions example/src/main/res/drawable/example_rejected_drawable.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<stroke android:color="#000" android:width="0.5dp" />
<solid android:color="#F44336" />
<corners
android:radius="4dp"
/>
</shape>
10 changes: 10 additions & 0 deletions example/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"/>
Expand All @@ -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"/>
Expand All @@ -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"/>
Expand All @@ -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"/>
Expand All @@ -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"
Expand Down
35 changes: 34 additions & 1 deletion pinview/src/main/java/com/goodiebag/pinview/Pinview.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
9 changes: 9 additions & 0 deletions pinview/src/main/res/drawable/sample_accepted_background.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<stroke android:color="#000" android:width="0.5dp" />
<solid android:color="#4CAF50" />
<corners
android:radius="4dp"
/>
</shape>
9 changes: 9 additions & 0 deletions pinview/src/main/res/drawable/sample_rejected_background.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<stroke android:color="#000" android:width="0.5dp" />
<solid android:color="#F44336" />
<corners
android:radius="4dp"
/>
</shape>
2 changes: 2 additions & 0 deletions pinview/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<resources>
<declare-styleable name="Pinview">
<attr name="pinBackground" format="reference"/>
<attr name="pinRejectedBackground" format="reference"/>
<attr name="pinAcceptedBackground" format="reference"/>
<attr name="pinLength" format="integer"/>
<attr name="pinWidth" format="dimension"/>
<attr name="pinHeight" format="dimension"/>
Expand Down