Skip to content

Commit 2fe52b8

Browse files
authored
Merge pull request #16 from swapnil1104/hint_support
Added Hint Support
2 parents 8428154 + 8c11604 commit 2fe52b8

File tree

6 files changed

+61
-6
lines changed

6 files changed

+61
-6
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public void onComplete(String value) {
3939
public void displayText(View view) {
4040
String otpValue = otpEditText.getOtpValue();
4141
if (otpValue != null) {
42+
otpEditText.triggerErrorAnimation();
4243
textDisplay.setText("Entered Value: " + otpEditText.getOtpValue());
4344
}
4445
}

app/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
android:digits="0123456789"
2626
android:maxLength="6"
2727
android:padding="32dp"
28-
android:hint="hi"
28+
android:hint="126786786"
2929
android:textSize="30sp"
3030
app:layout_constraintTop_toBottomOf="@+id/textView"
3131
app:oev_box_style="@string/style_rounded_underline"

images/hint.gif

11.1 KB
Loading

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

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44
* Created by Swapnil Tiwari on 2019-05-07.
55
* swapniltiwari775@gmail.com
66
*/
7+
78
import android.content.Context;
89
import android.content.res.TypedArray;
910
import android.graphics.Canvas;
1011
import android.graphics.Paint;
1112

13+
import androidx.annotation.ColorRes;
1214
import androidx.annotation.Nullable;
1315
import androidx.core.content.ContextCompat;
1416
import androidx.appcompat.widget.AppCompatEditText;
17+
1518
import android.text.Editable;
1619
import android.text.TextWatcher;
1720
import android.util.AttributeSet;
@@ -29,6 +32,7 @@ public class OtpEditText extends AppCompatEditText implements TextWatcher {
2932
private Paint mLinesPaint;
3033
private Paint mStrokePaint;
3134
private Paint mTextPaint;
35+
private Paint mHintPaint;
3236

3337
private boolean mMaskInput;
3438

@@ -37,6 +41,7 @@ public class OtpEditText extends AppCompatEditText implements TextWatcher {
3741
private int mPrimaryColor;
3842
private int mSecondaryColor;
3943
private int mTextColor;
44+
private int mHintTextColor;
4045

4146
private float mLineStrokeSelected = 2; //2dp by default
4247
private float mLineStroke = 1; //1dp by default
@@ -47,12 +52,16 @@ public class OtpEditText extends AppCompatEditText implements TextWatcher {
4752

4853
private String mBoxStyle;
4954
private String mMaskCharacter = "*";
55+
private String mHintText = "";
5056

5157
private final String ROUNDED_BOX = "rounded_box";
5258
private final String UNDERLINE = "underline";
5359
private final String SQUARE_BOX = "square_box";
5460
private final String ROUNDED_UNDERLINE = "rounded_underline";
5561

62+
float[] textWidths;
63+
float[] hintWidth = new float[1];
64+
5665
private OnCompleteListener completeListener;
5766

5867
public OtpEditText(Context context) {
@@ -79,6 +88,9 @@ private void init(Context context, @Nullable AttributeSet attrs) {
7988
mTextPaint = getPaint();
8089
mTextPaint.setColor(mTextColor);
8190

91+
mHintPaint = new Paint(getPaint());
92+
mHintPaint.setColor(mHintTextColor);
93+
8294
// Set the TextWatcher
8395
this.addTextChangedListener(this);
8496
float multi = context.getResources().getDisplayMetrics().density;
@@ -126,6 +138,8 @@ private void getAttrsFromTypedArray(AttributeSet attributeSet) {
126138
final TypedArray a = getContext().obtainStyledAttributes(attributeSet, R.styleable.OtpEditText, defStyleAttr, 0);
127139

128140
mMaxLength = attributeSet.getAttributeIntValue(XML_NAMESPACE_ANDROID, "maxLength", 6);
141+
mHintText = attributeSet.getAttributeValue(XML_NAMESPACE_ANDROID, "hint");
142+
mHintTextColor = attributeSet.getAttributeIntValue(XML_NAMESPACE_ANDROID, "textColorHint", getResources().getColor(R.color.hint_color));
129143
mPrimaryColor = a.getColor(R.styleable.OtpEditText_oev_primary_color, getResources().getColor(android.R.color.holo_red_dark));
130144
mSecondaryColor = a.getColor(R.styleable.OtpEditText_oev_secondary_color, getResources().getColor(R.color.light_gray));
131145
mTextColor = a.getColor(R.styleable.OtpEditText_oev_text_color, getResources().getColor(android.R.color.black));
@@ -174,13 +188,17 @@ private void getAttrsFromTypedArray(AttributeSet attributeSet) {
174188
@Nullable
175189
public String getOtpValue() {
176190
if (String.valueOf(getText()).length() != mMaxLength) {
177-
this.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.shake));
191+
triggerErrorAnimation();
178192
return null;
179193
} else {
180194
return String.valueOf(getText());
181195
}
182196
}
183197

198+
public void triggerErrorAnimation() {
199+
this.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.shake));
200+
}
201+
184202
@Override
185203
public void setCustomSelectionActionModeCallback(ActionMode.Callback actionModeCallback) {
186204
throw new RuntimeException("setCustomSelectionActionModeCallback() not supported.");
@@ -198,13 +216,32 @@ protected void onDraw(Canvas canvas) {
198216
mLineSpacing = (float) (getHeight() * .6);
199217

200218
int startX = getPaddingLeft();
219+
int hintStartX = getPaddingLeft();
201220
int bottom = getHeight() - getPaddingBottom();
202221
int top = getPaddingTop();
203222

204223
//Text Width
205224
Editable text = getText();
225+
226+
206227
int textLength = text.length();
207-
float[] textWidths = new float[textLength];
228+
textWidths = new float[textLength];
229+
230+
231+
if (text.length() == 0 && !mHintText.isEmpty()) {
232+
getPaint().getTextWidths("1", 0, 1, hintWidth);
233+
for (int i = 0; i < mNumChars && i < mHintText.length(); i++) {
234+
float middle = hintStartX + mCharSize / 2;
235+
canvas.drawText(mHintText, i, i + 1, middle - hintWidth[0] / 2, mLineSpacing, mHintPaint);
236+
237+
if (mSpace < 0) {
238+
hintStartX += mCharSize * 2;
239+
} else {
240+
hintStartX += mCharSize + mSpace;
241+
}
242+
}
243+
}
244+
208245
getPaint().getTextWidths(getText(), 0, textLength, textWidths);
209246

210247
for (int i = 0; i < mNumChars; i++) {
@@ -240,9 +277,9 @@ protected void onDraw(Canvas canvas) {
240277
if (getText().length() > i) {
241278
float middle = startX + mCharSize / 2;
242279
if (mMaskInput) {
243-
canvas.drawText(getMaskText(), i, i + 1, middle - textWidths[0] / 2, mLineSpacing, getPaint());
280+
canvas.drawText(getMaskText(), i, i + 1, middle - textWidths[0] / 2, mLineSpacing, mTextPaint);
244281
} else {
245-
canvas.drawText(text, i, i + 1, middle - textWidths[0] / 2, mLineSpacing, getPaint());
282+
canvas.drawText(text, i, i + 1, middle - textWidths[0] / 2, mLineSpacing, mTextPaint);
246283
}
247284
}
248285

@@ -251,6 +288,7 @@ protected void onDraw(Canvas canvas) {
251288
} else {
252289
startX += mCharSize + mSpace;
253290
}
291+
254292
}
255293
}
256294

otpedittext2/src/main/res/values/colors.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
<resources>
33
<color name="light_gray">#eeeeee</color>
44
<color name="underline_gray">#ECD0D0</color>
5+
<color name="hint_color">#BBBBBB</color>
56
</resources>

readme.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@ With stunning animation, and high customizability.
1010
![Demo with underline](images/demo2.gif)
1111
![Dark theme demo](images/dark_theme_demo.gif)
1212
![Error animation](images/err_anim.gif)
13+
![Hint usage](images/hint.gif)
1314

1415
## Packed with features
16+
1517
- Add custom character limit.
18+
- Animation supported on wrong input
1619
- Use your own color scheme.
1720
- Do not allow user changing cursor position for smooth functioning.
18-
21+
- Hint is supported!
1922

2023
## How to integrate the library in your app?
2124
Step 1: Add it in your root build.gradle at the end of repositories:
@@ -65,6 +68,18 @@ This method when invoked, will either return the OTP value, in case this is invo
6568

6669

6770
## How to customize the view.
71+
72+
### Trigger Error Animation
73+
To trigger error animation for incorrect input, or any other use case, use.
74+
```
75+
OtpEditText otpEditText;
76+
....
77+
otpEditText = findViewById(R.id.oev_view);
78+
....
79+
//Invalid input, animation triggered
80+
otpEditText.triggerErrorAnimation();
81+
```
82+
6883
### Setting desired length for the OTP(One time password code)
6984

7085
To set custom length of the OtpEditText, use

0 commit comments

Comments
 (0)