Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
fahim44 committed Feb 18, 2020
2 parents f996433 + 29486d0 commit d852655
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 20 deletions.
6 changes: 3 additions & 3 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
android:textColor="@android:color/black"
android:textSize="34sp"
app:focusedStateLineColor="@android:color/holo_blue_dark"
app:innerColor="@android:color/white"
app:innerColor="@android:color/transparent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:lineCornerRadius="8dp"
app:lineWidth="3dp"
app:lineCornerRadius="4dp"
app:lineWidth="2dp"
app:selectedStateLineColor="@android:color/holo_red_dark"
app:unFocusedStateLineColor="@android:color/darker_gray" />

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.lamonjush.pinentryedittext;

import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;

class BackgroundShape {
private int fillColor, strokeWidth;
private float cornerRadius;

void setFillColor(int fillColor) {
this.fillColor = fillColor;
}

void setCornerRadius(float cornerRadius) {
this.cornerRadius = cornerRadius;
}

void setStrokeWidth(int strokeWidth) {
this.strokeWidth = strokeWidth;
}

Drawable getDrawable(int strokeColor) {
GradientDrawable gradientDrawable = new GradientDrawable();
gradientDrawable.setColor(fillColor);
gradientDrawable.setCornerRadius(cornerRadius);
gradientDrawable.setStroke(strokeWidth, strokeColor);
return gradientDrawable;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.lamonjush.pinentryedittext;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
Expand All @@ -14,6 +15,7 @@
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

import androidx.appcompat.widget.AppCompatEditText;

Expand All @@ -25,15 +27,15 @@ public class PinEntryEditText extends AppCompatEditText {
float mCharSize = 0;
float mNumChars = 4;
float mLineSpacing = 16; //8dp by default
float mBoxWidth = 10;
float mBoxCornerRadius = 8;
Paint mOuterBoxPaint = new Paint();
Paint mInnerBoxPaint = new Paint();

private OnClickListener mClickListener;

private PinEntryListener mPinEntryListener;

private BackgroundShape backgroundShape = new BackgroundShape();

private int strokeColor = Color.GRAY;

int[][] mStates = new int[][]{
new int[]{android.R.attr.state_selected}, // selected
new int[]{android.R.attr.state_focused}, // focused
Expand Down Expand Up @@ -76,9 +78,9 @@ private void init(Context context, AttributeSet attrs) {
mColors[1] = a.getColor(R.styleable.PinEntryEditText_focusedStateLineColor, mColors[2]);
mColors[0] = a.getColor(R.styleable.PinEntryEditText_selectedStateLineColor, mColors[1]);

mInnerBoxPaint.setColor(a.getColor(R.styleable.PinEntryEditText_innerColor, Color.WHITE));
mBoxCornerRadius = a.getDimension(R.styleable.PinEntryEditText_lineCornerRadius, 8);
mBoxWidth = a.getDimension(R.styleable.PinEntryEditText_lineWidth, 10);
backgroundShape.setFillColor(a.getColor(R.styleable.PinEntryEditText_innerColor, Color.TRANSPARENT));
backgroundShape.setCornerRadius(Utils.convertDpToPixel(a.getDimension(R.styleable.PinEntryEditText_lineCornerRadius, 4), getContext()));
backgroundShape.setStrokeWidth((int) Utils.convertDpToPixel(a.getDimension(R.styleable.PinEntryEditText_lineWidth, 2), getContext()));
getPaint().setColor(getCurrentTextColor());
} finally {
a.recycle();
Expand Down Expand Up @@ -168,6 +170,12 @@ public void afterTextChanged(Editable editable) {
}
});

setOnFocusChangeListener((view, b) -> {
if (b) {
postDelayed(() -> ((EditText) view).setSelection(((EditText) view).getText().length()), 50);
}
});

}

@Override
Expand All @@ -192,15 +200,16 @@ protected void onDraw(Canvas canvas) {
//Text Width
Editable text = getText();
int textLength = Objects.requireNonNull(text).length();
float[] textWidths = new float[textLength];
@SuppressLint("DrawAllocation") float[] textWidths = new float[textLength];
getPaint().getTextWidths(getText(), 0, textLength, textWidths);

for (int i = 0; i < mNumChars; i++) {

updateColorForLines(i == textLength);

canvas.drawRoundRect(startX, top, startX + mCharSize, bottom, mBoxCornerRadius, mBoxCornerRadius, mOuterBoxPaint);
canvas.drawRoundRect(startX + mBoxWidth, top + mBoxWidth, startX + mCharSize - mBoxWidth, bottom - mBoxWidth, mBoxCornerRadius, mBoxCornerRadius, mInnerBoxPaint);
Drawable drawable = backgroundShape.getDrawable(strokeColor);
drawable.setBounds(startX, top, (int) (startX + mCharSize), bottom);
drawable.draw(canvas);

if (getText().length() > i) {
float middle = startX + mCharSize / 2;
Expand Down Expand Up @@ -242,15 +251,12 @@ private int getColorForState(int... states) {
/* next = is the current char the next character to be input? */
private void updateColorForLines(boolean next) {
if (isFocused()) {
mOuterBoxPaint.setColor(
getColorForState(android.R.attr.state_focused));
strokeColor = getColorForState(android.R.attr.state_focused);
if (next) {
mOuterBoxPaint.setColor(
getColorForState(android.R.attr.state_selected));
strokeColor = getColorForState(android.R.attr.state_selected);
}
} else {
mOuterBoxPaint.setColor(
getColorForState(-android.R.attr.state_focused));
strokeColor = getColorForState(-android.R.attr.state_focused);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.lamonjush.pinentryedittext;

import android.content.Context;
import android.util.DisplayMetrics;

class Utils {


/**
* This method converts dp unit to equivalent pixels, depending on device density.
*
* @param dp A value in dp (density independent pixels) unit. Which we need to convert into pixels
* @param context Context to get resources and device specific display metrics
* @return A float value to represent px equivalent to dp depending on device density
*/
static float convertDpToPixel(float dp, Context context){
return dp * ((float) context.getResources().getDisplayMetrics().densityDpi / DisplayMetrics.DENSITY_DEFAULT);
}

/**
* This method converts device specific pixels to density independent pixels.
*
* @param px A value in px (pixels) unit. Which we need to convert into db
* @param context Context to get resources and device specific display metrics
* @return A float value to represent dp equivalent to px value
*/
static float convertPixelsToDp(float px, Context context){
return px / ((float) context.getResources().getDisplayMetrics().densityDpi / DisplayMetrics.DENSITY_DEFAULT);
}


}

0 comments on commit d852655

Please sign in to comment.