Skip to content

Instantly share code, notes, and snippets.

@codeversed
Forked from alexfu/PrefixedEditText.java
Created February 26, 2016 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeversed/8b02960248ab6ae1227c to your computer and use it in GitHub Desktop.
Save codeversed/8b02960248ab6ae1227c to your computer and use it in GitHub Desktop.
EditText with support for prefixes.
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.EditText;
/**
* Custom EditText that displays a fixed prefix in line with the text.
* The trick here is to draw the prefix as a drawable and attach it via
* setCompoundDrawables().
*/
public class PrefixedEditText extends EditText {
private ColorStateList mPrefixTextColor;
public PrefixedEditText(Context context) {
this(context, null);
}
public PrefixedEditText(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.editTextStyle);
}
public PrefixedEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mPrefixTextColor = getTextColors();
}
public void setPrefix(String prefix) {
setCompoundDrawables(new TextDrawable(prefix), null, null, null);
}
public void setPrefixTextColor(int color) {
mPrefixTextColor = ColorStateList.valueOf(color);
}
public void setPrefixTextColor(ColorStateList color) {
mPrefixTextColor = color;
}
private class TextDrawable extends Drawable {
private String mText = "";
public TextDrawable(String text) {
mText = text;
setBounds(0, 0, (int) getPaint().measureText(mText) + 2, (int) getTextSize());
}
@Override
public void draw(Canvas canvas) {
Paint paint = getPaint();
paint.setColor(mPrefixTextColor.getColorForState(getDrawableState(), 0));
int lineBaseline = getLineBounds(0, null);
canvas.drawText(mText, 0, canvas.getClipBounds().top + lineBaseline, paint);
}
@Override
public void setAlpha(int alpha) {/* Not supported */}
@Override
public void setColorFilter(ColorFilter colorFilter) {/* Not supported */}
@Override
public int getOpacity() {
return 1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment