1

I found this question, and that works fine. But I have a resource string which is a format string that goes into String.format.

Consider R.string.test to be This is a test: <b>%1$s</b>.

Then this code:

getActionBar().setTitle(String.format(getString(R.string.test), "TEST"));

does not show bold, but this does (but it doesn't show the replaced string obviously):

getActionBar().setTitle(getResources().getText(R.string.test));

How can I use String.format to create a formatted title?

Community
  • 1
  • 1
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
  • Use a Spannable to set the text font, the current way you have it, Android will just ignore it. See [this question](http://stackoverflow.com/questions/3282940/set-color-of-textview-span-in-android) on how to achieve it. – t0mm13b May 23 '16 at 13:57

2 Answers2

1

String.format has this syntax, for example : String.format("%s %d %f", "string1, myInt, myDouble); Your variable types and formatting go in the quotes, prefaced by %, then after the quotes and the comma, you add your variables - the must be of the type specified in the quotes, or it will probably crash.

Richard Goodman
  • 307
  • 3
  • 7
1

Did you tried Html.fromHtml:

getActionBar().setTitle(Html.fromHtml("<font color=#343434>" + "Your Text" + "</font>"));

I have another too , this is a Html string with multiple space :

getActionBar().setTitle(Html.fromHtml("<font color=#FFFFFF>" + "&nbsp&nbsp&nbsp&nbsp&nbsp" +"Your Text" + "</font>"));

Also you can directly go for :

setTitle(Html.fromHtml("<font color=#343434>" + "Your Text" + "</font>"));

enter image description here

Yasin Kaçmaz
  • 6,573
  • 5
  • 40
  • 58