Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Most probably you've read up on <code>SpannableStringBuilder</code>, which allows you to add color to the text in your TextView's content. Check out the code below:</p> <pre><code>SpannableStringBuilder ssb = new SpannableStringBuilder(&lt;your text&gt;); ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(255, 0, 0)); ssb.setSpan(fcs, 0, ssb.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); textView.setText(ssb); </code></pre> <p>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:</p> <pre><code>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); </code></pre> <p>The above code will now work, but you'll notice that if you add another text <code>your_another_text</code> and want to use the previous <code>fcs</code> instance for a second time, the previously colored <code>your_text</code> 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 <code>setSpan</code> method only once. See below:</p> <pre><code>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); </code></pre> <p>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 <code>setSpan</code>, and <code>append</code> it to the TextView. This is useful if you're using a loop to build your TextView.</p> <pre><code>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); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload