Skip to content

Commit

Permalink
Merge pull request #7 from swapnil1104/completion_callback
Browse files Browse the repository at this point in the history
Completion callback
  • Loading branch information
swapnil1104 authored Jul 28, 2019
2 parents 07f52af + d88628f commit 4f9fe62
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
14 changes: 14 additions & 0 deletions app/src/main/java/com/broooapps/otpedittext/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,27 @@
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.broooapps.otpedittext2.OnCompleteListener;
import com.broooapps.otpedittext2.OtpEditText;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

OtpEditText editText = findViewById(R.id.top);

editText.setOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(String value) {
Toast.makeText(MainActivity.this, "Completed " + value, Toast.LENGTH_SHORT).show();
}
});

}

public void displayText(View view) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.broooapps.otpedittext2;

/**
* Created by Swapnil Tiwari on 2019-07-28.
* [email protected]
*/
public interface OnCompleteListener {

void onComplete(String value);

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public class OtpEditText extends AppCompatEditText {
private final String SQUARE_BOX = "square_box";
private final String ROUNDED_UNDERLINE = "rounded_underline";

private OnCompleteListener completeListener;

public OtpEditText(Context context) {
super(context);
}
Expand Down Expand Up @@ -228,6 +230,11 @@ protected void onDraw(Canvas canvas) {
} else {
canvas.drawText(text, i, i + 1, middle - textWidths[0] / 2, mLineSpacing, getPaint());
}
if (i + 1 == mNumChars) {
if (completeListener != null) {
completeListener.onComplete(String.valueOf(getText()));
}
}
}

if (mSpace < 0) {
Expand Down Expand Up @@ -263,4 +270,8 @@ private void updateColorForLines(boolean next, boolean current) {
mStrokePaint.setColor(mPrimaryColor);
}
}

public void setOnCompleteListener(OnCompleteListener listener) {
completeListener = listener;
}
}
14 changes: 14 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,20 @@ app:oev_mask_character="ø"

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.

### OnComplete callback for the View
To implement an OnComplete callback, use `setOnCompleteListener` setter method and pass on an interface implementation.
eg:
```
editText.setOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(String value) {
Toast.makeText(MainActivity.this, "Completed " + value, Toast.LENGTH_SHORT).show();
}
});
```

This callback will be triggered when the number of characters is equal to the `android:maxLength` value.

## For optimum usage; Please note.
* Specify `android:textSize` according to your needs.
* Specify `android:padding` according to your needs, there are no paddings drawn by default.
Expand Down

0 comments on commit 4f9fe62

Please sign in to comment.