I have a requirement to build a textView which contains something like:
“Title[x]” followed by “some text” followed by “\n” followed by “Title[y]” followed by “more text” etc. giving
Title[x] some text
Title[y] more text
I need Title to be bold and to have a different color for each Title. I have 11 titles in total and up to 30 text strings to choose from and any title can have any text string. On each cycle, I will have from 1 to 6 Titles in my final resultText.
I have no problem building the textView with “known” values of x and y but, in real life, I won”t know the values until I come to build the spannable string. I don”t want to build every possible variation of strings (over 300).
I”ve tried creating all the “Title” spannables and appending them to my “result” as I create them and it works fine, but if I create them all and append them in one statement at the end I lose the Bold and Color attributes.
My main.xml has a textView with an ID of color_test.
package uk.cmj.color;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.view.View;
import android.widget.TextView;
import android.widget.TextView.BufferType;
public class ColorActivity extends Activity {
/** Called when the activity is first created. */
private static int titleIndex = 0;
private final String[] titles = {"Title A", "Title B", "Title C"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// ---------------------------------------------
// THIS WORKS AND I SEE COLOR AND BOLD FOR TITLE
// ---------------------------------------------
titleIndex = 1;
SpannableStringBuilder resultText = new SpannableStringBuilder();
String firstTitle = titles[titleIndex];
SpannableString firstTitleSpannable= new SpannableString(firstTitle);
firstTitleSpannable.setSpan(new ForegroundColorSpan(Color.rgb(155,79,25)), 0, firstTitle.length(), 0);
firstTitleSpannable.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, firstTitle.length(), 0);
resultText.append(firstTitleSpannable);
String body1 = " some text" + "\n";
SpannableString body1Spannable= new SpannableString(body1);
body1Spannable.setSpan(new ForegroundColorSpan(Color.BLACK), 0, body1.length(), 0);
resultText.append(body1Spannable);
titleIndex = 2;
String nextTitle = titles[titleIndex];
SpannableString nextTitleSpannable= new SpannableString(nextTitle);
nextTitleSpannable.setSpan(new ForegroundColorSpan(Color.rgb(155,79,25)), 0, nextTitle.length(), 0);
nextTitleSpannable.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, nextTitle.length(), 0);
resultText.append(nextTitleSpannable + "\n");
String body2 = " some different text" + "\n";
SpannableString body2Spannable= new SpannableString(body2);
body1Spannable.setSpan(new ForegroundColorSpan(Color.BLACK), 0, body1.length(), 0);
resultText.append(body2Spannable);
TextView color_test = (TextView) findViewById(R.id.color_test);
color_test.setText(resultText, BufferType.SPANNABLE);
// ------------------------------------------
// THIS DOESN'T WORK AS I LOSE COLOR AND BOLD
// ------------------------------------------
titleIndex = 1;
SpannableStringBuilder resultText = new SpannableStringBuilder();
String firstTitle = titles[titleIndex];
SpannableString firstTitleSpannable= new SpannableString(firstTitle);
firstTitleSpannable.setSpan(new ForegroundColorSpan(Color.rgb(155,79,25)), 0, firstTitle.length(), 0);
firstTitleSpannable.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, firstTitle.length(), 0);
String body1 = " some text" + "\n";
SpannableString body1Spannable= new SpannableString(body1);
body1Spannable.setSpan(new ForegroundColorSpan(Color.BLACK), 0, body1.length(), 0);
resultText.append(body1Spannable);
titleIndex = 2;
String nextTitle = titles[titleIndex];
SpannableString nextTitleSpannable= new SpannableString(nextTitle);
nextTitleSpannable.setSpan(new ForegroundColorSpan(Color.rgb(155,79,25)), 0, nextTitle.length(), 0);
nextTitleSpannable.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, nextTitle.length(), 0);
String body2 = " some different text" + "\n";
SpannableString body2Spannable= new SpannableString(body2);
body1Spannable.setSpan(new ForegroundColorSpan(Color.BLACK), 0, body1.length(), 0);
resultText.append(body2Spannable);
resultText.append(firstTitleSpannable
+ body1Spannable
+ nextTitleSpannable
+ body2Spannable);
TextView color_test = (TextView) findViewById(R.id.color_test);
color_test.setText(resultText, BufferType.SPANNABLE);
}
}
I’m not quite sure if I understand your problem correctly. But if I do, I’d write a class which contains all the attributes needed and create objects during runtime:
Then do during runtime something like
You could store the objects in an ArrayList of the type
SpannableContent.