Element size in variables

The size_provider extension writes the width and height of the element to variables after its size is calculated.

Warning

The preferred way to get the element size is the layout_provider property. It accepts the same parameters and doesn't require connecting an extension. On Android and iOS, the size_provider extension handler is marked as deprecated.

Example
{
  "type": "container",
  "items": [
    {
      "type": "text",
      "text": "Hello!",
      "width": {
        "type": "wrap_content"
      },
      "extensions": [
        {
          "id": "size_provider",
          "params": {
            "width_variable_name": "title_width"
          }
        }
      ]
    },
    {
      "type": "separator",
      "width": {
        "type": "fixed",
        "value": "@{title_width}"
      }
    }
  ]
}

In this example, the separator width is equal to the text width. The title_width integer variable must be declared in the card.

Parameters

Parameters

Description

id

Extension ID.

width_variable_name

Name of the variable that the element width in dp is written to. Optional parameter.

height_variable_name

Name of the variable that the element height in dp is written to. Optional parameter.

Specify at least one of the parameters. The variables must be declared with the integer type.

Connection

build.gradle

dependencies {
    implementation "com.yandex.div:div-size-provider:${versions.divkit}"
}

In the code:

val divConfiguration = DivConfiguration.Builder(imageLoader)
    .extension(DivSizeProviderExtensionHandler())
    .build()

The SizeProviderExtensionHandler handler is available only when the module is imported with the @_spi(Legacy) attribute:

@_spi(Legacy) import DivKitExtensions

DivKitComponents(
  ...
  extensionHandlers: [
    SizeProviderExtensionHandler(variablesStorage: variablesStorage)
  ]
)
import { SizeProvider } from '@divkitframework/divkit/client';

const map = new Map();

map.set('size_provider', SizeProvider);

render({
    id: 'test',
    target: element,
    json: {},
    extensions: map
});

Learn more

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

DivKit Repository