Trim text to a maximum size - Android java.lang

Android examples for java.lang:String Strip

Description

Trim text to a maximum size

Demo Code

import android.content.Context;
import android.graphics.Paint;
import com.android.videoeditor.R;
import java.util.Random;

public class Main{

    /**/*from   w  w w  . ja  v  a2 s .  co  m*/
     * 
     *
     * @param text The text
     * @param p The paint
     * @param maxSize The maximum size
     *
     * @return The text
     */
    public static String trimText(String text, Paint p, int maxSize) {
        final int textSize = (int) p.measureText(text);
        if (textSize > maxSize) {
            final int chars = p.breakText(text, true, maxSize - 12, null);
            text = text.substring(0, chars);
            text += "...";
        }

        return text;
    }

}

Related Tutorials