A computer is a machine that follows a list of instructions called a program. An Android device is a computer and an app is a program, written in the language Java. Inside the device are containers called variables that hold values such as numbers or pieces of text. An object is a big variable that can contain smaller variables inside it. Attached to an an object we can have lists of instructions—in effect, little programs—called methods.
There are many classes (types) of objects. For example, an object of class ImageView can display an image on the screen, and an object of class TextView can display a piece of text. We usually create these View objects by writing an layout file, in the language XML, that describes them and their positions on the screen.
Every object of a given class has the same set of methods attached to it. For example, every object of class Activity has methods that create the objects of the user interface: the ImageViews, TextViews, Buttons, etc., that the user sees on the screen.
After creating the View objects, an Activity object might also need to configure them individually. That’s why each View object can have an ID number. These numbers make it possible for the part of the app written in Java to communicate with the part written in XML. More specifically, these numbers make it possible for the methods of the objects created in Java to call the methods of the objects created via XML.
The ID number of each View is stored in a Java variable that can be passed to the methods of the Activity. This variable is created by writing its name in the description of the View object in the layout file. For example, the code sample creates a variable named R.id.today that holds the ID number of a TextView. It does this by writing the attribute android:id="@id+/today" in the TextView’s description in the layout file. (The R stands for “resource”.)
Every Activity object has a method named findViewById, which finds a View object given its ID number. The code sample passes the variable R.id.today to this method to find the TextView. If all goes well, the return value (the result produced by the method) will be a reference to the TextView, which is a piece of information that will allow the Activity to call the methods of the TextView. To use this reference conveniently, we must store it into a special-purpose variable capable of holding only this one type of value, a “reference to a TextView”.
Since findViewById can be called upon to find View objects of many different classes (TextViews, ImageViews, etc.), the value that it returns is a general-purpose reference that can refer to an object of any of these classes. Before it can be stored into the special-purpose variable, the return value must be cast (converted) from general to special. A conversion in this direction is called a downcast and is performed by the word TextView in the parentheses.
Another requirement is that findViewById cannot be executed before setContentView, which is the method that creates the TextView and other View objects described in the layout file.