YouTube

Got a YouTube account?

New: enable viewer-created translations and captions on your YouTube channel!

English subtitles

← Formatting Strings - Developing Android Apps

Get Embed Code
10 Languages

Showing Revision 16 created 05/24/2016 by Udacity Robot.

  1. Currently this is our forecast list.
  2. It looks okay, but it would be nice
    if we could show the degree symbol
  3. for these temperature values.
  4. Using this notation is best practice.
  5. It will help the translator
    know how to reorder the text
  6. and the parameters
    so that it fits the local language.
  7. Now let's look at how
    we can use this method
  8. to format temperatures within our app.
  9. Within the string.xml file, we declared
    the format temperature string resource.
  10. We use XLIFF tags to denote
    that this is where
  11. the decimal temperature value will go.
  12. This is followed by the unit code
    character for the degrees symbol.
  13. In the utility class, in the format
    temperature method,
  14. we can use this string resource.
  15. Remember that in this method,
    we take in a temperature
  16. and a user's preference
    for metric or imperial.
  17. Then we return the
    converted temperature.
  18. We're going to modify this method
    so that it also returns
  19. the formatted string with a degrees
    symbol after the value.
  20. In order to do this, we need
    to pass in a context,
  21. From the context we can get access
    to the string resource ID
  22. that we declared earlier.
  23. Then we passed in any additional
    parameters for that string template.
  24. In this case we only have one parameter
    so we pass in the temperature value.
  25. Then this fully formatted string
    gets returned to the caller.
  26. This method is used in the forecast list,
    as well in the detail page.
  27. So we also have to update
    the code there
  28. so that it would compile
    with this new parameter.
  29. And this is what the app looks like
    after the change.
  30. Go ahead and make the change in your code
    to add a string resource
  31. for displaying temperature in degrees.
  32. Check this box when you're done.
  33. And remember, use this notation
  34. going forward, for when
    you need to format strings.