Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prefix feature #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@
app:libIZO_setBorderView="true"
app:libIZO_setCornerRadius="4dp" />

<com.libizo.CustomEditText
android:id="@+id/t6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:hint="With Prefix"
android:inputType="text"
android:text="85624503400"
app:libIZO_prefix="+62 " />

</LinearLayout>

</RelativeLayout>
30 changes: 27 additions & 3 deletions customedittext/src/main/java/com/libizo/CustomEditText.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.text.TextUtils;
Expand Down Expand Up @@ -50,6 +47,8 @@ public class CustomEditText extends android.widget.EditText {
private int clearIconTint;
private int hideShowIconTint;
private boolean isBorderView = false;
private float mOriginalLeftPadding = -1;
private String mPrexix;

public CustomEditText(Context context) {
super(context);
Expand Down Expand Up @@ -82,6 +81,7 @@ public void init(Context context, AttributeSet attrs) {
hideShowIconTint = a.getColor(R.styleable.CustomEditText_libIZO_hideShowPasswordIconTint, DEFAULT_COLOR);
clearIconTint = a.getColor(R.styleable.CustomEditText_libIZO_clearIconTint, DEFAULT_COLOR);
this.fontName = a.getString(R.styleable.CustomEditText_libIZO_setFont);
mPrexix = a.getString(R.styleable.CustomEditText_libIZO_prefix);
// set corner radius
mCornerRadius = a.getDimension(R.styleable.CustomEditText_libIZO_setCornerRadius, 1);
if (isBorderView) {
Expand All @@ -99,6 +99,9 @@ public void init(Context context, AttributeSet attrs) {
if (!isPassword && isClearIconVisible) {
handleClearButton();
}
if(mPrexix != null){
calculatePrefix();
}
if (isPassword)
if (!TextUtils.isEmpty(getText())) {
showPasswordVisibilityIndicator(true);
Expand Down Expand Up @@ -217,6 +220,11 @@ private Drawable getShapeBackground(@ColorInt int color) {
@SuppressLint("DrawAllocation")
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

if(mPrexix != null){
String prefix = mPrexix;
canvas.drawText(prefix, mOriginalLeftPadding,getLineBounds(0, null), getPaint());
}
}


Expand Down Expand Up @@ -298,4 +306,20 @@ public void setFontName(String fontName) {
this.fontName = fontName;
setFont();
}

private void calculatePrefix() {
if (mOriginalLeftPadding == -1) {
String prefix = mPrexix;
float[] widths = new float[prefix.length()];
getPaint().getTextWidths(prefix, widths);
float textWidth = 0;
for (float w : widths) {
textWidth += w;
}
mOriginalLeftPadding = getCompoundPaddingLeft();
setPadding((int) (textWidth + mOriginalLeftPadding),
getPaddingRight(), getPaddingTop(),
getPaddingBottom());
}
}
}
1 change: 1 addition & 0 deletions customedittext/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<attr name="libIZO_setClearIconVisible" format="boolean" />
<attr name="libIZO_clearIconTint" format="color" />
<attr name="libIZO_hideShowPasswordIconTint" format="color" />
<attr name="libIZO_prefix" format="string" />
</declare-styleable>

</resources>