345.5k
345.5k

Reputation: 123

Truncate word at end of string in Textview not char from last

Truncate String At End using this code

CharSequence charSequence;
float avail = 8 * content.getMeasuredWidth();
charSequence = TextUtils.ellipsize(textString,textview.getPaint(),avail, TextUtils.TruncateAt.END);

this code giving me result like this truncating last word and showing dots

hello wor...

but i need truncate also last word at end like below

hello

Upvotes: 2

Views: 685

Answers (1)

okdear
okdear

Reputation: 74

set character length . simply

  protected String genrateString(String source, int numberOfWords){
    BreakIterator iterator = BreakIterator.getSentenceInstance(Locale.US);
    iterator.setText(source);
    int start = iterator.first();
    StringBuffer sb = new StringBuffer();
    for (int end = iterator.next();end != BreakIterator.DONE;start = end, end = iterator.next()) {
        String newSentence =source.substring(start,end);

        int currentWordCount = countWord(sb.toString());

        int newWordCount = currentWordCount + countWord(newSentence);

        if(newWordCount-numberOfWords >= 0.1*numberOfWords) {
            if(currentWordCount<numberOfWords && numberOfWords-currentWordCount > newWordCount-numberOfWords)
                sb.append(newSentence);
            else
                break;
        }
        else
            sb.append(newSentence);
    }
    return sb.toString();
}

Upvotes: 2

Related Questions