Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How do i make nonbreakable block in TextView?

I have long text containing name that looks like "something-something". This long text is shown in TextView. The problem is "something-something" got line breaked.

I have found unicode character U+2011 NON-BREAKING HYPHEN. But it looks like this unicode character is supported in font since Android 3.0. However I'm supporting Android 2.1 where replacement character is shown instead.

I have looked at class Spannable, but I did not find how to define nonbreaking block of text. Maybe I overlook something.

like image 687
Salw Avatar asked Aug 18 '11 10:08

Salw


People also ask

How do I limit characters in TextView?

To set the maximum length for text in TextView widget, set the maxLength attribute with required number of length.

How can make span clickable in android?

SpannableString string = new SpannableString("Text with clickable text"); string. setSpan(new CustomClickableSpan(), 10, 19, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); Text with ClickableSpan .


1 Answers

I solved breaking of the text block by implementing ReplacementSpan to render text in single block. Here is the code:

public class NonbreakingSpan extends ReplacementSpan {

    @Override
    public void draw(
            Canvas canvas,
            CharSequence text, int start, int end,
            float x, int top, int y, int bottom,
            Paint paint) {
        canvas.drawText(text, start, end, x, y, paint);
    }

    @Override
    public int getSize(
            Paint paint,
            CharSequence text, int start, int end,
            FontMetricsInt fm) {
        return Math.round(paint.measureText(text, start, end));
    }
}
like image 141
Salw Avatar answered Oct 26 '22 00:10

Salw