‫In Android studio initializing and declaring views refer to the process of creating references to UI
‫elements or the views defined in the layout XML files so that you can interact with them programmatically
‫in Java or Kotlin code.
‫Let's take this text view as an example and let's define the most important attribute in all widgets,
‫which is the ID.
‫We start by Android ID equal, and here we specify at plus ID slash, and here you specify the ID for
‫this text view.
‫I'll add my text view as an ID for this text view and whenever I need to initialize, declare or use
‫this text view, I need to refer to this unique identifier which is the ID.
‫So in this example we declared a text view with id my text view.
‫Now let's move to the main activity dot Java.
‫Once you've declared a view in the XML layout, you need to initialize it in your Java or Kotlin code
‫to interact with it programmatically.
‫Initialization means creating a reference to the view using the ID you assign to it in the XML.
‫To do this, we use the find view by ID method.
‫What is the type of the view that we create?
‫It's a text view.
‫So we start by text view.
‫I'm creating an object of this class class text view and here I'll press alt plus enter to import the
‫class.
‫If we look up we are importing the Android widget dot text view.
‫Find the view by ID is a method provided by the Android framework and it searches for the view with
‫a specified ID.
‫I am specifying.
‫Here are the id dot.
‫Find the text view that it is named as my text view.
‫So this method will searches the view with the specified ID in the layout and returns a reference to
‫that view as a text view object.
‫Now you can use my text view to modify the text appearance or perform other actions on the text view
‫programmatically so you can access and modify this text view during the runtime and not during the coding.
‫So if I set here, set text, there are a lot of methods you can use set color, set appearance, set,
‫cursor, set, text and so on.
‫So everything you set or modify here programmatically will take place during the runtime.
‫It's important to note that when you use find the view by ID, you need to ensure that the view with
‫the specified ID exists in the layout XML file and the ID is unique within the layout.
‫If the view is not found or the ID is incorrect, the find view by ID method will return null and you
‫might encounter null pointer exceptions or other exceptions if you attempt to use the view without proper
‫null checks.