Reputation: 2847
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)
Upvotes: 1
Views: 942
Reputation: 12909
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.
Upvotes: 2
Reputation: 61
before draw a text on canvas follow following step and
String temp = "address:345346943693495349534963";
String firstString=temp.substring(0, 21);
[![enter image description here][1]][1]String secondString = temp.substring(20);
String finalString = firstString+"\n"+secondString;
now finally draw a text on canvas with "finalString",and if set text in textview then set final string in textview.
TextView yourTextView = (TextView) findViewById(R.id.txt1);
yourTextView.setText(finalString);
[1]: https://i.sstatic.net/godJJ.png
Upvotes: 0
Reputation: 5017
You could use soft hypens (\u00AD) which you have to place at several points in your number string:
address:3\u00AD453469\u00AD4369\u00AD349534953\u00AD4963
Unfortunately, this will render the hyphens, I don't know if that is okay for you:
address:3453469436934-
95349534963
This also works with the android:text
attribute of TextViews.
P.S: I also thought about using Html like this:
Html.fromHtml("<span style="display: inline-block;">address:3</span>45346943693495349534963");
but unfortunately I could not make it work in Android
Upvotes: 0