0

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?

Community
  • 1
  • 1
Peter
  • 71
  • 10

1 Answers1

0

It's not perfect, but if someone is looking for the same, this more or less does it (for AutoResizableTextViews with android:singleLine="true"):

public class AutoResizeTextViewNoPadding extends TextView
{
(...)
        @Override
        public int onTestSize(final int suggestedSize,final RectF availableSPace)
        {
            paint.setTextSize(suggestedSize);
            final String text=getText().toString();
            final boolean singleline=getMaxLines()==1;
            if(singleline)
            {
                textRect.bottom=(float)(paint.getFontSpacing()*.8);
                textRect.right=paint.measureText(text);
            }
        (...)
        }

@Override
protected void onDraw(Canvas canvas) {
    int yOffset = getHeight() - getBaseline() - (int)super.getTextSize()/20;
    canvas.translate(0, 2*yOffset);
    super.onDraw(canvas);
}

}
Peter
  • 71
  • 10