Context:
I would like to have a TextView which automatically adjusts its textsize to the width of the screen. Therefore I downloaded the AutoResizeTextView based on the post by Chase. It works pretty well, sometimes the View is still growing or shrinking on the device, but I can accept that. However, I really would like to have a very limited Padding of the TextView to make optimal use of the space left on the screen. Therefore I extended the class as follows:
public class AutoResizeTextViewNoPadding extends AutoResizeTextView {
public AutoResizeTextViewNoPadding(Context context) {
super(context);
}
public AutoResizeTextViewNoPadding(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AutoResizeTextViewNoPadding(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onDraw(Canvas canvas) {
int yOffset = getHeight() - getBaseline() - (int)super.getTextSize()/15;
// this does not work, gives a blank view
// int bottom = getHeight() - getBaseline() - (int)super.getTextSize()/15;
// int top = getHeight() - bottom; // absolute number of space cut on bottom, equals top
// canvas.clipRect(0,top,getWidth(),bottom);
// canvas.clipRect(0, top, getWidth(), bottom, Region.Op.REPLACE);
// super.onDraw(canvas);
// this does not work, also gives a blank view
// Bitmap bitmap= Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight() - 2*yOffset, Bitmap.Config.ARGB_8888);
// Paint p = getPaint();
// Canvas othercanvas = new Canvas();
// othercanvas.drawBitmap(bitmap,0,0,p);
// super.onDraw(othercanvas);
// this works to remove the FontPadding on the bottom, but then the top gets more Padding of course
canvas.translate(0, yOffset);
super.onDraw(canvas);
}
}
Problem:
The canvas.translate moves the actual text towards the bottom of the View (yOffset). But I actually would like to have this amount (yOffset) cropped from the bottom and the top of the View. As seen in the code, I tried two things which both do not work, i.e. I see an empty View with the same size as when it would be a normal (AutoResize)TextView. Can this be done?
Or can this issue be solved in another way? Note that setting a negative margin will not work, as the textsize ranges significantly, such that the margins will have to range too. Or can I set the margins somewhere inside the (AutoResize)TextView(NoPadding) class?