Return to Video

Resource IDs

  • 0:01 - 0:03
    Now that you know what
    Android resources are,
  • 0:03 - 0:05
    and that you can put them
    in different folders.
  • 0:05 - 0:09
    Let's talk about how you can actually
    access the resources that you saved in
  • 0:09 - 0:11
    those folders.
  • 0:11 - 0:15
    When your app is compiled,
    there's a tool in Android called AAPT.
  • 0:15 - 0:18
    And that generates the R class.
  • 0:18 - 0:22
    This is simply an R.Java file, and
    I'll show you an example next.
  • 0:22 - 0:27
    It contains the resource IDs for all
    the resources in your S directory, so
  • 0:27 - 0:31
    for all the resources that you've added,
    there's an ID to refer to it.
  • 0:31 - 0:35
    The ID is in a format that is
    based on the resource type, so for
  • 0:35 - 0:41
    all Java resources which are graphics it
    would follow the format of R.drawable.
  • 0:41 - 0:44
    For all strings that are resources,
    it would follow the formula
  • 0:44 - 0:48
    R.string.resource name which
    is hello in this case.
  • 0:49 - 0:52
    Note there are two ways that
    you can access a resource.
  • 0:52 - 0:59
    In Java code it follows the format of
    R.resource type.resource name In XML
  • 0:59 - 1:04
    though, it follows the at notation, so
    it would be at and then the resource
  • 1:04 - 1:08
    type would be string slash, and
    then the resource name, which is hello.
  • 1:09 - 1:13
    Here are more examples of different
    resource types and how to access them.
  • 1:14 - 1:16
    These are just a couple of
    the common resource types,
  • 1:16 - 1:20
    but there are many others.
  • 1:20 - 1:22
    You always start with R, a dot,
  • 1:22 - 1:25
    and then resource type, a dot,
    and then the resource name.
  • 1:25 - 1:27
    And the name is whatever you assign it.
  • 1:27 - 1:32
    And then in XML you have @ and
    then the resource type, slash,
  • 1:32 - 1:33
    and then the name of the resource.
  • 1:34 - 1:37
    Let's take a moment to find
    all the resource IDs in
  • 1:37 - 1:39
    our main activity Java file.
  • 1:39 - 1:41
    I'm going to open up the find
    command in Android Studio.
  • 1:41 - 1:46
    I'm going to use Cmd+F, or
    you can use Ctrl+F if you're on Windows.
  • 1:47 - 1:48
    It pops up a search field here.
  • 1:48 - 1:53
    I'm going to search for
    capital R dot, and
  • 1:53 - 1:58
    then click on match case, so
    that it only grabs the capital R cases.
  • 1:58 - 2:04
    The first thing it finds is this
    r.layout.activity_main reference.
  • 2:04 - 2:06
    This is interesting because we're
    referring to the activity main
  • 2:06 - 2:08
    layout file.
  • 2:08 - 2:12
    This is actually how the main activity
    knows to load up this XML file.
  • 2:12 - 2:13
    Isn't that cool?
  • 2:13 - 2:15
    Let's look at the next reference.
  • 2:16 - 2:17
    Okay.
    In the displayQuantity method,
  • 2:17 - 2:22
    we have a reference to
    R.id.quantity_text_view.
  • 2:22 - 2:25
    So this is referring to a ViewId.
  • 2:25 - 2:29
    Specifically, the TextView
    that displays the quantity.
  • 2:29 - 2:32
    Hopefully some of the puzzle
    pieces are coming together.
  • 2:32 - 2:34
    By referring to this
    view in the Java code.
  • 2:34 - 2:39
    We can update it while the app is
    running to show this numberOfCoffees.
  • 2:39 - 2:41
    Let's go to the next occurance.
  • 2:41 - 2:44
    OK in the displayMessage
  • 2:44 - 2:47
    method we see a reference to
    R.id.order_summary_text_view.
  • 2:47 - 2:52
    Here it seems like we're trying to
    refer to this text view which displays
  • 2:52 - 2:54
    the order summary, so
    that we can update it with this message.
Cím:
Resource IDs
Leírás:

more » « less
Video Language:
English
Team:
Udacity
Projekt:
UD837 - Android for Beginners
Duration:
02:56
Udacity Robot edited Angol subtitles for 12-21 Resource IDs
Udacity Robot edited Angol subtitles for 12-21 Resource IDs
Cogi-Admin edited Angol subtitles for 12-21 Resource IDs

English subtitles

Felülvizsgálatok Compare revisions