Badge integration

Badges are small elements on a product card that contain information about cashback as Plus points or Yandex Split payments.

Alert

Supported versions: Android 7.0 (API Level 24) or higher.

Step 1. Connect the library to your project

Open your project's gradle file, find the "dependencies" section, and add the com.yandex.pay:widget-badge library dependency to it. We recommend using the most recent library version.

dependencies{
    ...
    implementation "com.yandex.pay:widget-badge:2.3.10"
    ...
}

Step 2. Add a YPayBadgeView

Add the specified code to where you need in your markup.

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <!-- Your markup -->

    <com.yandex.pay.widgets.badge.api.view.YPayBadgeView
        android:id="@+id/badgeView"
        android:layout_width="wrap_content"
        android:layout_height="24dp" />

</LinearLayout>

Warning

Badges have a fixed aspect ratio set, which changes depending on the badge type. For more information, see the section below.

How badge sizes are calculated

Badge sizes are determined according to the following rules:

  • If the height is set, the width is calculated automatically based on the aspect ratio. In this case, the width set in the markup is ignored.
  • If the height is not set, but the width is specified in the markup, the height is calculated automatically based on the aspect ratio.
  • In other cases, the badge height is equal to 24 dp by default. If the height or width limit doesn't allow taking up the required size, the badge is automatically reduced to the maximum allowed size.

Step 3. Initialize the inventory

Set the basic parameters required to run the inventory and badges, such as:

// general inventory parameters:
YPayInventory.init {
    // your app's context:
    appContext = yourApplicationContext
    // your Merchant ID :
    merchantId = YPayMerchantId("")
    // PROD / SANDBOX network environment:
    environment = YPayNetworkEnvironment.PROD
}
// initializes badge components:
.badges {
    // mode that hides badges if there is no data
    hidingPolicy = HidingPolicy.GONE
}

Warning

The appContext and merchantId parameters are required. The environment parameter that defines the network environment is optional. If not specified, the PROD environment is used by default. The hidingPolicy parameter affects the way badges are hidden if the data to display can't be uploaded. The default value is HidingPolicy.INVISIBLE.

Use the following code to shut down the badge components correctly:

YPayInventory.clear()
    // deinitializes badge components:
    .badges()

Step 4. Set the badge parameters

  1. Use the setSum() method to set the badge sum.

    badgeView.setSum(BigDecimal(10000))
    
  2. There are two ways to set the BadgeRenderData visual parameters:

    badgeView.setRenderData(
        // Badge type: With cashback or with Split
        BadgeRenderData.SplitBadgeRenderData(
            // Badge theme
            theme = BadgeTheme.LIGHT,
            // Badge color
            color = SplitBadgeColor.PRIMARY,
            // Badge alignment relative to the container
            align = BadgeAlign.LEFT,
            // Badge version, only for the "With Split" type
            variant = SplitBadgeVariant.SIMPLE
        )
    )
    
    <com.yandex.pay.widgets.badge.api.view.YPayBadgeView
    android:id="@+id/badgeView"
    android:layout_width="wrap_content"
    android:layout_height="24dp"
    app:ypay_badge_type="split"
    app:ypay_badge_align="left"
    app:ypay_badge_split_color="green"
    app:ypay_badge_split_variant="simple"
    app:ypay_badge_theme="light" />
    

You can use different types of the BadgeRenderData badge for cashback and Split:

BadgeRenderData type Description Layout
CashbackBadgeRenderData Configures the badge to show cashback Pay Badge Cashback
SplitBadgeRenderData Configures the badge to show Split Pay Badge Split

Sample badge layouts depending on how the visual parameters are set

Parameter Value Layout
Badge theme
theme BadgeTheme.LIGHT Pay Badge Light
theme BadgeTheme.DARK Pay Badge Dark
theme BadgeTheme.SYSTEM Pay Badge System
Badge color (for CashbackBadgeRenderData)
color CashbackBadgeColor.PRIMARY Pay Badge Primary
color CashbackBadgeColor.GREY Pay Badge Grey
color CashbackBadgeColor.TRANSPARENT Pay Badge Transparent
Badge color (for SplitBadgeRenderData)
color SplitBadgeColor.PRIMARY Pay Badge Primary
color SplitBadgeColor.GREEN Pay Badge Green
color SplitBadgeColor.GREY Pay Badge Grey
color SplitBadgeColor.TRANSPARENT Pay Badge Transparent
Badge alignment relative to the container
align BadgeAlign.LEFT
align BadgeAlign.RIGHT
align BadgeAlign.CENTER
Badge display variant (for CashbackBadgeRenderData)
variant CashbackBadgeVariant.SIMPLE Pay Badge Simple
variant CashbackBadgeVariant.DETAILED Pay Badge Detailed
Badge display variant (for SplitBadgeRenderData)
variant SplitBadgeVariant.SIMPLE Pay Badge Simple
variant SplitBadgeVariant.DETAILED Pay Badge Detailed

Note

To change the badge parameters, call the setSum() or setRenderData() method with new parameter values. Once updated, the requestLayout() method is called and the badge layout changes.

Step 5. Test the app

Run the app and check if the badges are displayed correctly. If the badge container is too big, reduce its size.

Previous