1

I'm pretty new to android, Java and sqlite. For my first program I'm creating a program that will take user input and place in predefined text.

Example: "text" string1 "more text" string2 "even more text" etc

My text will be one color and strings will display in another color.

I'm using sqlite to seperate my data and code and this is where I hit a wall. Trying to find help on how I will be able to combine my above text into one row/column in my database table.

Using only one above example i could get this up and running. But there will be 50+ of above example for release making a database a must especially when I want to add more after release.

maebe
  • 553
  • 1
  • 6
  • 18
  • Read related SO thread - http://stackoverflow.com/questions/4283755/android-question-implementing-a-rich-text-editor – KV Prajapati Jan 02 '12 at 02:45

1 Answers1

6

Most probably you've read up on SpannableStringBuilder, which allows you to add color to the text in your TextView's content. Check out the code below:

SpannableStringBuilder ssb = new SpannableStringBuilder(<your text>);
ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(255, 0, 0));

ssb.setSpan(fcs, 0, ssb.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(ssb); 

The code above will work in most cases, however what you want is to have different alternating colors on a single TextView. Then you should do the following:

String text = your_text + text_from_database;
SpannableStringBuilder ssb = new SpannableStringBuilder(text);
ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(255, 0, 0));
ForegroundColorSpan fcs2 = new ForegroundColorSpan(Color.rgb(0, 255 0));

ssb.setSpan(fcs, 0, your_text, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
ssb.setSpan(fcs2, your_text.length(), ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(ssb);

The above code will now work, but you'll notice that if you add another text your_another_text and want to use the previous fcs instance for a second time, the previously colored your_text will now lose its formatting (color). This time you'll need to create another ForegroundColorSpan fcs3 to format the third part of the SpannableStringBuilder. The key here is to use a character style in a setSpan method only once. See below:

String testTest = "abcdefgh";
String testTest2 = "ijklmnop";
String testTest3 = "qrstuvwxyz";

SpannableStringBuilder ssb = new SpannableStringBuilder(testTest+testTest2+testTest3);
ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(255, 0, 0));
ForegroundColorSpan fcs2 = new ForegroundColorSpan(Color.rgb(0, 255, 0));
ForegroundColorSpan fcs3 = new ForegroundColorSpan(Color.rgb(255, 0, 0));


ssb.setSpan(fcs, 0, testTest.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
ssb.setSpan(fcs2, testTest.length(), (testTest+testTest2).length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
ssb.setSpan(fcs3, (testTest+testTest2).length(), ssb.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);

test.setText(ssb);

This method is good if you know you have a fixed number of String elements in your SpannableStringBuilder. If you have wish to have a TextView of dynamic length and number of elements, you need to do this in a different approach. What worked for me was to convert each string element into a SpannableString, use setSpan, and append it to the TextView. This is useful if you're using a loop to build your TextView.

TextView test = (TextView)findViewById(R.id.test);

String testTest = "abcdefgh";
String testTest2 = "ijklmnop";
String testTest3 = "qrstuvwxyz";

SpannableString ssb = new SpannableString(testTest);
ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(255, 0, 0));
ForegroundColorSpan fcs2 = new ForegroundColorSpan(Color.rgb(0, 255, 0));
ForegroundColorSpan fcs3 = new ForegroundColorSpan(Color.rgb(255, 0, 0));

ssb.setSpan(fcs, 0, ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
test.setText(ssb);

SpannableString ssb2 = new SpannableString(testTest2);
ssb2.setSpan(fcs2, 0, ssb2.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
test.append(ssb2);

SpannableString ssb3 = new SpannableString(testTest3);
ssb3.setSpan(fcs3, 0, ssb3.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
test.append(ssb3);
Bartek Lipinski
  • 30,698
  • 10
  • 94
  • 132
josephus
  • 8,284
  • 1
  • 37
  • 57
  • Today finally to the part of my program that will use this. To make it more clear of what I'm doing. I'm taking one long text string and insert edittexts into it and those edittexts will have the different color. The text string is store in a database while the edittext working on them but want to pull them from a string but only found a way to convert them into a string array instead of individual strings setup in string.xml. BTW the amount of edittext and where they fit into the text string will be dynamic. Having trouble telling the program where to put the edittext – maebe Jan 22 '12 at 23:20