Paulina
Paulina

Reputation: 15

android - Different colors in one textview

I would like to know how can I define different colors in a single textview depending on the textview repeats, for example:

If I declare something like: Color[] colors = {Color.green, Color.blue, Color.red, Color.brown, Color.yellow};

Then I want to pass "colors" somehow in tv.setBackgroundColor(Color.GREEN); instead of "Color.green" in order that if the textview repeats five times I could see the textview in green, blue, red, brown and yellow and if it's 7 times I could get green, blue, red, brown, yellow, green and blue.

Here's some code, plus an image

adapter = new SimpleCursorAdapter(this, R.layout.hrprocess_item_list, cursor, from, to, 0);
    adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
            if (view.getId() == R.id.category) {
              String s=cursor.getString(cursor.getColumnIndex(manager.CONTENT_CATEGORY));
                TextView tv = (TextView)view;               
               int[] androidColors = getResources().getIntArray(R.array.androidcolors);
               for (int i = 0; i < androidColors.length; i++) {
                   tv.setBackgroundColor(androidColors[i]);
                }
              tv.setText(s);
                return true;
            }
            return false;
        }

    });
    lv.setAdapter(adapter); 

Colors.xml

<item name="azul1" type="color">#4178BE</item>
<item name="verde1" type="color">#008571</item>
<item name="morado1" type="color">#9855D4</item>
<item name="rojo1" type="color">#E71D32</item>
<item name="rojo_naranja1" type="color">#D74108</item>
<item name="amarillo1" type="color">#EFC100</item>
<item name="gris1" type="color">#6D7777</item>


<integer-array name="androidcolors">
    <item>@color/azul1</item>
    <item>@color/verde1</item>
    <item>@color/morado1</item>
    <item>@color/rojo1</item>
    <item>@color/rojo_naranja1</item>
    <item>@color/amarillo1</item>
    <item>@color/gris1</item>

</integer-array> 

(https://i.sstatic.net/dCQep.jpg)

Upvotes: 0

Views: 264

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006614

Rather than using setBackgroundColor(), you will need to apply BackgroundColorSpan objects to the ranges that you are interested in.

This sample project applies BackgroundColorSpan to highlight search results in a TextView:

  private void searchFor(String text) {
    TextView prose=(TextView)findViewById(R.id.prose);
    Spannable raw=new SpannableString(prose.getText());
    BackgroundColorSpan[] spans=raw.getSpans(0,
                                             raw.length(),
                                             BackgroundColorSpan.class);

    for (BackgroundColorSpan span : spans) {
      raw.removeSpan(span);
    }

    int index=TextUtils.indexOf(raw, text);

    while (index >= 0) {
      raw.setSpan(new BackgroundColorSpan(0xFF8B008B), index, index
          + text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
      index=TextUtils.indexOf(raw, text, index + text.length());
    }

    prose.setText(raw);
  }

In your case, you would rotate through your colors, rather than using the hardcoded hex value of a color that I'm using.

Upvotes: 2

Related Questions