Reputation: 1722
I was unable to find any question on StackOverflow about this:
I dynamically want to add TextView
objects to a ScrollView
(see XML layout) using Kotlin.
The simplified XML structure:
<ScrollView>
<LinearLayout android:id="@+id/entry_list" />
</ScrollView>
What the app should look like after I execute the Kotlin:
<ScrollView>
<LinearLayout android:id="@+id/entry_list" />
<TextView android:text="Dynamically added text" />
</LinearLayout>
</ScrollView>
(note: The actual XML will not be changed in Runtime, this just describes what it should look like afterwards)
How can I use Kotlin to achieve this?
Upvotes: 0
Views: 4562
Reputation: 1722
I did some research on this, it is quite simple:
val dynamicTextview = TextView(this)
dynamicTextview.text = "Dynamically added text"
// add TextView to LinearLayout
entry_list.addView(dynamicTextview)
(note: you should import TextView
, if you use Android Studio, it should prompt you to import that automatically)
Just add this code somewhere in the Activity you want to add the TextView
to.
Upvotes: 2