'Android Studio Kotlin setting UI elements from code

I want to set the UI elements from the code for an English learning App. There are more than 100 topics for each section of the app so I just created three pages of fragment changing UI elements according to the string I receive from getStringExtra. as follows:

when (activity?.intent?.getStringExtra("clickedGrammarTopic")) {

            "To Be - am/is/are" -> {
                binding.firstPageToolbarText.text = getText(R.string.tobe_amisare_fp_toolbar_text)
                binding.firstPageTextOne.text =  getText(R.string.tobe_amisare_fp_1)
                binding.firstPageTablePronoun.text = getText(R.string.tobe_amisare_table_pronoun)
                binding.firstPageTableAuxillaryVerb.text = getText(R.string.tobe_amisare_table_auxillary_verb)
                binding.firstPageTableVerb.text = getText(R.string.tobe_amisare_table_pronoun)
                binding.firstPageTablePronounI.text = getText(R.string.tobe_amisare_table_pronoun_I)
                binding.firstPageTableAuxAm.text = getText(R.string.tobe_amisare_table_aux_am)
                binding.firstPageTableVerbOne.text = getText(R.string.tobe_amisare_table_verb_one)
                binding.firstPageTableTranslation.text = getText(R.string.tobe_amisare_table_translation)
                binding.firstPageTablePronounYou.text = getText(R.string.tobe_amisare_table_pronoun_you)
                binding.firstPageTableAuxAre.text = getText(R.string.tobe_amisare_table_aux_are)
                binding.firstPageTableVerbTwo.text = getText(R.string.tobe_amisare_table_verb_two)
                binding.firstPageTableTranslationTwo.text = getText(R.string.tobe_amisare_table_translation)
                binding.firstPageTablePronounHe.text = getText(R.string.tobe_amisare_table_pronoun_he)
                binding.firstPageTableAuxIs.text = getText(R.string.tobe_amisare_table_aux_is)
                ....
            }

The problem here is whenever I try to add a new topic, I need to write over 50 lines of code. Is there an easier way to deal with this? If not, would this method have any negative effect on the overall performance of the app? (tried designing each page on .xml files ended up in a mess)



Solution 1:[1]

put your text/string in string array

    <string-array name="lesson_one">
        <item>item1</item>
        <item>item2</item>
        <item>item3</item>
        <item>item4</item>
    </string-array>

for UI element define a list of UI ids:

    private val ids = arrayListOf(
        R.id.text1,
        R.id.text2,
    )

and loop through it

        for (i in 0..binding.root.childCount) {
            if (binding.root.getChildAt(i) is TextView) {
                if (binding.root.getChildAt(i).id == ids[i]) {
                    binding.root.getChildAt(i).text = resources.getStringArray(R.array.lesson_one)[i]
                }
            }
        }

and get your text from string array, your code goes concise

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 OneDev