4

i would like to have an actionbar title like "titlepart1titleparttwo". But as I understand it correctly through a theme you can either set the whole title on bold or not but not partly. Is there any workaround?

<style name="MyTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:background">#81CFEB</item>
    <item name="android:textStyle">bold</item>
</style>
SnafuBernd
  • 261
  • 4
  • 16

2 Answers2

11

No need to use themes, actually.

CharSequence instances can have markup information. You can add Spans manually, or you can use the Html class to make it easier. For example:

actionBar.setTitle(Html.fromHtml("titlepart1<b>titleparttwo</b>"));
matiash
  • 54,791
  • 16
  • 125
  • 154
  • this is Great! but why does it say method invocation may produce null pointer? – fullmoon Jun 05 '16 at 09:14
  • @fullMoon Probably because `getActionBar()` might return null on activities that don't have one. You can check for `if (actionBar != null) ...` if you want. – matiash Jun 05 '16 at 18:35
3

What matiash answered is good but it is not the native method

You should try this, also this will be faster

getSupportActionBar().setDisplayShowTitleEnabled(true);
SpannableStringBuilder str = new SpannableStringBuilder("HelloWorld");
str.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), 5, 10,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
getSupportActionBar().setTitle(str);
Vishal Singh
  • 1,341
  • 14
  • 13