‫Welcome back.
‫In this video we're going to learn about declaring and initializing views in Kotlin.
‫Declaring and initializing views refers to the process of defining and setting up references to user
‫interface elements, which are the views that you want to work with in your Kotlin code.
‫This process is crucial for interacting with and manipulating the views within your Android app.
‫We have this only view which is a text view.
‫It has an attribute ID which is text view.
‫You can define any view I can give it.
‫For example text view 77 for example.
‫Now let's move to the main activity wkt file.
‫You first need to declare variables to hold references to the views you want to work with.
‫These variables should be declared in the scope of the activity or the fragment class, so that they
‫can be accessed throughout your code.
‫You can declare view references as class level properties or local variables in a function, depending
‫on your needs.
‫I'll start with the class level properties or the general and global variables here I'll start with
‫late.
‫Initiate var text view.
‫It's of type text view and I'll name this text view as my text view late initiate.
‫This is a Kotlin modifier that is used for non-null properties, which are initialized at a later point
‫in your code.
‫It tells the compiler that the property will be initialized before it is used, even though it doesn't
‫have any initial value at the point of declaration.
‫This is commonly used for Android views because they are typically initialized in the Oncreate method.
‫After setting the content view, the var keyword declares the property as a mutable variable, meaning
‫that you can change its value after it's initialized.
‫My text view.
‫This is the name of the property.
‫You can choose any valid variable name for your property.
‫Text view.
‫This is the type of the property indicating that text view or my text view variable will hold a reference
‫to a text view object, and the initialization usually happens within the Oncreate method or a similar
‫lifecycle method.
‫After calling Setcontentview to set the content of view, let's move to the Oncreate method.
‫This is a callback method or a lifecycle method, and later on we'll talk about these callback methods
‫in deep details.
‫So please skip this for later lessons.
‫Setcontentview.
‫This method in Android is used to set the content view of an activity or a fragment to a specified layout
‫resource.
‫In this code, you you are telling Setcontentview method to use the activity underscore Mainpage.xaml
‫file that we defined here.
‫To set the content view of this activity called main activity Wkt file.
‫Again, this method is used to set the user interface layout for the given component.
‫R dot layout dot activity underscore main.
‫This is a reference to the layout resource that you want to use as the content view, and in this case
‫activity underscore main.
‫And if we go to the layout folder you can see it activity underscore main.
‫So I'm referencing this layout to this main activity.
‫The R class is generated by Android build system and it contains references to all resources such as
‫layouts, strings drawables in your Android project.
‫In this case, our layout dot activity underscore main refers to the XML file layout file that it's
‫placed in the layout folder called activity underscore main dot XML file.
‫So I am linking this file, this KTX file with this dot XML file.
‫And after the Setcontentview method, we need to start initializing the views that we declared in our
‫layout file.
‫Here we have only one text, so I'll start with it.
‫My text view equals to find the view by id r.id dot text view 77.
‫This is.
‫The variable or property declared here, holding a reference to a text view widget equals.
‫This is the assignment operator which assigned the result of findviewbyid to the text my text view variable
‫and find the view by id.
‫This is a method called that searches for a view in the current layout based on its unique resource
‫ID, which is the unique identifier which is specified as our ID dot text view 77.
‫It returns a reference to the found view, which in this case a text view.
‫By executing this code, you are initializing the My Text view variable with a reference to the text
‫view widget defined in your layout Xaml file.
‫This allows you to access and manipulate the properties and the contents of text view in your Kotlin
‫code.
‫For example, I can use my text view to set and change the text of this text view.
‫For example.
‫Welcome again!
‫If we go back to the layout, our text.
‫Welcome to our course.
‫If I run the program, it will give us welcome again, which is the text that we defined programmatically.
‫So whenever you initialize the views, you can use them to access and manipulate the properties and
‫the contents of these views in Kotlin files.
‫So this is the process of creating a new view in the layout dot XML files.
‫Then you need to declare the view and initialize it in the Oncreate method.