Android

To test your code and see a preview of the card on an Android device, use the DivKit Playground app. You can add the card code in JSON format to the app or open a link to it:

  1. Install the DivKit Playground app.

  2. Select demo.

  3. Add a card using one of the following methods:

    • Insert the code in JSON format or a link to the card prototype in the input field.
    • Open the link from the DivKit Web Playground editor.
    • Select the file in JSON format on the device.

    Tip

    To create a card code, you can use the DivKit Web Playground sample library.

  4. To view the result, click show the result.

Data state management

DivKit for Android provides an API for managing stored data. Use DivKit.reset() to remove stored variables and DivData from storage.

Resetting data

Obtain the shared DivKit instance with DivKit.getInstance(context). To reset data for specific cards, pass their DivDataTag values in tags. Omitting tags or passing an empty list resets stored data for all cards.

View reset examples
import com.yandex.div.DivDataTag
import com.yandex.div.core.DivKit

val divKit = DivKit.getInstance(context)

// Reset all stored data
divKit.reset()

// Reset only stored variables
divKit.reset(DivKit.RESET_STORED_VARIABLES)

// Reset only stored DivData
divKit.reset(DivKit.RESET_STORED_DIV_DATA)

// Reset data with tag filtering
divKit.reset(
    flags = DivKit.RESET_STORED_VARIABLES,
    tags = listOf(DivDataTag("tag1"), DivDataTag("tag2"))
)

// Reset DivData with tag filtering
divKit.reset(
    flags = DivKit.RESET_STORED_DIV_DATA,
    tags = listOf(DivDataTag("tag1"), DivDataTag("tag2"))
)

// Combined reset with tag filtering
divKit.reset(
    flags = DivKit.RESET_STORED_VARIABLES or DivKit.RESET_STORED_DIV_DATA,
    tags = listOf(DivDataTag("tag1"), DivDataTag("tag2"))
)

Reset flags:

  • RESET_ALL - reset both stored variables and stored DivData (used by default)
  • RESET_STORED_VARIABLES - reset only stored variables
  • RESET_STORED_DIV_DATA - reset only stored DivData

Flags can be combined using Kotlin's bitwise or, as shown above.

DivConfiguration Initialization

To work with images in DivKit Android, you need to configure DivImageLoader. The following built-in implementations are available:

  • CoilDivImageLoader — recommended for most use cases
  • GlideDivImageLoader — alternative implementation based on Glide

Both loaders can be initialized with an Android Context using their default settings.

View Coil initialization example
import com.yandex.div.coil.CoilDivImageLoader
import com.yandex.div.core.DivConfiguration

val imageLoader = CoilDivImageLoader(context)
val configuration = DivConfiguration.Builder(imageLoader).build()
View Glide initialization example
import com.yandex.div.core.DivConfiguration
import com.yandex.div.glide.GlideDivImageLoader

val imageLoader = GlideDivImageLoader(context)
val configuration = DivConfiguration.Builder(imageLoader).build()

Learn more

You can discuss topics of interest in the DivKit user community in Telegram: https://t.me/divkit_community_en.

DivKit Repository

Previous
Next