2

This is probably a really simple question to answer but at the moment I can't sort it.

I have my action set up as follows:

// get action bar
ActionBar actionBar = getSupportActionBar();

// set up the action bar layout
final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater().inflate(R.layout.actionbar_layout,null);

// apply layout
actionBar.setCustomView(actionBarLayout);

I have tried to set up the action bar title as follows:

  // setting action bar title
  actionBar.setTitle(getResources().getString(R.string.testing_title));

That applies the title correctly, the problem is testing_title is formatted in the strings file as follows:

 <string name = "testing_title"><b>Testing</b>Testing</string>

So the first 'Testing' should be bold and the second normal. The problem is that neither are emboldened. Would anyone know what I should do here?

In my action bar custom layout I had a text view which I was using to display the title and found an answer on here similar to this:

  private void setActionBarTitle(String title, ActionBar ab)
    {
        View v = ab.getCustomView();
        TextView titleTxtView = (TextView) v.findViewById(R.id.actionBarTitle);
        titleTxtView.setText(title);
    }

I then called this like so:

setActionBarTitle(getResources().getString(R.string.audioWalks_title), actionBar);

This however didn't produce any results. If anyone could be on any help I'd appreciate it.

DJ-DOO
  • 4,545
  • 15
  • 58
  • 98
  • My first guess - but just a guess - is that the string is interpreted as a string. But if you have a look at [this answer here](http://stackoverflow.com/a/1533512/1798304), you need to use `Html.fromHtml(String)`. So decoding your String and feeding this to the `fromHtml` might work. – MalaKa Feb 04 '14 at 11:23
  • hi thanks for your quick reply, the problem is that I want to keep all my strings in the string.xml and not be creating random strings in different classes. When I set the text to the text view within the actionbar layout file and set it to @string/... it kept the formatting. – DJ-DOO Feb 04 '14 at 11:27

3 Answers3

1

The error must be here

// setting action bar title
actionBar.setTitle(getResources().getString(R.string.testing_title));

The java.lang.String can't contain spans. Try getting the CharSequence reference.

actionBar.setTitle(getResources().getText(R.string.testing_title));
Yaroslav Mytkalyk
  • 16,950
  • 10
  • 72
  • 99
1

As I suggested in the comment, I would recommend to use the Html.fromHtml(String) method. You can still keep the Strings in your resource, that's why I said decode ;)

So I found something very interessting for you on android-developers. If you are not using formatted numbers, you just need to do this:

private void setActionBarTitle(String title, ActionBar ab)
{
    View v = ab.getCustomView();
    TextView titleTxtView = (TextView) v.findViewById(R.id.actionBarTitle);
    titleTxtView.setText(Html.fromHtml(title));
}
MalaKa
  • 3,734
  • 2
  • 18
  • 31
0

Try with this :

private void setActionBarTitle(String title, ActionBar ab)
    {
        View v = ab.getCustomView();
        TextView titleTxtView = (TextView) v.findViewById(R.id.actionBarTitle);
        titleTxtView.setTypeface(null, Typeface.BOLD);
        titleTxtView.setText(title);
    }
T_V
  • 17,440
  • 6
  • 36
  • 48
  • That method didn't work for me, it just displayed the default application label in the actionbar. Also I have titles with some of the title embolden. For example The Great of China These will vary from activity/fragment to activity/fragment – DJ-DOO Feb 04 '14 at 11:25