I have a TextView that will hold a styled, multiline string. This text is generated line by line as the data is drawn from different sources bit by bit. As I grab each field, I add a Bold header to the TextView on a new line, then I add the data for that field in italics on a new line.
Example formatting:
Due Date
July 22, 2010
Course
CSC 350
I could set the styling like this:
Spannable str = (Spannable) textview.getText();
str.setSpan(new StyleSpan(android.graphics.Typeface.Bold), 0, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new StyleSpan(android.graphics.Typeface.Italic), 8, 21, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new StyleSpan(android.graphics.Typeface.Bold), 21, 27, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new StyleSpan(android.graphics.Typeface.Italic), 27, 33, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
But that requires knowing the length of each section of text at runtime which is simply unmanageable since there is a variable number of data fields that could be added to the textview.
How can I add the styling as I'm adding each piece of text instead of after all the text has been added?