English subtitles

← Find View by Id - Solution

Get Embed Code
13 Languages

Showing Revision 3 created 05/25/2016 by Udacity Robot.

  1. Let's look at number 1.
  2. Before I reveal the answer,
    I'm going to copy this code and
  3. paste it into Android Studio.
  4. I want to show you what kind
    of error would come out.
  5. Now when you were solving this quiz,
    you didn't need to use Android Studio,
  6. you could have solved it by just
    looking at the code here and
  7. then writing in the answer.
  8. So, whether or not you use
    Android Studio, it's fine with me.
  9. To make the code work,
    I created a new project and
  10. then I modified the activity
    main layout file so that it
  11. would have a bunch of views with these
    ID's that we're going to be referencing.
  12. I'm not even sure what this
    layout would look like, but
  13. I just wanted to put the views in here
    with the corresponding view ID names.
  14. That way, my code will work here.
  15. So for number one, I pasted the code
    here, and I see that there's an error.
  16. Android Studios says it cannot
    resolve the method setText.
  17. If you remember from the last video,
  18. I mentioned that setText
    is a TextView method.
  19. That means you can only call
    this method on TextView objects.
  20. In this case,
    we're calling it on nameTextView.
  21. A nameTextView is declared
    to be a view object,
  22. we need to change this
    to be a TextView object.
  23. Then we get an error saying
    there is incompatible types.
  24. A textview, an object of type
    TextView is required, but
  25. instead it found an object of type view.
  26. That's because find view
    by ID returns a view, and
  27. this left-hand side requires a TextView.
  28. That means we have to add this to cast
  29. the view returned by
    findViewById into a TextView.
  30. Then we can store this whole thing,
  31. which is the nameTextView,
    into the left-hand side variable.
  32. Then the error goes away, and we can
    set text, "Laura" on this TextView.
  33. The error is on line 1, so
    I wrote the correct code right here.
  34. The general explanation is that we need
    to cast this view into a TextView and
  35. then store that inside
    a nameTextView variable.
  36. That way,
    we can call nameTextView.setText,
  37. which is a TextView method,
    on the next line of code here.
  38. Let's move on to number two.
  39. I'm going to copy this code and
    put it into Android Studio.
  40. Here's the code I pasted in.
  41. The first line of code seems to
    be correct, there's no errors.
  42. But the second line has
    a red underline and
  43. it says there's
    an incompatible types error.
  44. It says, a string is required but
    instead it found an int.
  45. To understand this better,
  46. we should look up the method getMaxLines
    within the TextView documentation.
  47. In the TextView documentation,
    I'm going to search for
  48. the getMaxLines method.
  49. And here it is.
  50. There's no input parameters, and
  51. the return value of this
    method is an integer.
  52. So that's why Android studio was
    complaining, because this method,
  53. if it's returning an integer, but
  54. we were trying to store it
    into a String variable.
  55. As a result, I need to change this data
    type of the variable to be an int.
  56. Then the error goes away.
  57. So, the return value of
    this method is an integer.
  58. So, we can store it inside
    this maxLines variable.
  59. There is a warning still remaining.
  60. Saying that the variable
    maxLines is never used.
  61. But this is just a code snippet.
  62. I assume that if this
    was in a regular app we
  63. would actually want to
    use this variable later.
  64. So, the error's on line two and
    here's the updated code.
  65. The general explanation is that
    the return value of this method should
  66. be an integer.
  67. Let's move on to number three.
  68. Here's a code for number three.
  69. I copied and pasted the code for
    number three in Android Studio.
  70. There's an error message that
    says incompatible types.
  71. An ImageView was required,
    but instead it found a view.
  72. This is similar to the error
    we just saw with the TextView.
  73. This method returns a view.
  74. And, on the left-hand side, we're
    trying to store it inside an ImageView.
  75. So we need to cast this view
    object into an ImageView.
  76. This is valid because the view with
    the ID icon actually is an ImageView,
  77. so it's okay to cast
    it into an image view.
  78. Now that the right-hand side
    evaluates to an ImageView,
  79. we can store that into the variable
    on the left-hand side.
  80. This variable has a data type ImageView
    so now everything matches up and
  81. the error is gone.
  82. On the next side,
    we can call an ImageView method,
  83. SetImageResource, onto
    this IconImageView object.
  84. There is one remaining error saying
    that it cannot resolve symbol logo.
  85. That's because I just didn't add
    an image in our app with the name logo.
  86. If I added that, then the error would
    go away, so our code here is correct.
  87. Here's the correct code where I
    cast this view into an ImageView.
  88. Lastly, we have number 4.
  89. I pasted the code here in Android Studio
    and surprisingly there's no errors.
  90. The code is trying to find
    a view with the id title, and
  91. that should return a view, and that gets
    stored in this variable called TextView.
  92. TextView has a data type of View, so
  93. that matches with the right-hand side,
    so this whole line of code is correct.
  94. On the next line we're taking
    this object, which is a View And
  95. we're calling a setVisibility
    method on it.
  96. The method takes in one input
    parameter which is View.GONE.
  97. I can check the documentation
    to verify why that is.
  98. In the documentation for
    the view class, I can search for
  99. the method setVisibility.
  100. I can see that setVisibility is
    a method within the view class.
  101. It takes this input, an integer that
    represents the visibility state.
  102. The possible visibility values
  103. Since setVisibility is a method
    within the view class,
  104. this is a valid call here.
  105. There's no need to cast
    this into a TextView
  106. because the method that we're calling
    is a method of the view class.
  107. Now if we were trying to
    use a TextView method,
  108. then we would need to
    cast it to a TextView.
  109. So for this question,
    you should put no error, because
  110. setVisibility is a View method, and
    it's okay to leave it as a View object.
  111. These were hard questions.
  112. It's okay if you didn't
    get them correct.
  113. In the next test, you'll have
    a chance to practice this more.