Format to Fitable String by font size and width - Android java.lang

Android examples for java.lang:String Shorten

Description

Format to Fitable String by font size and width

Demo Code

import android.content.Context;
import android.util.Log;

public class Main{


    public static String toFitableString(Context ctx, String original,
            int leftWidth, int rightWidth, float fontSize) {
        float screenWidth = SysUtils.getScreenWidth(ctx);
        float screenWidthInDip = SysUtils.px2dip(ctx, screenWidth);
        float halfFont = fontSize / 2;
        int maxWordsCount = (int) ((screenWidthInDip - leftWidth - rightWidth) / SysUtils
                .px2dip(ctx, halfFont));
        Log.v("", String.format("Max Words Count: %d", maxWordsCount));
        try {/*  ww w. j  a v a  2s .c o m*/
            return org.apache.commons.lang3.StringUtils.abbreviate(
                    original, maxWordsCount);
        } catch (IllegalArgumentException e) {
            return original;
        }
    }

}

Related Tutorials