Creating Tiles for Wear OS

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

Part 1: Creating Tiles for Wear OS

06. Handle Click Events

Episode complete

About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous 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.

Notes: 06. Handle Click Events

Try adding two buttons that increment and decrement the glass count by 0.5 respectively. These buttons can be smaller in size and located below the existing buttons. You can improve the UI by making the “-1” button visible only if the glass count is greater than zero.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

On Wear OS, a click listener to also a modifier. To add click listeners on the buttons, first open the WaterService and add the following modifier in the imageButton method:

.setClickable(
    ModifiersBuilders.Clickable.builder()
        .setId(id)
        .setOnClick(ActionBuilders.LoadAction.builder())
)
.setModifiers(
    ModifiersBuilders.Modifiers.builder()
        .setBackground(
            ModifiersBuilders.Background.builder()
            .setColor(
                ColorBuilders.argb(ContextCompat.getColor(this, R.color.colorPrimary))
            )
            .setCorner(ModifiersBuilders.Corner.builder().setRadius(BUTTON_RADIUS).build())
            .build()
        )
        .setPadding(
            ModifiersBuilders.Padding.builder()
                .setAll(dp(4f))
                .build()
        )
        .setClickable(
            ModifiersBuilders.Clickable.builder()
                .setId(id)
                .setOnClick(ActionBuilders.LoadAction.builder())
        )
)
when(requestParams.state.lastClickableId) {
    ID_IMAGE_PLUS_ONE -> waterRepository.incrementFullGlass()
    ID_IMAGE_MINUS_ONE -> waterRepository.decrementFullGlass()
}