Summary
I have a string [tab] [ch]C[/ch] [ch]Am[/ch] \n I heard there was a secret chord[/tab]
When the TextView is big enough to hold it with no wrapping it should (and does) look like this:
C Am
I heard there was a secret chord
When the line(s) are too long to fit in the TextView, I want it to wrap like this:
C
I heard there was a
Am
secret chord
Right now it wraps like this (like you'd expect if it was just text)
C
Am
I heard there was a
secret chord
Constraints:
- I use a monospace text font to keep alignment
- The chords (
C
,F
,Am
,G
) are clickable so if you make a custom implementation of TextView, it still has to be able to handleClickableSpans
or otherwise keep them clickable - Kotlin or Java (or XML) is fine
If it's helpful, this is for an open source project of mine, so the source is available on Github. Here's the fragment source (look for fun processTabContent(text: CharSequence)
-- that's where I process the text right now. Here's the layout xml.
Input Format
My data is stored in a single string (this can't be changed -- I get it from an API). Here's how the above tab would be formatted:
[Intro]\n[tab][ch]C[/ch] [ch]Am[/ch] [ch]C[/ch] [ch]Am[/ch][/tab]\n[Verse 1][tab] [ch]C[ch] [ch]Am[/ch] I heard there was a secret chord [/tab][tab] [ch]C[/ch] [ch]Am[/ch]\nThat David played, and it pleased the Lord[/tab][tab] [ch]C[/ch] [ch]F[/ch] [ch]G[/ch]\n But you don't really care for music, do you?[/tab]
Note that the chords (notes that a guitarist would play, like C
or F
) are wrapped in [ch]
tags. I currently have code that finds these, removes the [ch]
tags, and wraps each chord in a ClickableSpan
. On click, my application shows another fragment with instructions how to play the chord on a guitar. This is only important in that the answer to this question must allow these chords to be clicked like this still.
What I'm doing right now (that isn't working)
As you may have noticed by now, it's the [tab]
tags that we're going to have to focus on for this question. Right now, I'm going through the string and replacing [tab]
with a newline and removing all instances of [/tab]
. This works fine if my TextView
's text size is small enough that entire lines fit on the device screen. However, when the word wrap kicks in I start having problems.
This:
C Am
I heard there was a secret chord
Should wrap to this:
C
I heard there was a
Am
secret chord
But instead wraps like this:
C
Am
I heard there was a
secret chord