get TextView Length With Separate - Android android.widget

Android examples for android.widget:TextView

Description

get TextView Length With Separate

Demo Code

import android.graphics.Paint;
import android.widget.TextView;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main{

    /**/* ww w.j  av a 2s .  c om*/
     * 
     * @param textView
     * @param srcStr
     * @param seprateStr
     * @return
     */
    public static float getTextViewLengthWithSeparate(TextView textView,
            String srcStr, String seprateStr) {
        String[] strArray = srcStr.split(seprateStr);
        Paint paint = new Paint();
        paint.setTextSize(textView.getTextSize());
        paint.setTypeface(textView.getTypeface());
        float size = 0;

        if (strArray.length == 0) {
            return 0;
        }
        for (int i = 0; i < strArray.length; i++) {
            float localSize = paint.measureText(strArray[i]);
            if (size < localSize) {
                size = localSize;
            }
        }
        return size;
    }

}

Related Tutorials