-
Title:
Find View by Id - Solution
-
Description:
-
Let's look at number 1.
-
Before I reveal the answer,
I'm going to copy this code and
-
paste it into Android Studio.
-
I want to show you what kind
of error would come out.
-
Now when you were solving this quiz,
you didn't need to use Android Studio,
-
you could have solved it by just
looking at the code here and
-
then writing in the answer.
-
So, whether or not you use
Android Studio, it's fine with me.
-
To make the code work,
I created a new project and
-
then I modified the activity
main layout file so that it
-
would have a bunch of views with these
ID's that we're going to be referencing.
-
I'm not even sure what this
layout would look like, but
-
I just wanted to put the views in here
with the corresponding view ID names.
-
That way, my code will work here.
-
So for number one, I pasted the code
here, and I see that there's an error.
-
Android Studios says it cannot
resolve the method setText.
-
If you remember from the last video,
-
I mentioned that setText
is a TextView method.
-
That means you can only call
this method on TextView objects.
-
In this case,
we're calling it on nameTextView.
-
A nameTextView is declared
to be a view object,
-
we need to change this
to be a TextView object.
-
Then we get an error saying
there is incompatible types.
-
A textview, an object of type
TextView is required, but
-
instead it found an object of type view.
-
That's because find view
by ID returns a view, and
-
this left-hand side requires a TextView.
-
That means we have to add this to cast
-
the view returned by
findViewById into a TextView.
-
Then we can store this whole thing,
-
which is the nameTextView,
into the left-hand side variable.
-
Then the error goes away, and we can
set text, "Laura" on this TextView.
-
The error is on line 1, so
I wrote the correct code right here.
-
The general explanation is that we need
to cast this view into a TextView and
-
then store that inside
a nameTextView variable.
-
That way,
we can call nameTextView.setText,
-
which is a TextView method,
on the next line of code here.
-
Let's move on to number two.
-
I'm going to copy this code and
put it into Android Studio.
-
Here's the code I pasted in.
-
The first line of code seems to
be correct, there's no errors.
-
But the second line has
a red underline and
-
it says there's
an incompatible types error.
-
It says, a string is required but
instead it found an int.
-
To understand this better,
-
we should look up the method getMaxLines
within the TextView documentation.
-
In the TextView documentation,
I'm going to search for
-
the getMaxLines method.
-
And here it is.
-
There's no input parameters, and
-
the return value of this
method is an integer.
-
So that's why Android studio was
complaining, because this method,
-
if it's returning an integer, but
-
we were trying to store it
into a String variable.
-
As a result, I need to change this data
type of the variable to be an int.
-
Then the error goes away.
-
So, the return value of
this method is an integer.
-
So, we can store it inside
this maxLines variable.
-
There is a warning still remaining.
-
Saying that the variable
maxLines is never used.
-
But this is just a code snippet.
-
I assume that if this
was in a regular app we
-
would actually want to
use this variable later.
-
So, the error's on line two and
here's the updated code.
-
The general explanation is that
the return value of this method should
-
be an integer.
-
Let's move on to number three.
-
Here's a code for number three.
-
I copied and pasted the code for
number three in Android Studio.
-
There's an error message that
says incompatible types.
-
An ImageView was required,
but instead it found a view.
-
This is similar to the error
we just saw with the TextView.
-
This method returns a view.
-
And, on the left-hand side, we're
trying to store it inside an ImageView.
-
So we need to cast this view
object into an ImageView.
-
This is valid because the view with
the ID icon actually is an ImageView,
-
so it's okay to cast
it into an image view.
-
Now that the right-hand side
evaluates to an ImageView,
-
we can store that into the variable
on the left-hand side.
-
This variable has a data type ImageView
so now everything matches up and
-
the error is gone.
-
On the next side,
we can call an ImageView method,
-
SetImageResource, onto
this IconImageView object.
-
There is one remaining error saying
that it cannot resolve symbol logo.
-
That's because I just didn't add
an image in our app with the name logo.
-
If I added that, then the error would
go away, so our code here is correct.
-
Here's the correct code where I
cast this view into an ImageView.
-
Lastly, we have number 4.
-
I pasted the code here in Android Studio
and surprisingly there's no errors.
-
The code is trying to find
a view with the id title, and
-
that should return a view, and that gets
stored in this variable called TextView.
-
TextView has a data type of View, so
-
that matches with the right-hand side,
so this whole line of code is correct.
-
On the next line we're taking
this object, which is a View And
-
we're calling a setVisibility
method on it.
-
The method takes in one input
parameter which is View.GONE.
-
I can check the documentation
to verify why that is.
-
In the documentation for
the view class, I can search for
-
the method setVisibility.
-
I can see that setVisibility is
a method within the view class.
-
It takes this input, an integer that
represents the visibility state.
-
The possible visibility values
are VISIBLE, INVISIBLE, and GONE.
-
Since setVisibility is a method
within the view class,
-
this is a valid call here.
-
There's no need to cast
this into a TextView
-
because the method that we're calling
is a method of the view class.
-
Now if we were trying to
use a TextView method,
-
then we would need to
cast it to a TextView.
-
So for this question,
you should put no error, because
-
setVisibility is a View method, and
it's okay to leave it as a View object.
-
These were hard questions.
-
It's okay if you didn't
get them correct.
-
In the next test, you'll have
a chance to practice this more.