0

I am using collapsing Toolbar layout in my android application. while layout is collapse or expand in both case I need collapsing Toolbar text displaying my "amount" in green and "balance" in red. I had try to use HTML like below:

collapsingToolbar.setTitle(Html.fromHtml("<font color='green'>50</font><font color='red'>balance</font>"));

I had used SpannableString like below:

String part1 = " 50";
String part2 = "Balance";
SpannableString str = new SpannableString(part1 + part2);
str.setSpan(new ForegroundColorSpan(Color.parseColor("#ff0000")), part1.length(), str.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
collapsingToolbar.setTitle(str);

Both did not work in my case. Dose someone have some new idea to work in this case.

Philipp
  • 468
  • 3
  • 24
sofquestion 9
  • 167
  • 4
  • 12

2 Answers2

0

You should add a TextView inside the Toolbar widget and set it's text to the HTML formatted one.

Tiago Ornelas
  • 1,109
  • 8
  • 21
0

Please use this

StringBuilder builder = new StringBuilder();
Spannable word1 = new SpannableString("50");
    word1.setSpan(new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.green)), 0, word1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    collapsingToolbar.append(word1);
    Spannable word2 = new SpannableString("Balance");
    word2.setSpan(new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color. red)), 0, word2.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    builder.append(word2);
    collapsingToolbar.setTitle(builder);
Saurabh Vadhva
  • 511
  • 5
  • 12