0

How to give color to string: Example:

String str="Hello Android";

How to give color to the above string?

Also how to give red color to string "cancel" the following :

final String[] items = { "Switch Off", "Timer Settings","Cancel"};
5
  • You mean in the app (and not in the IDE), right? Your question is a bit confusing... Commented Feb 21, 2012 at 6:28
  • are you using this strings to set text for Textview? Commented Feb 21, 2012 at 6:29
  • There is no way to that in string file. but you can do that by creating a State List and setting background of TextView Commented Feb 21, 2012 at 6:29
  • assuming you are using those strings for a textview, simply use the 'setTextColor(int)' method. Documentation: developer.android.com/reference/android/widget/… Commented Feb 21, 2012 at 6:29
  • Try making use of the styles in the Android and use these styles in the view. Commented Feb 21, 2012 at 7:04

3 Answers 3

3

Use spannable class to do this:

 Spannable WordtoSpan = new SpannableString(myString);        
            WordtoSpan.setSpan(new ForegroundColorSpan(Color.RED), 0, myString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Sign up to request clarification or add additional context in comments.

Comments

2
final CharSequence[] items = {
    "Switch Off",
    "Timer Settings",
    Html.fromHtml("<font color=\"red\">Cancel</font>"),
};

Comments

0

Instead of string use TextView and change the background of it e.g

  TextView tv;
  tv.setText("yourText");
  tv.setTextColor(Color.CYAN);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.