Skip to content

Commit 4f9fe62

Browse files
authored
Merge pull request #7 from swapnil1104/completion_callback
Completion callback
2 parents 07f52af + d88628f commit 4f9fe62

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

app/src/main/java/com/broooapps/otpedittext/MainActivity.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,27 @@
55
import android.view.View;
66
import android.widget.EditText;
77
import android.widget.TextView;
8+
import android.widget.Toast;
9+
10+
import com.broooapps.otpedittext2.OnCompleteListener;
11+
import com.broooapps.otpedittext2.OtpEditText;
812

913
public class MainActivity extends AppCompatActivity {
1014

1115
@Override
1216
protected void onCreate(Bundle savedInstanceState) {
1317
super.onCreate(savedInstanceState);
1418
setContentView(R.layout.activity_main);
19+
20+
OtpEditText editText = findViewById(R.id.top);
21+
22+
editText.setOnCompleteListener(new OnCompleteListener() {
23+
@Override
24+
public void onComplete(String value) {
25+
Toast.makeText(MainActivity.this, "Completed " + value, Toast.LENGTH_SHORT).show();
26+
}
27+
});
28+
1529
}
1630

1731
public void displayText(View view) {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.broooapps.otpedittext2;
2+
3+
/**
4+
* Created by Swapnil Tiwari on 2019-07-28.
5+
* swapnil.tiwari@box8.in
6+
*/
7+
public interface OnCompleteListener {
8+
9+
void onComplete(String value);
10+
11+
}

otpedittext2/src/main/java/com/broooapps/otpedittext2/OtpEditText.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public class OtpEditText extends AppCompatEditText {
5151
private final String SQUARE_BOX = "square_box";
5252
private final String ROUNDED_UNDERLINE = "rounded_underline";
5353

54+
private OnCompleteListener completeListener;
55+
5456
public OtpEditText(Context context) {
5557
super(context);
5658
}
@@ -228,6 +230,11 @@ protected void onDraw(Canvas canvas) {
228230
} else {
229231
canvas.drawText(text, i, i + 1, middle - textWidths[0] / 2, mLineSpacing, getPaint());
230232
}
233+
if (i + 1 == mNumChars) {
234+
if (completeListener != null) {
235+
completeListener.onComplete(String.valueOf(getText()));
236+
}
237+
}
231238
}
232239

233240
if (mSpace < 0) {
@@ -263,4 +270,8 @@ private void updateColorForLines(boolean next, boolean current) {
263270
mStrokePaint.setColor(mPrimaryColor);
264271
}
265272
}
273+
274+
public void setOnCompleteListener(OnCompleteListener listener) {
275+
completeListener = listener;
276+
}
266277
}

readme.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,20 @@ app:oev_mask_character="ø"
106106

107107
P.S. Please note that, in case of masking with a special character other than `*`, specify string with length one, otherwise the input string will be truncated to length 1.
108108

109+
### OnComplete callback for the View
110+
To implement an OnComplete callback, use `setOnCompleteListener` setter method and pass on an interface implementation.
111+
eg:
112+
```
113+
editText.setOnCompleteListener(new OnCompleteListener() {
114+
@Override
115+
public void onComplete(String value) {
116+
Toast.makeText(MainActivity.this, "Completed " + value, Toast.LENGTH_SHORT).show();
117+
}
118+
});
119+
```
120+
121+
This callback will be triggered when the number of characters is equal to the `android:maxLength` value.
122+
109123
## For optimum usage; Please note.
110124
* Specify `android:textSize` according to your needs.
111125
* Specify `android:padding` according to your needs, there are no paddings drawn by default.

0 commit comments

Comments
 (0)