Code example for StaticLayout

Methods: draw, getLineCount

0
    private int getTextLineCount(CharSequence source, TextPaint paint, int widthPx, float textSize) {
        // Update the text paint object 
        paint.setTextSize(textSize);
 
        // Draw using a static layout 
        StaticLayout layout = new StaticLayout(source, paint, widthPx, Layout.Alignment.ALIGN_NORMAL, mSpacingMult,
                mSpacingAdd, true);
 
        try { 
            layout.draw(TEXT_RESIZE_CANVAS);
        } catch (Exception e) {
            return 1; 
        } 
        return layout.getLineCount();
 
    } 
 
    // Set the text size of the text paint object and use a static layout to render text off screen before measuring 
    private int getTextHeight(CharSequence source, TextPaint paint, int width, float textSize) {
        // Update the text paint object 
        paint.setTextSize(textSize);