Skip to content

Commit 76f4856

Browse files
committed
✨ Implemented onComplete callback
1 parent fa3ea1d commit 76f4856

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-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
}

0 commit comments

Comments
 (0)