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:
- Click on File, then New and then New Project and give name whatever you like
- Then, select Kotlin language Support and click next button.
- Select minimum SDK, whatever you need.
- Select Empty activity and then click finish.
Modify activity_main.xml file
Second step is to design our layout page. Here, we will use the RelativeLayout to get the TextView from the Kotlin file.
Create TextView in MainActivity.kt file
Open app/src/main/java/yourPackageName/MainActivity.kt. In this file, we declare a variable TextView to create the TextView widget like this:
val textView = TextView(this) //setting height and width textView.layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
then add the widget in layout using this:
// Add TextView to LinearLayout layout?.addView(textView)
Run as Emulator:
- TextView in Kotlin
- Dynamic ProgressBar in Kotlin
- Dynamic CheckedTextView in Kotlin
- Dynamic Spinner in Kotlin
- Dynamic ImageSwitcher in Kotlin
- Dynamic TextSwitcher in Kotlin
- Dynamic TimePicker in Kotlin
- Dynamic ImageView in Kotlin
- Dynamic TextClock in Kotlin
- Dynamic RatingBar in Kotlin
- Dynamic SeekBar in Kotlin
- Dynamic Switch in Kotlin
- Dynamic RadioButton in Kotlin
- Dynamic ImageButton in Kotlin
- Dynamic DatePicker in Kotlin
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.