I have a TextView which contains a random string of 60 characters.
The string is a single word and does not contain any white spaces.
The problem is that after certain characters (e.g. @, &, %) I get an automatic line break which I do not want.
My desired outcome is that every line is filled up to the end and no random line breaks are present.
I have tried setting breakStrategy and updating hyphenationFrequency, but this has not helped.
How can I prevent this from happening?
UPDATE: Thanks to @Darkman, this is the solution, which I have written. It checks how many characters can fit into a single line without line-breaks and appends \n at the end.
Be aware that for my use case string has no blank spaces and I am using a monospace font.
fun TextView.toNonBreakingString(text: String?): String {
if (text == null) return ""
val container = parent as? ViewGroup ?: return text
val lineWidth = (container.width - container.paddingStart - container.paddingEnd).toFloat()
val maxCharsInOneLine = paint.breakText(text, 0, text.length, true, lineWidth, null)
if (maxCharsInOneLine == 0) return text
val sb = StringBuilder()
var currentLine = 1
var end = 0
for (i in 0..text.count() step maxCharsInOneLine) {
end = currentLine * maxCharsInOneLine
if (end > text.length) end = text.length
sb.append(text.subSequence(i, end))
sb.append("\n")
currentLine = currentLine.inc()
}
if (end < text.length) {
val remainingChars = text.length - end
sb.append(text.takeLast(remainingChars))
}
return sb.toString()
}

