Creating Tiles for Wear OS

Jun 8 2021 · Kotlin 1.5, Android 8, Android Studio 4.2

Part 1: Creating Tiles for Wear OS

04. Add Images to the Tile

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 03. Create a Basic Tile Next episode: 05. Add Modifiers to Tweak the Elements

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Intro $[==]

There are two parts to adding an image: declare the image resource inside onResourcesRequest and associating it with an ID. Fetch the image inside onTileRequest using the defined ID. Let’s see how you can start adding images to the Tile.

Demo $[==]

To view the images you will use in the Tile, open the res/drawable directory. Here you can see three vector images named: 1. ic_drink.xml 2. ic_minus_one.xml 3. ic_plus_one.xml

private val ID_IMAGE_DRINK = "image_drink"
private val ID_IMAGE_PLUS_ONE = "image_plus_one"
private val ID_IMAGE_MINUS_ONE = "image_minus_one"
.addIdToImageMapping(
  ID_IMAGE_DRINK,
  ResourceBuilders.ImageResource.builder()
      .setAndroidResourceByResId(
          ResourceBuilders.AndroidImageResourceByResId.builder()
              .setResourceId(R.drawable.ic_drink)
      )
)
.addIdToImageMapping(
  ID_IMAGE_MINUS_ONE,
  ResourceBuilders.ImageResource.builder()
      .setAndroidResourceByResId(
          ResourceBuilders.AndroidImageResourceByResId.builder()
              .setResourceId(R.drawable.ic_minus_one)
      )
)
.addIdToImageMapping(
  ID_IMAGE_PLUS_ONE,
  ResourceBuilders.ImageResource.builder()
      .setAndroidResourceByResId(
          ResourceBuilders.AndroidImageResourceByResId.builder()
              .setResourceId(R.drawable.ic_plus_one)
      )
)
private fun glassIcon() =
  LayoutElementBuilders.Image.builder()
      .setWidth(dp(40f))
      .setHeight(dp(40f))
      .setResourceId(ID_IMAGE_DRINK)
      .build()
LayoutElementBuilders.Column.builder()
  .addContent(
      LayoutElementBuilders.Row.builder()
          .addContent(currentGlassText(glassCount.toString(), deviceParameters))
  )
.addContent(glassIcon())
LayoutElementBuilders.Row.builder()
  .addContent(currentGlassText(glassCount.toString(), deviceParameters))
  .addContent(glassIcon())
)
private fun imageButton(id: String) =
  LayoutElementBuilders.Image.builder()
    .setWidth(DimensionBuilders.dp(32f))
    .setHeight(DimensionBuilders.dp(32f))
    .setResourceId(id)
    .build()
.addContent(
  LayoutElementBuilders.Row.builder()
)
.addContent(imageButton(ID_IMAGE_MINUS_ONE))
.addContent(imageButton(ID_IMAGE_PLUS_ONE))