Reputation: 395
my actionbar title is 2 word, i want to use 2 color for each word like this image :
now i`m using these code to change text color:
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:actionBarSize">45dp</item>
<item name="actionBarSize">45dp</item>
<item name="android:actionBarStyle">@style/MyActionBar</item>
<item name="actionBarStyle">@style/MyActionBar</item>
<item name="android:windowActionBarOverlay">true</item>
</style>
<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar">
<item name="android:background">@color/actionbar</item>
<item name="background">@color/actionbar</item>
<item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
<item name="android:subtitleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
<item name="titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
<item name="subtitleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
</style>
<style name="MyTheme.ActionBar.TitleTextStyle" parent="@android:style/TextAppearance">
<item name="android:textColor">@color/white</item>
<item name="android:textSize">15sp</item>
</style>
but those code change both word color, so how can i do that?
Upvotes: 2
Views: 141
Reputation: 198
Put it in your oncreate()
String text = "<font color='#F44336'>Your</font><font color='#FF9800'>Title</font>";
getSupportActionBar().setTitle(Html.fromHtml(text));
Upvotes: 3
Reputation: 6356
Try with spannable
String part1 = "Actionbar ";
String part2 = "TITLE";
SpannableString s = new SpannableString(part1 + part2);
s.setSpan(new ForegroundColorSpan(Color.RED), part1.length(), part1.length()+part2.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// Update the action bar title with the spannable instance
getActionBar().setTitle(s);
Upvotes: 0
Reputation: 2436
Create a separate style for your subtitle. Add this in bottom:
<style name="MyTheme.ActionBar.SubTitleTextStyle" parent="android:style/TextAppearance">
<item name="android:textColor">@color/black</item>
<item name="android:textSize">12sp</item>
</style>
Then change the
<item name="@android:subtitleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
<item name="subtitleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
to
<item name="android:subtitleTextStyle">@style/MyTheme.ActionBar.SubTitleTextStyle</item>
<item name="subtitleTextStyle">@style/MyTheme.ActionBar.SubTitleTextStyle</item>
Upvotes: 1