adjust TextView Text Size - Android User Interface

Android examples for User Interface:TextView

Description

adjust TextView Text Size

Demo Code


//package com.java2s;

import android.text.TextPaint;

import android.widget.TextView;

public class Main {
    public static void adjustTextSize(TextView tv) {

        float size = tv.getPaint().getTextSize();
        TextPaint textPaint = tv.getPaint();
        float stringWidth = textPaint.measureText(tv.getText().toString());
        int textViewWidth = tv.getWidth();
        if (textViewWidth > stringWidth) {

            float percent = (textViewWidth / stringWidth);
            textPaint.setTextSize(size * percent);
        }//from w  w  w . ja  va  2  s  .  c o  m
    }
}

Related Tutorials