I have the following line of text: address:345346943693495349534963
I want this line to always break like this (exact breaking point is not important but it should not be a colon):
address:3453469436934
95349534963
On my Android 4.2.2 device it breaks as above, but on Android 6.0 emulator it behaves differently:
address:
345346943693495349534963
How do I make a breaking behavior consistent across all devices?
I get different results when I draw a text on canvas, here's the code (in Scala):
def paint = {
val newPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG)
newPaint setTextSize getResources.getDimensionPixelSize(R.dimen.text_size)
newPaint setTypeface Typeface.create("Droid Sans", Typeface.NORMAL)
newPaint setTextAlign Paint.Align.CENTER
newPaint setStyle Paint.Style.FILL
newPaint setColor Color.BLACK
newPaint
}
def drawText(canvas: Canvas, text: String, x: Float, baseY: Float) = {
val layout = new StaticLayout(text, paint, textBounds, ALIGN_NORMAL, 1.0f, 0.0f, false)
val y = baseY - layout.getHeight / 2f
canvas.save
canvas.translate(x, y)
layout draw canvas
canvas.restore
}
drawText(canvas, gText, bitmap.getWidth / 2, topSize / 2)
It looks like you are looking for the Word Joiner unicode codepoint https://en.wikipedia.org/wiki/Word_joiner
It's also know as Zero Width no-break space (ZWNBSP).
According to this other question: Zero-width line breaking space for Android
it should be supported on Android since version 5 (Lollipop). The same goes for soft-hypen (suggested in PhilLab response).
I would suggest you to try it out, you can write it as ​
in the XML resource string. Put it between the colon and the number.
If it doesn't work before Lollipop and you need support for previous platform the only other thing I can think about is measuring the text, character by character and, knowing the available width of your view (which you have to compute separately), manually add a new line in the text where you want it to split.
You can see how the Text measurement works in Android by reading this article: https://chris.banes.me/2014/03/27/measuring-text/
Maybe there's a way to do it using Spans too, but I'm not aware of any.