0

I am using a fragment list, so I have a child .xml to manage the formatting of each bit of text within each drop down. How do I manipulate my code to embold my heading?

ItemClass

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {

    val rootView = inflater.inflate(R.layout.fragment_list, container, false)
    val parent1 = Parent(0, "Wudu")
    val childItems1 = ArrayList<Child>()
    childItems1.add(Child(parent1, 0, getString(R.string.wudu)))

            parent1.childItems.clear()
    parent1.childItems.addAll(childItems1)

My String code

<string name="wudu">

    <b>Introduction</b>
    "\n
    What is most important is to set intentions…
</string>

Child XML

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingStart="16dp"
    android:paddingTop="16dp"
    android:paddingEnd="16dp"
    android:paddingBottom="16dp"
    android:textAlignment="center"
    android:textColor="#000000"
    android:textSize="20sp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:text="Text" />

</androidx.constraintlayout.widget.ConstraintLayout>

You can see the result in the image. The text is not bold. I believe the container overrides the string formatting: Image

james
  • 5
  • 3
  • I'm using a template off the net... so if it the nature of the container to apply to everything within it.. then how do I amend the code in ItemClass to not use the container... – james Jun 11 '22 at 22:27
  • `getString(R.string.wudu)` – I'm not sure how you're handling this everywhere else, but right there is at least one problem. Plain `String`s do not have any formatting, like bold or underline. You'll need to handle the text as a `CharSequence` instead, and use the `getText()` function to retrieve it from resources, rather than `getString()`. – Mike M. Jun 11 '22 at 22:31
  • I should've mentioned, if you don't need to do anything with that text other than set it on a `TextView`, then you don't really need to mess with `String`s or `CharSequence`s at all. You can set the resource identifier directly on the `TextView`, and it will handle loading the formatted text itself; e.g., `titleTextView.setText(R.string.wudu)`. You'd have to change the `Child` class a bit, though, since it would be using an `int` now for the `R.string` value, instead of a `String`/`CharSequence`. – Mike M. Jun 11 '22 at 23:58
  • I tried titleTextView.setText(R.string.wudu) but titleTextView is red even if I create a local variable – james Jun 16 '22 at 23:25
  • That was simply an example usage. I have no idea what the names of your `TextView` variables are because you've not included that part of your code. – Mike M. Jun 16 '22 at 23:26
  • @MikeM. The simplest method to do this is to use getText() instead of getString() and then convert the 'R.string.wudu' from title to charsequence. I believe this wis what you meant in your comment on Jun 11 at 22:31. If you want to submit as answer, I can mark as accept – james Jun 20 '22 at 22:28
  • Yep, that's what that one was suggesting. I'm good, however. :-) It was nothing major; just a quick tip. Also, when you indicated that the linked possible duplicate had solved your problem, this question was closed to be marked as such, and you can't post answers on closed questions. Just a heads up, for future reference. Thank you, though. I appreciate the offer. Glad you got it working. Cheers! – Mike M. Jun 20 '22 at 22:45

1 Answers1

0

If you want to use html-tags from string resources, you have to use Html.fromHtml function for set this changes, for example:

title.text = Html.fromHtml(getString(R.string.wudu))
  • title does not seem to be recognized (in red), even if I create a local variable – james Jun 16 '22 at 23:24
  • @James I think you need to do some more effort in doing your own research and understand the technology before asking the follow up questions. There are plenty of answers on this in [stack](https://stackoverflow.com/questions/3235131/set-textview-text-from-html-formatted-string-resource-in-xml) and other places – rahu1613 Jun 17 '22 at 14:31
  • You're right, but I couldnt find the solution with correct tags. Many of them relate to the xml, whereas Im concerned with the java class/activity. – james Jun 20 '22 at 22:29