So what to start with...
Probably with the latest task I have tried to solve on Android. The task was simple enough - show not editable phone prefix in EditText. Something like this:
Most of the time I try to optimize my code so it implies some limitations:
- it must work fast. Actually, it will be a bigger challange for me to make such a simple control work slowly :)
- minimize view hierarchy - no need to use "Linear -> Text -> Edit" here
- code must look good without any dirty hacks
- generalization is good most of the times but no need to be a fanatic
So lets start. Idea is to extend EditText and to override four methods:
- onMeasure - to calculate prefix size
- onLayout - to position prefix correctly
- onDraw - to actually draw prefix
- getCompoundPaddingLeft - to offset EditText's content
Here is full class code:
public class PhoneNumberEdit extends EditText { private String mPrefix = "+3"; // can be hardcoded for demo purposes private Rect mPrefixRect = new Rect(); // actual prefix size public PhoneNumberEdit(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { getPaint().getTextBounds(mPrefix, 0, mPrefix.length(), mPrefixRect); mPrefixRect.right += getPaint().measureText(" "); // add some offset super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawText(mPrefix, super.getCompoundPaddingLeft(), getBaseline(), getPaint()); } @Override public int getCompoundPaddingLeft() { return super.getCompoundPaddingLeft() + mPrefixRect.width(); } }
Thanks a lot, sir. You're like a savior to Android noobs like me.
ReplyDeletecan u please make it to support rtl.
ReplyDeleteThanks a lot mate. Solved my issue. Is there a way of making the prefix part of the string in the EditText? Like when I set prefix as +254 and enter 720123456 in the text field, I want to get +254720123456 when I call phoneEditText.getText() in the java code
ReplyDeleteYou can't override getText() method for this but you can always add new one:
Deletepublic String getTextWithPrefix() {
return mPrefix + getText();
}
please help me in making it to support rtl.
ReplyDeleteMan, thanks a lot!
ReplyDeleteVery useful, thank you!
ReplyDelete