We know that when we call setContentView
in the onCreate
method, Android inflates all the widgets and layouts, and turns them into real Java objects on the Heap.
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
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 = (TextView) 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 appear as follows:
myTextView.setText("Hi there");
Make it disappear like this:
// Bye bye myTextView.setVisibility(View.GONE)
And then change its text again and make it reappear:
myTextView.setText("BOO!"); // Surprise...