0

I have the following function -

private fun initData() {
        tasksViewmodel.getGroupsAndTasksCounter().observe(requireActivity(), Observer {model ->
            if (model.groupsCount == 0 && model.tasksCount == 0) {
                return@Observer
            }
            val spannableStringBuilder = SpannableStringBuilder()
            val color = requireContext().getColor(R.color.black)
            val foregroundColorSpan = ForegroundColorSpan(color)

            val groupsBold = SpannableString(model.groupsCount.toString()
                .plus(" ")
                .plus(getString(R.string.groups)))

            val tasksBold = SpannableString(model.tasksCount.toString()
                .plus(" ")
                .plus(getString(R.string.groups_info_ongoing_tasks)))

            groupsBold.setSpan(foregroundColorSpan, 0, groupsBold.length, 0)
            groupsBold.setSpan(StyleSpan(Typeface.BOLD), 0, groupsBold.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            groupsBold.setSpan(RelativeSizeSpan(1.1f), 0, groupsBold.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            tasksBold.setSpan(foregroundColorSpan, 0, tasksBold.length, 0)
            tasksBold.setSpan(StyleSpan(Typeface.BOLD), 0, tasksBold.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            tasksBold.setSpan(RelativeSizeSpan(1.1f), 0, tasksBold.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)


            spannableStringBuilder.append(getString(R.string.groups_info_participating_in)
                .plus(" ")
                .plus(groupsBold)
                .plus(" ")
                .plus(getString(R.string.groups_info_and_you_have))
                .plus(" ")
                .plus(tasksBold)
                .plus(getString(R.string.dot)))

            spannableStringBuilder.toString()


            sharedInformationViewModel.value.groupsInfoString.value = spannableStringBuilder.toString()
        })
    }

and the following shared viewmodel -

class SharedInformationViewModel : ViewModel() {

    ....

    val groupsInfoString = MutableLiveData<String>()
}

and I am using the string value in other fragments -

private fun initData() {
        sharedInformationViewModel.value.groupsInfoString.observe(requireActivity(), Observer { info ->
            binding.groupsGroupsAndTasksCounter.setText(info, TextView.BufferType.SPANNABLE)
        })
    }

The string does indeed come out correct.

The issue I am facing is that for some reason the spannablestring does not change the color and the does not make the text bold, even though this piece of code is a duplicate of another in my app.

What am I missing?

Alon Shlider
  • 1,187
  • 1
  • 16
  • 46
  • 1
    Calling `toString()` on your `SpannableStringBuilder` is returning a plain `String` again, without all of the spans you just added. `String`s cannot hold any formatting information. Get rid of the `toString()` call, and change your `groupsInfoString` to a `MutableLiveData`. – Mike M. Aug 09 '20 at 17:54
  • Just did that, and got the same result (plain String without bold and color change) using a `MutableLiveData`. Am I missing anything? – Alon Shlider Aug 09 '20 at 19:02
  • 1
    Did you remember to remove `toString()` from `sharedInformationViewModel.value.groupsInfoString.value = spannableStringBuilder.toString()`. You also have a lone `spannableStringBuilder.toString()` line that isn't really doing anything, so you can get rid of that, too. – Mike M. Aug 09 '20 at 19:05
  • Just checked and yes indeed I removed it and nothing happens. Using the charsequence for filling the textview and getting the same result – Alon Shlider Aug 09 '20 at 19:17
  • 1
    OK, I see it now. I misread your code a bit, and thought the `plus()` method was some extension I wasn't familiar with. You're treating your formatted texts as `String`s in constructing `spannableStringBuilder`, so the formatting is being stripped before you would even get to that `toString()`. You should be `append()`ing all of those things onto the `spannableStringBuilder`, instead of `plus()`ing them onto the first `getString()` return. Follow me? `spannableStringBuilder.append(getString(R.string.groups_info_participating_in)).append(" ").append(groupsBold).append(" ")...` – Mike M. Aug 09 '20 at 19:33
  • Thank you very much - worked. Please comment and I will approve. – Alon Shlider Aug 10 '20 at 08:26

0 Answers0