Declaring and initializing the objects
We know that when we call the setContentView
method from the onCreate
method, Android inflates all the widgets and layouts and turns them into real Java objects on the heap.
Additionally, we know that to use a widget from the heap, we must first declare an object of the correct type and then use it to get a reference to the UI widget object on the heap by using its unique id
property.
For example, we get a reference to a TextView
widget with an id
property of txtTitle
and assign it to a new Java object, called myTextView
, as follows:
// Grab a reference to an object on the heap TextView myTextView = findViewById(R.id.txtTitle);
Now, using our myTextView
instance variable, we can do anything that the TextView
class was designed to do. For example, we can set the text to show the following:
myTextView.setText("Hi there");
Additionally, we can make it disappear as follows:
// Bye bye myTextView.setVisibility(View...