Search code examples
androiduitextviewtextview

How can I change the color of a part of a TextView?


text = text + CepVizyon.getPhoneCode() + "\n\n"
            + getText(R.string.currentversion) + CepVizyon.getLicenseText();
    activationText.setText(text);   
myTextView.setText(text);

I want to change the color for CepVizyon.getPhoneCode()'s string. How could I do that?


Solution

  • Spannable is more flexible:

    String text2 = text + CepVizyon.getPhoneCode() + "\n\n"
                + getText(R.string.currentversion) + CepVizyon.getLicenseText();
    
    Spannable spannable = new SpannableString(text2);
    
    spannable.setSpan(new ForegroundColorSpan(Color.WHITE), text.length(), (text + CepVizyon.getPhoneCode()).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    
    myTextView.setText(spannable, TextView.BufferType.SPANNABLE);