Skip to content

Instantly share code, notes, and snippets.

@AswinpAshok
Last active June 26, 2018 11:38
Show Gist options
  • Save AswinpAshok/7e5d140fff339b8c2197307f7383e067 to your computer and use it in GitHub Desktop.
Save AswinpAshok/7e5d140fff339b8c2197307f7383e067 to your computer and use it in GitHub Desktop.
public class ExtendedEditText extends AppCompatEditText {
String TAG = "ExtendedEditText";
TextPaint textPaint = new TextPaint();
private String suffix = "";
private String prefix = "";
private boolean appendWhiteSpace;
private int hackPadding; /**Used to fix wrong positioning of EditText cursor, when both prefix and suffix are set*/
private int leftPadding, prefixXPosition, stringXPosition, suffixXPosition;
/**
* This constructor is called when view is created from code, ie,
* ExtendedEditText exEditText = new ExtendedEditText(this);
* */
public ExtendedEditText(Context context) {
super(context);
}
/**
* This constructor is called usually, when you add View in layout xml file, ie,
* <your.package.name.ExtendedEditText
* android:layout_width = ""
* android:layout_height = ""/>
* */
public ExtendedEditText(Context context, AttributeSet attrs) {
super(context, attrs);
getAttributes(context, attrs, 0);
}
/**
* This constructor is called, when you add View in layout xml file and Specify a style for it, ie,
* <your.package.name.ExtendedEditText
* android:layout_width = ""
* android:layout_height = ""
* android:style="@style/my_style"/>
* */
public ExtendedEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
getAttributes(context, attrs, defStyleAttr);
}
/**
* set color for text
* */
@Override
protected void onFinishInflate() {
super.onFinishInflate();
textPaint.setColor(getCurrentTextColor());
textPaint.setTextSize(getTextSize());
textPaint.setTextAlign(Paint.Align.LEFT);
textPaint.setAntiAlias(true);
}
/**
* get the value user entered, as string
* */
public String getUserInput(){
return getText().toString();
}
/**
* get the whole string from the view, including prefix and suffix, if any.
* */
public String getCompleteString() {
return prefix+getText().toString()+suffix;
}
/**
* onDraw draws the view on device screen
* */
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
suffixXPosition = (int) (textPaint.measureText(getText().toString()) + leftPadding + textPaint.measureText(prefix));
canvas.drawText(prefix, prefixXPosition, getBaseline(), textPaint);
canvas.drawText(getText().toString(), stringXPosition, getBaseline(), textPaint);
canvas.drawText(suffix, suffixXPosition, getBaseline(), textPaint);
}
@Override
public void setSelection(int index) {
super.setSelection(index);
Log.d(TAG, "setSelection: called at "+index);
}
@Override
public void setOnFocusChangeListener(OnFocusChangeListener l) {
super.setOnFocusChangeListener(l);
setSelection(getText().toString().length());
}
/**
* set suffix string programmatically
* */
public void setSuffix(String suffix){
this.suffix = suffix;
if(appendWhiteSpace){
this.suffix = " " + suffix;
}
invalidate();
}
/**
* set prefix string programmatically
* */
public void setPrefix(String prefix){
this.prefix = prefix;
if(appendWhiteSpace){
this.prefix = prefix + " ";
}
invalidate();
}
/**
* called before onDraw, to measure dimensions of view
* */
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
hackPadding = (int) (leftPadding + textPaint.measureText(prefix));
prefixXPosition = leftPadding;
stringXPosition = (int) (leftPadding + textPaint.measureText(prefix));
setPadding(hackPadding, getPaddingTop(), getPaddingRight(),getPaddingBottom());
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
setSelection(getText().toString().length());
}
/**
* gets the custom attributes defined in attrs.xml for ExtendedEditText,
* such as prefix, suffix, etc, before inflating view.
* */
private void getAttributes(Context context, AttributeSet attrs, int defStyleAttr) {
leftPadding = getPaddingLeft();
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ExtendedEditText, defStyleAttr, 0);
if(a != null) {
suffix = a.getString(R.styleable.ExtendedEditText_suffix);
prefix = a.getString(R.styleable.ExtendedEditText_prefix);
appendWhiteSpace = a.getBoolean(R.styleable.ExtendedEditText_appendWhiteSpace, false);
Log.d(TAG, "getAttributes: prefix : "+prefix+"\n suffix : "+suffix);
if(suffix == null) {
suffix = "";
}else {
if(appendWhiteSpace) {
suffix = " " + suffix;
}
}
if(prefix == null) {
prefix = "";
}else {
if(appendWhiteSpace) {
prefix = prefix + " ";
}
}
}else {
Log.d(TAG, "getAttributes: typed array null");
}
a.recycle();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment