Midhun VP
Midhun VP

Reputation: 629

How to give color to string in the java code of android?

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"};

Upvotes: 0

Views: 478

Answers (3)

sachy
sachy

Reputation: 789

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

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

Upvotes: 0

Julian Fondren
Julian Fondren

Reputation: 5619

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

Upvotes: 2

Use spannable class to do this:

 Spannable WordtoSpan = new SpannableString(myString);        
            WordtoSpan.setSpan(new ForegroundColorSpan(Color.RED), 0, myString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

Upvotes: 3

Related Questions