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?