Skip to content

Commit 3da72cd

Browse files
committed
Merge branch 'master' of https://github.com/swapnil1104/OtpEditText into 1_hide_input_text
2 parents 73f1b27 + 952dc15 commit 3da72cd

File tree

9 files changed

+211
-36
lines changed

9 files changed

+211
-36
lines changed

CODE_OF_CONDUCT.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Contributor Covenant Code of Conduct
2+
3+
## Our Pledge
4+
5+
In the interest of fostering an open and welcoming environment, we as
6+
contributors and maintainers pledge to making participation in our project and
7+
our community a harassment-free experience for everyone, regardless of age, body
8+
size, disability, ethnicity, sex characteristics, gender identity and expression,
9+
level of experience, education, socio-economic status, nationality, personal
10+
appearance, race, religion, or sexual identity and orientation.
11+
12+
## Our Standards
13+
14+
Examples of behavior that contributes to creating a positive environment
15+
include:
16+
17+
* Using welcoming and inclusive language
18+
* Being respectful of differing viewpoints and experiences
19+
* Gracefully accepting constructive criticism
20+
* Focusing on what is best for the community
21+
* Showing empathy towards other community members
22+
23+
Examples of unacceptable behavior by participants include:
24+
25+
* The use of sexualized language or imagery and unwelcome sexual attention or
26+
advances
27+
* Trolling, insulting/derogatory comments, and personal or political attacks
28+
* Public or private harassment
29+
* Publishing others' private information, such as a physical or electronic
30+
address, without explicit permission
31+
* Other conduct which could reasonably be considered inappropriate in a
32+
professional setting
33+
34+
## Our Responsibilities
35+
36+
Project maintainers are responsible for clarifying the standards of acceptable
37+
behavior and are expected to take appropriate and fair corrective action in
38+
response to any instances of unacceptable behavior.
39+
40+
Project maintainers have the right and responsibility to remove, edit, or
41+
reject comments, commits, code, wiki edits, issues, and other contributions
42+
that are not aligned to this Code of Conduct, or to ban temporarily or
43+
permanently any contributor for other behaviors that they deem inappropriate,
44+
threatening, offensive, or harmful.
45+
46+
## Scope
47+
48+
This Code of Conduct applies both within project spaces and in public spaces
49+
when an individual is representing the project or its community. Examples of
50+
representing a project or community include using an official project e-mail
51+
address, posting via an official social media account, or acting as an appointed
52+
representative at an online or offline event. Representation of a project may be
53+
further defined and clarified by project maintainers.
54+
55+
## Enforcement
56+
57+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
58+
reported by contacting the project team at swapniltiwari775@gmail.com. All
59+
complaints will be reviewed and investigated and will result in a response that
60+
is deemed necessary and appropriate to the circumstances. The project team is
61+
obligated to maintain confidentiality with regard to the reporter of an incident.
62+
Further details of specific enforcement policies may be posted separately.
63+
64+
Project maintainers who do not follow or enforce the Code of Conduct in good
65+
faith may face temporary or permanent repercussions as determined by other
66+
members of the project's leadership.
67+
68+
## Attribution
69+
70+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71+
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
72+
73+
[homepage]: https://www.contributor-covenant.org
74+
75+
For answers to common questions about this code of conduct, see
76+
https://www.contributor-covenant.org/faq

LICENSE.MD

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Swapnil Tiwari
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@
2121
android:clickable="false"
2222
android:cursorVisible="false"
2323
android:digits="0123456789"
24-
android:padding="32dp"
2524
android:inputType="number"
26-
app:oev_primary_color="@android:color/holo_blue_dark"
27-
app:oev_secondary_color="@android:color/holo_orange_light"
2825
android:maxLength="6"
26+
android:padding="32dp"
2927
android:textSize="30sp"
3028
app:layout_constraintTop_toTopOf="parent" />
3129

images/demo2.gif

299 KB
Loading

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

Lines changed: 82 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@
88
import android.content.Context;
99
import android.content.res.TypedArray;
1010
import android.graphics.Canvas;
11-
import android.graphics.Color;
1211
import android.graphics.Paint;
1312
import android.support.v4.content.ContextCompat;
1413
import android.support.v7.widget.AppCompatEditText;
1514
import android.text.Editable;
16-
import android.text.TextPaint;
1715
import android.util.AttributeSet;
1816
import android.view.ActionMode;
1917
import android.view.Menu;
@@ -23,25 +21,31 @@
2321

2422
public class OtpEditText extends AppCompatEditText {
2523
public static final String XML_NAMESPACE_ANDROID = "http://schemas.android.com/apk/res/android";
26-
private int defStyleAttr = 0;
27-
28-
private float mSpace = 8; //24 dp by default, space between the lines
29-
private float mCharSize;
30-
private float mNumChars = 6;
31-
private float mLineSpacing = 10; //8dp by default, height of the text from our lines
32-
private int mMaxLength = 6;
3324

3425
private OnClickListener mClickListener;
3526

36-
private float mLineStroke = 1; //1dp by default
37-
private float mLineStrokeSelected = 2; //2dp by default
3827
private Paint mLinesPaint;
28+
private Paint mStrokePaint;
3929

40-
private int mMainColor;
30+
private int defStyleAttr = 0;
31+
private int mMaxLength = 6;
32+
private int mPrimaryColor;
4133
private int mSecondaryColor;
4234
private int mTextColor;
4335

44-
private Paint mStrokePaint;
36+
private float mLineStrokeSelected = 2; //2dp by default
37+
private float mLineStroke = 1; //1dp by default
38+
private float mSpace = 8; //24 dp by default, space between the lines
39+
private float mCharSize;
40+
private float mNumChars = 6;
41+
private float mLineSpacing = 10; //8dp by default, height of the text from our lines
42+
43+
private String mBoxStyle;
44+
45+
private final String ROUNDED_BOX = "rounded_box";
46+
private final String UNDERLINE = "underline";
47+
private final String SQUARE_BOX = "square_box";
48+
private final String ROUNDED_UNDERLINE = "rounded_underline";
4549

4650
public OtpEditText(Context context) {
4751
super(context);
@@ -65,11 +69,10 @@ private void init(Context context, AttributeSet attrs) {
6569
float multi = context.getResources().getDisplayMetrics().density;
6670
mLineStroke = multi * mLineStroke;
6771
mLineStrokeSelected = multi * mLineStrokeSelected;
72+
6873
mLinesPaint = new Paint(getPaint());
69-
mStrokePaint = new Paint(getPaint());
70-
mStrokePaint.setStrokeWidth(4);
71-
mStrokePaint.setStyle(Paint.Style.STROKE);
7274
mLinesPaint.setStrokeWidth(mLineStroke);
75+
7376
setBackgroundResource(0);
7477
mSpace = multi * mSpace; //convert to pixels for our density
7578
mNumChars = mMaxLength;
@@ -108,9 +111,41 @@ private void getAttrsFromTypedArray(AttributeSet attributeSet) {
108111
final TypedArray a = getContext().obtainStyledAttributes(attributeSet, R.styleable.OtpEditText, defStyleAttr, 0);
109112

110113
mMaxLength = attributeSet.getAttributeIntValue(XML_NAMESPACE_ANDROID, "maxLength", 4);
111-
mMainColor = a.getColor(R.styleable.OtpEditText_oev_primary_color, getResources().getColor(android.R.color.holo_red_dark));
114+
mPrimaryColor = a.getColor(R.styleable.OtpEditText_oev_primary_color, getResources().getColor(android.R.color.holo_red_dark));
112115
mSecondaryColor = a.getColor(R.styleable.OtpEditText_oev_secondary_color, getResources().getColor(R.color.light_gray));
113116
mTextColor = a.getColor(R.styleable.OtpEditText_oev_text_color, getResources().getColor(android.R.color.black));
117+
mBoxStyle = a.getString(R.styleable.OtpEditText_oev_box_style);
118+
119+
if (mBoxStyle != null && !mBoxStyle.isEmpty()) {
120+
switch (mBoxStyle) {
121+
case UNDERLINE:
122+
case ROUNDED_UNDERLINE:
123+
mStrokePaint = new Paint(getPaint());
124+
mStrokePaint.setStrokeWidth(4);
125+
mStrokePaint.setStyle(Paint.Style.FILL);
126+
break;
127+
128+
case SQUARE_BOX:
129+
case ROUNDED_BOX:
130+
mStrokePaint = new Paint(getPaint());
131+
mStrokePaint.setStrokeWidth(4);
132+
mStrokePaint.setStyle(Paint.Style.STROKE);
133+
break;
134+
135+
default:
136+
mStrokePaint = new Paint(getPaint());
137+
mStrokePaint.setStrokeWidth(4);
138+
mStrokePaint.setStyle(Paint.Style.FILL);
139+
140+
mBoxStyle = UNDERLINE;
141+
}
142+
} else {
143+
mStrokePaint = new Paint(getPaint());
144+
mStrokePaint.setStrokeWidth(4);
145+
mStrokePaint.setStyle(Paint.Style.FILL);
146+
147+
mBoxStyle = UNDERLINE;
148+
}
114149

115150
a.recycle();
116151
}
@@ -147,15 +182,34 @@ protected void onDraw(Canvas canvas) {
147182
getPaint().getTextWidths(getText(), 0, textLength, textWidths);
148183

149184
for (int i = 0; i < mNumChars; i++) {
150-
updateColorForLines(i <= textLength, i == textLength, getText().length(), (int) mNumChars);
151-
canvas.drawLine(startX, bottom, startX + mCharSize, bottom, mLinesPaint);
152-
153-
try {
154-
canvas.drawRoundRect(startX, top, startX + mCharSize, bottom, 8, 8, mLinesPaint);
155-
canvas.drawRoundRect(startX, top, startX + mCharSize, bottom, 8, 8, mStrokePaint);
156-
} catch (NoSuchMethodError err) {
157-
canvas.drawRect(startX, top, startX + mCharSize, bottom, mLinesPaint);
158-
canvas.drawRect(startX, top, startX + mCharSize, bottom, mStrokePaint);
185+
updateColorForLines(i <= textLength, i == textLength);
186+
187+
switch (mBoxStyle) {
188+
case ROUNDED_UNDERLINE:
189+
try {
190+
canvas.drawRoundRect(startX, bottom * .95f, startX + mCharSize, bottom, 16, 16, mStrokePaint);
191+
} catch (NoSuchMethodError err) {
192+
canvas.drawRect(startX, bottom * .95f, startX + mCharSize, bottom, mStrokePaint);
193+
}
194+
break;
195+
case ROUNDED_BOX:
196+
try {
197+
canvas.drawRoundRect(startX, top, startX + mCharSize, bottom, 8, 8, mLinesPaint);
198+
canvas.drawRoundRect(startX, top, startX + mCharSize, bottom, 8, 8, mStrokePaint);
199+
} catch (NoSuchMethodError err) {
200+
canvas.drawRect(startX, top, startX + mCharSize, bottom, mLinesPaint);
201+
canvas.drawRect(startX, top, startX + mCharSize, bottom, mStrokePaint);
202+
}
203+
break;
204+
205+
case UNDERLINE:
206+
canvas.drawRect(startX, ((float) bottom * .95f), startX + mCharSize, bottom, mStrokePaint);
207+
break;
208+
209+
case SQUARE_BOX:
210+
canvas.drawRect(startX, top, startX + mCharSize, bottom, mLinesPaint);
211+
canvas.drawRect(startX, top, startX + mCharSize, bottom, mStrokePaint);
212+
break;
159213
}
160214
if (getText().length() > i) {
161215
float middle = startX + mCharSize / 2;
@@ -173,7 +227,7 @@ protected void onDraw(Canvas canvas) {
173227
/**
174228
* @param next Is the current char the next character to be input?
175229
*/
176-
private void updateColorForLines(boolean next, boolean current, int textSize, int totalSize) {
230+
private void updateColorForLines(boolean next, boolean current) {
177231
if (next) {
178232
mStrokePaint.setColor(mSecondaryColor);
179233
mLinesPaint.setColor(mSecondaryColor);
@@ -183,7 +237,7 @@ private void updateColorForLines(boolean next, boolean current, int textSize, in
183237
}
184238
if (current) {
185239
mLinesPaint.setColor(ContextCompat.getColor(getContext(), android.R.color.white));
186-
mStrokePaint.setColor(mMainColor);
240+
mStrokePaint.setColor(mPrimaryColor);
187241
}
188242
}
189243
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
<attr name="oev_primary_color" format="color" />
66
<attr name="oev_secondary_color" format="color" />
77
<attr name="oev_text_color" format="color" />
8+
<attr name="oev_box_style" format="string" />
9+
810
</declare-styleable>
911

1012

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
33
<color name="light_gray">#eeeeee</color>
4+
<color name="underline_gray">#ECD0D0</color>
45
</resources>
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
<resources>
22
<string name="app_name">OtpEditText</string>
3-
<string name="random_name">ABCDE</string>
3+
4+
<string name="style_square">square_box</string>
5+
<string name="style_rounded">rounded_box</string>
6+
<string name="style_underline">underline</string>
7+
<string name="style_rounded_underline">rounded_underline</string>
8+
49
</resources>

readme.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# OtpEditText
1+
# PassCodeText
22

33
[![](https://jitpack.io/v/swapnil1104/OtpEditText.svg)](https://jitpack.io/#swapnil1104/OtpEditText)
4+
[![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-OtpEditText-brightgreen.svg?style=flat)](https://android-arsenal.com/details/1/7666)
45

56
A customised EditText view serving the purpose of taking numeric **One Time Password** from a user.
67
With stunning animation, and high customizability.
78

8-
![Demo1](images/demo.gif)
9-
![Demo with other colors](images/demo1.gif)
9+
![Demo with underline](images/demo2.gif)
1010

1111
## Packed with features
1212
- Add custom character limit.
@@ -28,7 +28,7 @@ Step 2. Add the dependency
2828

2929
```
3030
dependencies {
31-
implementation 'com.github.swapnil1104:OtpEditText:0.0.1'
31+
implementation 'com.github.swapnil1104:OtpEditText:{current_lib_ver}'
3232
}
3333
```
3434
Step 3. Add OtpEditText to your layout file
@@ -71,6 +71,24 @@ To change that use,
7171

7272
```app:oev_secondary_color="@color/{your_color}"```
7373

74+
### Using multiple style options.
75+
There are 4 style options that are available within the library for now.
76+
- rounded box
77+
- square box
78+
- underline
79+
- rounded underline
80+
81+
To use any of these styles, please add ```app:oev_box_style="@string\{box_style_input}"```
82+
attribue.
83+
I have provided string resources for simpler usage.
84+
```
85+
<string name="style_square">square_box</string>
86+
<string name="style_rounded">rounded_box</string>
87+
<string name="style_underline">underline</string>
88+
<string name="style_rounded_underline">rounded_underline</string>
89+
```
90+
Suppose you want the rounded underline option to be displayed. Then, please add:
91+
`app:oev_box_style="@string/style_rounded_underline" ` in the OtpEditText xml code.
7492

7593
## For optimum usage; Please note.
7694
* Specify `android:textSize` according to your needs.

0 commit comments

Comments
 (0)