Customize EditText with your class.
find the code example below for reference.
public class CustomEdit extends EditText {
private String mPrefix = "+";
private Rect mPrefixRect = new Rect();
public CustomEdit(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(" ");
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();
}
}
In xml use as below
<com.example.CustomEdit
android:id="@+id/edt_no"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/edit_gray"
android:textSize="@dimen/text_14sp"
android:inputType="number"
android:maxLength="10"
>
source
share