# Step 2: Add the UI pieces

URL: https://www.digia.tech/docs/developer/moengage/add-the-ui-pieces

MoEngage Change your setup in the sidebar
Add DigiaHost, DigiaAnchor and DigiaSlot to your app so MoEngage-triggered Digia nudges, guides and inline content have a place to appear.

Campaigns need a place in your app to appear. There are three pieces. Add the ones for the campaign types your team plans to use:

| Piece | What it is | Needed for | `DigiaHost` | One layer above your whole app where popups appear. Android View and iOS UIKit apps add it with one call instead. | Nudges, surveys, floating videos, and guides | `DigiaAnchor` | A label you put on a button or other element, so a guide can point at it. | Guides (tooltips and spotlights) | `DigiaSlot` | An empty space in your layout that inline content fills. | Inline banners, carousels and stories 

### DigiaHost

**Add exactly one host, at your app's root.** It's required: without it, campaigns are accepted but never appear, and are dropped as `host_not_mounted` . With two, popups can appear twice.

- React Native - Android - iOS (Swift) - Flutter

```tsx
import { View } from 'react-native';
import { DigiaHost } from '@digia-engage/core';

export default function RootLayout() {
  return (
    <View style={{ flex: 1 }}>
      <AppNavigator />
      {/* Keep it last, so it sits above your app. */}
      <DigiaHost />
    </View>
  );
}
```

In React Native, popups (nudges, surveys, floating videos) are drawn by a native host that `Digia.initialize` adds for you. `<DigiaHost />` draws guides. Add it once, as the **last** child of your root view; placed earlier, your own screens can cover guide tooltips.

- Views / XML / Fragments - Jetpack Compose
Call `Digia.attach(application)` once, in your `Application` class. It adds the host to every Activity for you, so you don't change any layout:
```kotlin
import android.app.Application
import com.digia.engage.Digia

class MyApplication : Application() {
    override fun onCreate() {
        super.onCreate()
        Digia.attach(this)
        // Digia.initialize(...) and your messaging platform setup, before or after attach.
    }
}
```

If your app has no `Application` class yet, create one and register it in `AndroidManifest.xml` :
```xml
<application
    android:name=".MyApplication"
    ... >
```

`attach` needs the Android SDK 3.5.0 or later. It skips any Activity that isn't an AndroidX `ComponentActivity` (for example `AppCompatActivity` or `FragmentActivity` ) and logs a warning; extend one of those for screens that should show campaigns. Don't call `Digia.attach` in a React Native app. The Digia React Native module adds the host itself.

Wrap your content in `DigiaHost` :
```kotlin
import com.digia.engage.DigiaHost

setContent {
    DigiaHost {
        AppNavHost()
    }
}
```

An app with both Compose and View screens can also call `Digia.attach` (see the Views tab). Your `DigiaHost` then shows only your content, so popups never appear twice.

- UIKit - SwiftUI
Call `Digia.attach(to:)` once, after you set the window's root view controller:
```swift
import DigiaEngage

// In SceneDelegate.scene(_:willConnectTo:options:), or wherever you create the window
window.rootViewController = HomeViewController()
window.makeKeyAndVisible()
Digia.attach(to: window)
```

If you replace `window.rootViewController` later, Digia's layer moves to the new root by itself; a campaign on screen at that moment is ended. **Call `Digia.attach(to:)` once per window.** Call it again only when you create a new `UIWindow` , not when you change the root view controller. The root must be a plain `UIViewController` . Is your root a navigation, tab bar, split or page controller? `attach` draws nothing on a `UINavigationController` , `UITabBarController` , `UISplitViewController` or `UIPageViewController` root, and logs a warning. Wrap that controller in a plain `UIViewController` and make the wrapper the root:
```swift
final class RootViewController: UIViewController {
    private let content: UIViewController

    init(content: UIViewController) {
        self.content = content
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }

    override func viewDidLoad() {
        super.viewDidLoad()
        addChild(content)
        content.view.frame = view.bounds
        content.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        view.addSubview(content.view)
        content.didMove(toParent: self)
    }

    // Keep your screens in charge of the status bar and home indicator.
    override var childForStatusBarStyle: UIViewController? { content }
    override var childForStatusBarHidden: UIViewController? { content }
    override var childForHomeIndicatorAutoHidden: UIViewController? { content }
}

let tabBarController = UITabBarController()
// ...set up your tabs
window.rootViewController = RootViewController(content: tabBarController)
window.makeKeyAndVisible()
Digia.attach(to: window)
```

Code that reads `window.rootViewController` now gets the wrapper, so reach your own controller through it.

UIKit support needs the iOS SDK 4.5.0 or later.

Wrap your root view in `DigiaHost` :
```swift
WindowGroup {
    DigiaHost {
        RootView()
    }
}
```

```dart
MaterialApp(
  // Also reports the current screen for you: see the next step.
  navigatorObservers: [DigiaNavigatorObserver()],
  builder: (context, child) => DigiaHost(child: child!),
  home: const HomeScreen(),
)
```

`DigiaHost` needs your app's navigator. `DigiaNavigatorObserver` gives it one; without it, popups are dropped as `host_not_mounted` .

### DigiaAnchor

A guide is a tooltip or spotlight that points at something in your app, such as "Tap here to check out". `DigiaAnchor` tells Digia **which element** that is. You wrap the element and give it a name, the `anchorKey` . The element looks and works exactly as before.

- React Native - Android - iOS (Swift) - Flutter

```tsx
import { DigiaAnchorView } from '@digia-engage/core';

<DigiaAnchorView anchorKey="cart_checkout_button">
  <CheckoutButton />
</DigiaAnchorView>
```

- Views / XML / Fragments - Jetpack Compose
Wrap the view in `com.digia.engage.DigiaAnchorView` and set `app:anchorKey` :
```xml
<com.digia.engage.DigiaAnchorView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:anchorKey="cart_checkout_button">

    <Button
        android:id="@+id/checkoutButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Checkout" />

</com.digia.engage.DigiaAnchorView>
```

Add `xmlns:app="http://schemas.android.com/apk/res-auto"` to the layout's root element if it isn't there yet.

```kotlin
import com.digia.engage.DigiaAnchor

DigiaAnchor(anchorKey = "cart_checkout_button") {
    CheckoutButton()
}
```

- UIKit - SwiftUI
Add your view as the first subview of a `DigiaAnchorView` and set its `anchorKey` , in code or Interface Builder:
```swift
let anchor = DigiaAnchorView()
anchor.anchorKey = "cart_checkout_button"
anchor.addSubview(checkoutButton)
```

```swift
DigiaAnchor(anchorKey: "cart_checkout_button") {
    CheckoutButton()
}
```

```dart
DigiaAnchor(
  anchorKey: 'cart_checkout_button',
  child: CheckoutButton(),
)
```

**Before you add anchors:** guides only work well on elements that are always on screen, such as tab bar items or a screen's main button. On lists, content that loads later, or elements only some users see, a guide can skip steps or not show. Read [Guide Anchors and Inline Slots](/docs/developer/reference/anchors-and-slots/#1-guide-anchors) before you choose where to put them.

A spotlight can also work without an anchor, on elements that don't scroll. See [Spotlights without an anchor](/docs/developer/reference/anchors-and-slots/#spotlights-without-an-anchor) .

### DigiaSlot

A slot is a space in your layout where inline content can appear, such as a banner at the top of Home. You place the slot and give it a name, the `placementKey` . Your campaign team then picks that slot when they build an inline campaign.

- React Native - Android - iOS (Swift) - Flutter

```tsx
import { DigiaSlotView } from '@digia-engage/core';

<DigiaSlotView placementKey="home_top" />
```

- Views / XML / Fragments - Jetpack Compose
Add a `com.digia.engage.DigiaSlotView` and set `app:placementKey` . Give it the full width and let its height wrap the content:
```xml
<com.digia.engage.DigiaSlotView
    android:id="@+id/homeTopSlot"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:placementKey="home_top" />
```

```kotlin
import com.digia.engage.DigiaSlot

DigiaSlot(placementKey = "home_top")
```

- UIKit - SwiftUI
Use a `DigiaSlotView` and set its `placementKey` , in code or Interface Builder. Give it a position and a width; its height comes from the content:
```swift
let slot = DigiaSlotView()
slot.placementKey = "home_top"
stackView.addArrangedSubview(slot)
```

```swift
DigiaSlot("home_top")
```

```dart
const DigiaSlot('home_top')
```

**Before you add slots:** an empty slot takes no space and grows when content arrives, so whatever is below it moves down. Read [Guide Anchors and Inline Slots](/docs/developer/reference/anchors-and-slots/#2-inline-slots) before you choose where to put them.

Share your anchor keys and slot keys with your campaign team. They pick them in the dashboard.

### Details to keep in mind

- **Guide anchors:** when a Guide campaign runs, the SDK looks up the `anchorKey` to position the tooltip bubble or spotlight cutout relative to that element. Give each element a unique `anchorKey` . The key must match exactly what is configured for that step in the Digia dashboard Guide campaign.
- **Anchor not on screen:** if the anchor is not mounted when a Guide campaign fires, the SDK logs an `anchor_not_on_screen` health event and skips that step. Ensure the anchor is on screen before navigating to the Guide step.
- **Inline slots:** each slot's `placementKey` must match the slot key configured on the inline campaign in the Digia dashboard. If no campaign is active for a slot, it collapses to zero height.
- **React Native slot height:**`DigiaSlotView` auto-sizes to match native content height. Pass `style={{ height: N }}` to pin a fixed height.
**Android View/XML layouts:**`DigiaSlotView` wraps the Compose `DigiaSlot` API and reads `app:placementKey` from XML. `DigiaSlotView` needs a host in the same Activity: `Digia.attach(application)` or a `DigiaHost` (see above). Or set the placement programmatically after inflation:

```kotlin
import com.digia.engage.DigiaSlotView

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_home)

    findViewById<DigiaSlotView>(R.id.homeHeroBanner).placementKey = "home_hero_banner"
    findViewById<DigiaSlotView>(R.id.productOffers).placementKey = "product_offers"
}
```
