![]() |
VOOZH | about |
Android TextView is an user interface that is used to display some text to the user. In this article we will be discussing how to programmatically create a TextView in Kotlin .
Let’s start by first creating a project in Android Studio. To do so, follow these instructions:
Second step is to design our layout page. Here, we will use the RelativeLayout to get the TextView from the Kotlin file.
activity_main.xml:
Navigate to app > src > main > java/kotlin > {package-name} > MainActivity.kt. In this file, we declare a variable TextView to create the TextView widget like this:
val textView = TextView(this)
textView.layoutParams= LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
then add the widget in layout using this:
layout.addView(textView)MainActivity File: