adjust TextView Text Length - Android User Interface

Android examples for User Interface:TextView

Description

adjust TextView Text Length

Demo Code


//package com.java2s;

import android.text.TextPaint;

import android.widget.TextView;

public class Main {
    public static String adjustTextLength(TextView tv, String textSource,
            String appendText) {//from w ww  .jav  a 2  s .  c o m
        if (null == tv || null == textSource) {
            return textSource;
        }

        TextPaint textPaint = tv.getPaint();
        float stringWidth = textPaint.measureText(textSource);
        int width = tv.getWidth();
        int count = textSource.length();
        while (stringWidth > width && count > 0) {
            stringWidth = textPaint.measureText(textSource.substring(0,
                    --count) + appendText);
        }

        if (count > 0 && count < textSource.length()) {
            return textSource.substring(0, count) + appendText;
        }

        return textSource;
    }
}

Related Tutorials