0

This is my code in my class for my app. I am essentially displaying three expandable lists. Instead of adding the text to this class, how can I add a string instead? I've tried the way you add string to xml but does not work.

I need the ability to format text too, like change the size or embolden. Anyone what code type I need to use?

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
 import java.util.*

class ItemsFragment : Fragment() {

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

    val rootView = inflater.inflate(R.layout.fragment_list, container, attachtoroot: false)

    val parent1 = Parent(id:0, title:"MAC")
    val childItems1 = ArrayList<Child>()
    childItems1.add(Child(parent1, id:0, title:"Sample Text here"))
    parent1.childItems.clear()
    parent1.childItems.addAll(childItems1)

    val parent2 = Parent(id:1, title:"Stack")
    val childItems2 = ArrayList<Child>()
    childItems2.add(Child(parent2, id:9, title:"Joy"))
    childItems2.add(Child(parent2, id:10, title:"King))
    childItems2.add(Child(parent2, id:11, title:"Wink"))
    parent2.childItems.clear()
    parent2.childItems.addAll(childItems2)

    val parent3 = Parent(id:2, title:"TESO")
    val childItems3 = ArrayList<Child>()
    childItems3.add(Child(parent3, id:12, title:"Sting))
    childItems3.add(Child(parent3, id:13, title:"Run))
    childItems3.add(Child(parent3, id:14, title:"Fridge"))
    parent3.childItems.clear()
    parent3.childItems.addAll(childItems3)

    val itemList = ArrayList<Item>()
    itemList.add(parent1)
    itemList.add(parent2)
    itemList.add(parent3)

    val list = rootView.findViewById<RecyclerView>(R.id.list)
    list.adapter = ItemsAdapter(itemList)

    return rootView
}

companion object {
    fun getInstance() = ItemsFragment()
}
}
james
  • 5
  • 3

1 Answers1

0
rahu1613
  • 111
  • 9
  • where it says "Sample Text here", I want to use a stringid instead of hardcoded. – james Jun 09 '22 at 14:23
  • @James if you go through the above mentioned [blog](https://guides.codepath.com/android/Working-with-the-TextView), it have the details about the same thing ! – rahu1613 Jun 09 '22 at 21:32
  • sorry you will have to be more specific to which section. As far as I can see this applies to textviews which are used in .xml files. Im talking about class files. the blog on strings is basics on how to use strings. nothing on referencing them. – james Jun 10 '22 at 13:10
  • You can us it as getString(R.string.hello) Ref [Android documentation](https://developer.android.com/guide/topics/resources/string-resource#String) for more details – rahu1613 Jun 10 '22 at 16:46
  • Yes it did, you may be able to help with my follow up question: https://stackoverflow.com/questions/72588255/format-different-parts-of-text-that-is-in-a-container-in-android-studio?noredirect=1#comment128224576_72588255 – james Jun 16 '22 at 23:03