Search code examples
androiddialogandroid-custom-view

Display array of numbers with multicolor


How can i display array of numbers with multicolor in dialog?

Example:

      arr[] = {1, 2, 3, 4, 5 , 6, 7, 8, 9}

And in dialog, display:

arr= 1 2 3 4 5 6 7 8 9

1 in red, 2 in blue, 3 in green,... I try add Textview in RelativeLayout but til now, i don't know how?


Solution

  • Spannable allows you to add add such attributes.Here is small example have look at this.

    TextView TV = (TextView)findViewById(R.id.mytextview01);
     Spannable word = new SpannableString(" 1");        
    
     word.setSpan(new ForegroundColorSpan(Color.BLUE), 0, word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    
     TV.setText(word);
     Spannable wordTwo = new SpannableString(" 2");        
    
     wordTwo.setSpan(new ForegroundColorSpan(Color.RED), 0, wordTwo.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
     TV.append(wordTwo);
    

    OR :

    You can also set the colors by using the Html.

    String text = "<font color=#cc0029> 1</font> <font color=#ffcc00> 2</font>";
    TV.setText(Html.fromHtml(text));