Skip to content

Commit

Permalink
Merge pull request #1 from swapnil1104/2_underline_highlight
Browse files Browse the repository at this point in the history
added new feature which allows the OtpEditText to have multiple styles.
  • Loading branch information
swapnil1104 authored Jun 6, 2019
2 parents 5e5f713 + 8b61e2a commit a37c3ff
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 32 deletions.
7 changes: 3 additions & 4 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@
android:clickable="false"
android:cursorVisible="false"
android:digits="0123456789"
android:padding="32dp"
android:inputType="number"
app:oev_primary_color="@android:color/holo_blue_dark"
app:oev_secondary_color="@android:color/holo_orange_light"
android:maxLength="6"
android:padding="32dp"
android:textSize="30sp"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toTopOf="parent"
app:oev_box_style="@string/style_rounded_underline" />

</android.support.constraint.ConstraintLayout>
Binary file added images/demo2.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
110 changes: 82 additions & 28 deletions otpedittext2/src/main/java/com/broooapps/otpedittext2/OtpEditText.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.ActionMode;
import android.view.Menu;
Expand All @@ -23,25 +21,31 @@

public class OtpEditText extends AppCompatEditText {
public static final String XML_NAMESPACE_ANDROID = "http://schemas.android.com/apk/res/android";
private int defStyleAttr = 0;

private float mSpace = 8; //24 dp by default, space between the lines
private float mCharSize;
private float mNumChars = 6;
private float mLineSpacing = 10; //8dp by default, height of the text from our lines
private int mMaxLength = 6;

private OnClickListener mClickListener;

private float mLineStroke = 1; //1dp by default
private float mLineStrokeSelected = 2; //2dp by default
private Paint mLinesPaint;
private Paint mStrokePaint;

private int mMainColor;
private int defStyleAttr = 0;
private int mMaxLength = 6;
private int mPrimaryColor;
private int mSecondaryColor;
private int mTextColor;

private Paint mStrokePaint;
private float mLineStrokeSelected = 2; //2dp by default
private float mLineStroke = 1; //1dp by default
private float mSpace = 8; //24 dp by default, space between the lines
private float mCharSize;
private float mNumChars = 6;
private float mLineSpacing = 10; //8dp by default, height of the text from our lines

private String mBoxStyle;

private final String ROUNDED_BOX = "rounded_box";
private final String UNDERLINE = "underline";
private final String SQUARE_BOX = "square_box";
private final String ROUNDED_UNDERLINE = "rounded_underline";

public OtpEditText(Context context) {
super(context);
Expand All @@ -65,11 +69,10 @@ private void init(Context context, AttributeSet attrs) {
float multi = context.getResources().getDisplayMetrics().density;
mLineStroke = multi * mLineStroke;
mLineStrokeSelected = multi * mLineStrokeSelected;

mLinesPaint = new Paint(getPaint());
mStrokePaint = new Paint(getPaint());
mStrokePaint.setStrokeWidth(4);
mStrokePaint.setStyle(Paint.Style.STROKE);
mLinesPaint.setStrokeWidth(mLineStroke);

setBackgroundResource(0);
mSpace = multi * mSpace; //convert to pixels for our density
mNumChars = mMaxLength;
Expand Down Expand Up @@ -108,9 +111,41 @@ private void getAttrsFromTypedArray(AttributeSet attributeSet) {
final TypedArray a = getContext().obtainStyledAttributes(attributeSet, R.styleable.OtpEditText, defStyleAttr, 0);

mMaxLength = attributeSet.getAttributeIntValue(XML_NAMESPACE_ANDROID, "maxLength", 4);
mMainColor = a.getColor(R.styleable.OtpEditText_oev_primary_color, getResources().getColor(android.R.color.holo_red_dark));
mPrimaryColor = a.getColor(R.styleable.OtpEditText_oev_primary_color, getResources().getColor(android.R.color.holo_red_dark));
mSecondaryColor = a.getColor(R.styleable.OtpEditText_oev_secondary_color, getResources().getColor(R.color.light_gray));
mTextColor = a.getColor(R.styleable.OtpEditText_oev_text_color, getResources().getColor(android.R.color.black));
mBoxStyle = a.getString(R.styleable.OtpEditText_oev_box_style);

if (mBoxStyle != null && !mBoxStyle.isEmpty()) {
switch (mBoxStyle) {
case UNDERLINE:
case ROUNDED_UNDERLINE:
mStrokePaint = new Paint(getPaint());
mStrokePaint.setStrokeWidth(4);
mStrokePaint.setStyle(Paint.Style.FILL);
break;

case SQUARE_BOX:
case ROUNDED_BOX:
mStrokePaint = new Paint(getPaint());
mStrokePaint.setStrokeWidth(4);
mStrokePaint.setStyle(Paint.Style.STROKE);
break;

default:
mStrokePaint = new Paint(getPaint());
mStrokePaint.setStrokeWidth(4);
mStrokePaint.setStyle(Paint.Style.STROKE);

mBoxStyle = ROUNDED_BOX;
}
} else {
mStrokePaint = new Paint(getPaint());
mStrokePaint.setStrokeWidth(4);
mStrokePaint.setStyle(Paint.Style.STROKE);

mBoxStyle = ROUNDED_BOX;
}

a.recycle();
}
Expand Down Expand Up @@ -147,15 +182,34 @@ protected void onDraw(Canvas canvas) {
getPaint().getTextWidths(getText(), 0, textLength, textWidths);

for (int i = 0; i < mNumChars; i++) {
updateColorForLines(i <= textLength, i == textLength, getText().length(), (int) mNumChars);
canvas.drawLine(startX, bottom, startX + mCharSize, bottom, mLinesPaint);

try {
canvas.drawRoundRect(startX, top, startX + mCharSize, bottom, 8, 8, mLinesPaint);
canvas.drawRoundRect(startX, top, startX + mCharSize, bottom, 8, 8, mStrokePaint);
} catch (NoSuchMethodError err) {
canvas.drawRect(startX, top, startX + mCharSize, bottom, mLinesPaint);
canvas.drawRect(startX, top, startX + mCharSize, bottom, mStrokePaint);
updateColorForLines(i <= textLength, i == textLength);

switch (mBoxStyle) {
case ROUNDED_UNDERLINE:
try {
canvas.drawRoundRect(startX, bottom * .95f, startX + mCharSize, bottom, 16, 16, mStrokePaint);
} catch (NoSuchMethodError err) {
canvas.drawRect(startX, bottom * .95f, startX + mCharSize, bottom, mStrokePaint);
}
break;
case ROUNDED_BOX:
try {
canvas.drawRoundRect(startX, top, startX + mCharSize, bottom, 8, 8, mLinesPaint);
canvas.drawRoundRect(startX, top, startX + mCharSize, bottom, 8, 8, mStrokePaint);
} catch (NoSuchMethodError err) {
canvas.drawRect(startX, top, startX + mCharSize, bottom, mLinesPaint);
canvas.drawRect(startX, top, startX + mCharSize, bottom, mStrokePaint);
}
break;

case UNDERLINE:
canvas.drawRect(startX, ((float) bottom * .95f), startX + mCharSize, bottom, mStrokePaint);
break;

case SQUARE_BOX:
canvas.drawRect(startX, top, startX + mCharSize, bottom, mLinesPaint);
canvas.drawRect(startX, top, startX + mCharSize, bottom, mStrokePaint);
break;
}
if (getText().length() > i) {
float middle = startX + mCharSize / 2;
Expand All @@ -173,7 +227,7 @@ protected void onDraw(Canvas canvas) {
/**
* @param next Is the current char the next character to be input?
*/
private void updateColorForLines(boolean next, boolean current, int textSize, int totalSize) {
private void updateColorForLines(boolean next, boolean current) {
if (next) {
mStrokePaint.setColor(mSecondaryColor);
mLinesPaint.setColor(mSecondaryColor);
Expand All @@ -183,7 +237,7 @@ private void updateColorForLines(boolean next, boolean current, int textSize, in
}
if (current) {
mLinesPaint.setColor(ContextCompat.getColor(getContext(), android.R.color.white));
mStrokePaint.setColor(mMainColor);
mStrokePaint.setColor(mPrimaryColor);
}
}
}
2 changes: 2 additions & 0 deletions otpedittext2/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<attr name="oev_primary_color" format="color" />
<attr name="oev_secondary_color" format="color" />
<attr name="oev_text_color" format="color" />
<attr name="oev_box_style" format="string" />

</declare-styleable>


Expand Down
1 change: 1 addition & 0 deletions otpedittext2/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="light_gray">#eeeeee</color>
<color name="underline_gray">#ECD0D0</color>
</resources>
5 changes: 5 additions & 0 deletions otpedittext2/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<resources>
<string name="app_name">OtpEditText</string>

<string name="style_square">square_box</string>
<string name="style_rounded">rounded_box</string>
<string name="style_underline">underline</string>
<string name="style_rounded_underline">rounded_underline</string>
</resources>
19 changes: 19 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ With stunning animation, and high customizability.

![Demo1](images/demo.gif)
![Demo with other colors](images/demo1.gif)
![Demo with underline](images/demo2.gif)

## Packed with features
- Add custom character limit.
Expand Down Expand Up @@ -72,6 +73,24 @@ To change that use,

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

### Using multiple style options.
There are 4 style options that are available within the library for now.
- rounded box
- square box
- underline
- rounded underline

To use any of these styles, please add ```app:oev_box_style="@string\{box_style_input}"```
attribue.
I have provided string resources for simpler usage.
```
<string name="style_square">square_box</string>
<string name="style_rounded">rounded_box</string>
<string name="style_underline">underline</string>
<string name="style_rounded_underline">rounded_underline</string>
```
Suppose you want the rounded underline option to be displayed. Then, please add:
`app:oev_box_style="@string/style_rounded_underline" ` in the OtpEditText xml code.

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

0 comments on commit a37c3ff

Please sign in to comment.