We've created a simplified version of the TextView class
which would be saved in the TextView.java file.
We simplified it so it would be easier for
you to understand what a class would look like.
First, it starts with an access modifier.
It's marked as public so
that other things like inactivity can use this TextView class.
Then we have the word class.
This indicates to java that this is a class definition.
Then we have the name in a class.
The name of a class in Java starts with a capital letter and it's in camel case.
So there's no spaces and it capitalizes subsequent words.
There's some other text here but we'll look into that later.
The important thing is we have an opening brace and
then at the bottom of the definition we have a closing brace.
Now inside this TextView class we have a bunch of variables here.
Each of these represent a field in a TextView, so we have a field for
the text of the TextView and a field for the text color of the TextView.
There are other fields as well, but remember,
this is a simplified version of the TextView class.
You also have a variable to store a reference to the context of
the application.
You can read more about context in the instructor notes below, but
it's basically a way to access information about the app, like for
example you can access resources in the app using the context.
And these are the methods of the TextView.
The setText method takes as input a text string.
Then it updates the global variable mText to use this new value.
Similarly the setTextColor takes as input a new color and
it updates the internal state to have this new color value.
There's also methods like getText and
getTextColor, which return the internal text string, or the internal text color.
One more thing, you may have noticed that these global variables here all
start with the lower case m.
M stands for member variable, and
these are all member variables of this TextView class.
Member variable is just another way to say field or
the state of the TextView class.
Here's a simplified ImageView class.
You would find it in the ImageView.java file.
It starts with public class and then ImageView is the name of the class.
And then we have opening and closing braces.
This makes up the state of the ImageView.
Normally it would have more fields than this, but
I just want to show you an example.
This is an integer variable and it's called mImageId, m for member variable.
This stores the resource id for
the image that should be shown in this ImageView.
Down here, we have the constructor which is used to construct
a new ImageView object.
And it takes this input of Context.
And then it initializes these variables.
Then we have a method.
It's called setImage and it takes this input, a resource ID, for
the new image that should be shown in this ImageView.
Inside this method, we update the internal variable for image ID.
Again, we created the simplified TextView and
ImageView classes to help you understand the concept of classes.
Now let's look at the real version of these classes inside
the Android framework.
To see the code for
the Java classes in Android, you can install this Chrome extension.
If you're not using the Chrome browser,
you can see the instructor notes below for how to access these files.
Okay.
Once that Chrome extension is installed, then when I search for, something like
TextView Android, and I go look at the reference documentation for this class,
then there's going to be this extra link here that says view source.
This is really for advanced developers.
If you're curious about the code that is used to create this TextView.
This is the TextView.java file that was defined by someone on the Android team.
As you can see, it's really long and complex.
But it still has the main parts of a class.
It starts with public class and then TextView, the name of the class, and
then this is the opening brace and
the closing brace is all the way at the bottom here.
At the top of the class, we see a bunch of fields here, and
this makes up the state of the class.
For example, we see that TextColor is a field, we also see that text is a field.
And here's the constructor for TextView that takes this input a context.
If I hit Cmd+F, or Ctl+F if you're on Windows, then I can try to search for
setText.
And here's the setText method.
This method is going to set the string value of the TextView.
And if I search for GetText and
I also see this method returns the text that the TextView is displaying.
You get that idea, and you can browse through this on your own if you want.
If I search for the ImageView documentation page,
then I can click on view source to view all the code for the ImageView class.
The file says public calls ImageView, and
then these are the fields that make up the state of the ImageView,
and then there's a bunch of methods at the bottom of the file.
The code in this file is pretty complex as well,
and it's also thousands of lines of code.
The best part about object oriented programming,
though, is that you don't need to know about all these details.
As a developer, you can use TextView and ImageView in your app
without knowing about all the gory details of what's going on inside of it.