How To Display Text in TextView Using Kotlin

How To Display Text in TextView Using Kotlin: Android Studio Kotlin Example

In this example we will learn how to display any text in a TextView with button click using KOTLIN language. So, lets start How To Display Text in TextView Using Kotlin.

 

How To Display Text in TextView Using Kotlin:

1- Create a new Android Studio Project and name it ” DisplayText “. Also you name it whatever you like.

2- Go to “activity_main.xml” file and first of all change the default Constrain Layout to Relative Layout.

3- Here in our “activity_main.xml” file we need one EditText, one TextView and then one Button.

4- We will use EditText to write our text. TextView will display that text and Button will be used to display that text in Textview on Click event.

5- After designing Our EditText using some properties our EditText will look like this:

 

6- After EditText we will use a TextView.

7- After adding Id and some properties our TextView will look like this:

 

8- So now, we will take a Button.

9- After adding Id and some properties our Button will look like this:

 

10- Finally we are done with our “activity_main.xml” file. So, our complete “activity_main.xml” will look like this:

How To Display Text in TextView Using Kotlin :

11- Now, come to your ” MainActivity.kt “ file. Here we will provide the ids for our contents and we will use an OnClickListener on our Button.

12- First we will define our views ( EditText, TextView and Button ).

13- We will use ” lateinit “ because we do not have any default values for our Views.

 

14- Now, in our onCreate Method we are going to provide the Ids for our views like this:

 

15- After that we will use an interface “OnClickListener”. We will add it on the top of our activity like this:

 

16- After adding “OnClickListener” we have to import the method of this abstract interface. We can do that simply by pressing ALT+ENTER.

17- We will register an OnClickListener on our Button in our onCreate method like this:

 

18- So, in our onClick function outside onCreate method now we will create a variable ” name “ and we will store our text from EditText in it.

19- Finally, we will use “text” method to get our text from EditText and store it in our TextView with the help of “text” method again like this:

 

20- Finally our complete “MainActivity.kt” file will look like this:

 

 

OUTPUT

 

How To Display Text in TextView Using Kotlin
How To Display Text in TextView Using Kotlin

 

How To Display Text in TextView Using Kotlin
How To Display Text in TextView Using Kotlin

 

 

 

 People Also Read:

 

 

 

 

 

 

 

2 thoughts on “How To Display Text in TextView Using Kotlin: Android Studio Kotlin Example”

  1. Pingback: RecyclerView With Data Binding in Android Kotlin: Example With Kotlin

  2. Pingback: Android Custom Toolbar In Kotlin | Toolbar Kotlin Example - My Android

Leave a Comment

Your email address will not be published. Required fields are marked *