This code helps me to draw text in EditText element.
public class LimitedEditText extends EditText
{
private Paint limit_text_paint;
/**
* Current size of text displayed in the EditView.
*/
private int current_text_size;
/**
* Maximum text size allowed.
*/
private int maximum_text_size;
/**
* Default limit indicator X margin used.
*/
private final int DEFAULT_X_MARGIN= 10;
/**
* Default limit indicator Y margin used.
*/
private final int DEFAULT_Y_MARGIN= 20;
/**
* Flag indicating text size limit is unlimited.
*/
public static final int UNLIMITED= -100;
/**
* LimitedEditText constructor.
* @param context Context that will display this view.
* @param attrs Set of attributes defined for this view.
* @param defStyle Style defined for this view.
*/
public LimitedEditText(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
initComponents(attrs);
}
/**
* LimitedEditText constructor.
* @param context Context that will display this view.
* @param attrs Set of attributes defined for this view.
*/
public LimitedEditText(Context context, AttributeSet attrs)
{
super(context, attrs);
initComponents(attrs);
}
/**
* LimitedEditText constructor.
* @param context Context that will display this view.
*/
public LimitedEditText(Context context)
{
super(context);
initComponents(null);
}
/**
* Initialize the UI components.
* @param attrs Set of attributes defined for this view.
*/
private void initComponents(AttributeSet attrs)
{
limit_text_paint= new Paint();
limit_text_paint.setColor(Color.GRAY);
limit_text_paint.setTextSize(20);
maximum_text_size= UNLIMITED;
current_text_size= 0;
if(attrs!=null) //android:maxLength
maximum_text_size= attrs.getAttributeIntValue("android", "maxLength", UNLIMITED);
super.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable s)
{
if(maximum_text_size != UNLIMITED)
current_text_size= getText().toString().length();
invalidate();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before,int count) {}
});
}
/**
* Set maximum text size for given EditText.
* @param max Maximum number of characters allowed.
*/
public void setMaxTextSize(int max)
{
maximum_text_size= max;
if(max== UNLIMITED)
setFilters(null);
else
setFilters(new InputFilter[]{new InputFilter.LengthFilter(max)});
}
/**
* Remove text size limit.
* This will stop drawing the amount of characters left.
*/
public void removeTextSizeLimit()
{
setMaxTextSize(UNLIMITED);
}
/**
* Get the X position of limit indicator.
* @param text Text to be drawn on the limit indicator.
* @return X position of limit indicator.
*/
protected float getLimitIndicatorX(String text)
{
float widths[]= new float1;
limit_text_paint.getTextWidths(text, widths);
float sum= 0;
for(float w: widths)
sum+= w;
return getWidth() + getScrollX() - sum - DEFAULT_X_MARGIN;
}
/**
* Get the Y position of limit indicator.
* @param text Text to be drawn on the limit indicator.
* @return Y position of limit indicator.
*/
protected float getLimitIndicatorY(String text)
{
return DEFAULT_Y_MARGIN + getScrollY();
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
if(maximum_text_size== UNLIMITED)
return;
String text= current_text_size+"/"+maximum_text_size;
canvas.drawText(text, getLimitIndicatorX(text), getLimitIndicatorY(text), limit_text_paint);
}
}
I need to draw it under the text(and blue line), but i can't do it , because after blue line is end of edittext. How could i do it? Of course, i can draw line myself, but i think it will be not correct solution.