# NotificationCompat.MetricStyle

Artifact: [androidx.core:core](https://developer.android.com/jetpack/androidx/releases/core) [View Source](https://cs.android.com/search?q=file:androidx/core/app/NotificationCompat.java+class:androidx.core.app.NotificationCompat.MetricStyle) Added in [1.19.1](https://developer.android.com/jetpack/androidx/releases/core#1.19.1)

*** ** * ** ***


```
@RequiresApi(value = 26)
public final class NotificationCompat.MetricStyle extends NotificationCompat.Style
```

<br />

|---|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object.html) |||
| ↳ | [androidx.core.app.NotificationCompat.Style](https://developer.android.com/reference/androidx/core/app/NotificationCompat.Style) ||
|   | ↳ | [androidx.core.app.NotificationCompat.MetricStyle](https://developer.android.com/reference/androidx/core/app/NotificationCompat.MetricStyle) |

*** ** * ** ***

A notification style which shows up to 3 metrics when expanded. Metrics usually represent quantities that change over time, such as fitness information collected by a tracker, timers, weather information, etc.

To use this style with your Notification, feed it to `https://developer.android.com/reference/androidx/core/app/NotificationCompat.Builder#setStyle(androidx.core.app.NotificationCompat.Style)` like so:

```
Notification notif = new NotificationCompat.Builder(context)
  .setStyle(new MetricStyle()
      .addMetric(new Metric(new Metric.FixedInt(1979), "Steps"))
      .addMetric(new Metric(
          Metric.TimeDifference.forStopwatch(startTime, FORMAT_CHRONOMETER_AUTOMATIC),
          "Time elapsed")))
```

A MetricStyle must contain at least one `https://developer.android.com/reference/androidx/core/app/NotificationCompat.Metric` object to be valid; an invalid style will be rejected when `https://developer.android.com/reference/androidx/core/app/NotificationCompat.Builder#build()` is called.

If a notification with this style is `https://developer.android.com/reference/android/app/Notification.html#FLAG_PROMOTED_ONGOING`, then one of its metrics might be displayed in the status bar chip.

Note that this style doesn't display the large icon set via `https://developer.android.com/reference/androidx/core/app/NotificationCompat.Builder#setLargeIcon(android.graphics.drawable.Icon)`.

This style is only supported in devices with `https://developer.android.com/reference/android/os/Build.VERSION_CODES.html#CINNAMON_BUN` or greater. If used on a device with a lower `SDK_INT` then the notification will be displayed with the standard appearance (i.e. showing content text, but *not* metrics).

## Summary

| ### Constants |
|---|---|
| `static final int` | `https://developer.android.com/reference/androidx/core/app/NotificationCompat.MetricStyle#METRIC_INDEX_NONE() = -1` Special value for `https://developer.android.com/reference/androidx/core/app/NotificationCompat.MetricStyle#setCriticalMetric(int)` to indicate that none of the metrics should be considered the "most important" one. |

| ### Public constructors |
|---|
| `@https://developer.android.com/reference/androidx/annotation/RequiresApi(value = 37) https://developer.android.com/reference/androidx/core/app/NotificationCompat.MetricStyle#MetricStyle()()` Constructs a new instance of `https://developer.android.com/reference/androidx/core/app/NotificationCompat.MetricStyle`. |

| ### Public methods |
|---|---|
| `void` | `https://developer.android.com/reference/androidx/core/app/NotificationCompat.MetricStyle#addCompatExtras(android.os.Bundle)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/os/Bundle.html extras)` This is called with the extras of the framework `https://developer.android.com/reference/android/app/Notification.html` during the `https://developer.android.com/reference/androidx/core/app/NotificationCompat.Builder#build()` process, after `apply()` has been called. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/app/NotificationCompat.MetricStyle` | `https://developer.android.com/reference/androidx/core/app/NotificationCompat.MetricStyle#addMetric(androidx.core.app.NotificationCompat.Metric)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/app/NotificationCompat.Metric metric)` Adds a `https://developer.android.com/reference/androidx/core/app/NotificationCompat.Metric` to this `https://developer.android.com/reference/androidx/core/app/NotificationCompat.MetricStyle`. |
| `boolean` | `https://developer.android.com/reference/androidx/core/app/NotificationCompat.MetricStyle#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object.html obj)` |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/androidx/core/app/NotificationCompat.Metric` | `https://developer.android.com/reference/androidx/core/app/NotificationCompat.MetricStyle#getCriticalMetric()()` Returns which, if any, of the metrics is considered the "most important", or `null` if none are. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/List.html<https://developer.android.com/reference/androidx/core/app/NotificationCompat.Metric>` | `https://developer.android.com/reference/androidx/core/app/NotificationCompat.MetricStyle#getMetrics()()` Returns an immutable view of the list of `https://developer.android.com/reference/androidx/core/app/NotificationCompat.Metric` instances in this `https://developer.android.com/reference/androidx/core/app/NotificationCompat.MetricStyle`. |
| `int` | `https://developer.android.com/reference/androidx/core/app/NotificationCompat.MetricStyle#hashCode()()` |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/app/NotificationCompat.MetricStyle` | `https://developer.android.com/reference/androidx/core/app/NotificationCompat.MetricStyle#setCriticalMetric(int)(int index)` Indicates which of the metrics is considered the "most important". |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/app/NotificationCompat.MetricStyle` | `https://developer.android.com/reference/androidx/core/app/NotificationCompat.MetricStyle#setMetrics(java.util.List<androidx.core.app.NotificationCompat.Metric>)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/List.html<https://developer.android.com/reference/androidx/core/app/NotificationCompat.Metric> metrics)` Sets the list of `https://developer.android.com/reference/androidx/core/app/NotificationCompat.Metric` instances for this `https://developer.android.com/reference/androidx/core/app/NotificationCompat.MetricStyle`, replacing whatever was added before. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/core/app/NotificationCompat.MetricStyle#toString()()` |

| ### Protected methods |
|---|---|
| `void` | `https://developer.android.com/reference/androidx/core/app/NotificationCompat.MetricStyle#clearCompatExtraKeys(android.os.Bundle)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/os/Bundle.html extras)` |
| `void` | `https://developer.android.com/reference/androidx/core/app/NotificationCompat.MetricStyle#restoreFromCompatExtras(android.os.Bundle)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/os/Bundle.html extras)` |

### Inherited methods

From [androidx.core.app.NotificationCompat.Style](https://developer.android.com/reference/androidx/core/app/NotificationCompat.Style) `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/app/Notification.html` `https://developer.android.com/reference/androidx/core/app/NotificationCompat.Style#build()()`

If this Style object has been set on a notification builder, this method will build that notification and return it.
`void` `https://developer.android.com/reference/androidx/core/app/NotificationCompat.Style#setBuilder(androidx.core.app.NotificationCompat.Builder)(@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/androidx/core/app/NotificationCompat.Builder builder)`

Link this rich notification style with a notification builder.

## Constants

### METRIC_INDEX_NONE

Added in [1.19.1](https://developer.android.com/jetpack/androidx/releases/core#1.19.1)

```
public static final int METRIC_INDEX_NONE = -1
```

Special value for `https://developer.android.com/reference/androidx/core/app/NotificationCompat.MetricStyle#setCriticalMetric(int)` to indicate that none of the metrics should be considered the "most important" one.

## Public constructors

### MetricStyle

Added in [1.19.1](https://developer.android.com/jetpack/androidx/releases/core#1.19.1)

```
@RequiresApi(value = 37)
public MetricStyle()
```

Constructs a new instance of `https://developer.android.com/reference/androidx/core/app/NotificationCompat.MetricStyle`. Use `https://developer.android.com/reference/androidx/core/app/NotificationCompat.MetricStyle#addMetric(androidx.core.app.NotificationCompat.Metric)` to populate it.

This style is only supported in devices with `https://developer.android.com/reference/android/os/Build.VERSION_CODES.html#CINNAMON_BUN` or greater. If used on a device with a lower `SDK_INT` then the notification will be displayed with the standard appearance (i.e. showing content text, but *not* metrics).

## Public methods

### addCompatExtras

Added in [1.19.1](https://developer.android.com/jetpack/androidx/releases/core#1.19.1)

```
public void addCompatExtras(@NonNull Bundle extras)
```

This is called with the extras of the framework `https://developer.android.com/reference/android/app/Notification.html` during the `https://developer.android.com/reference/androidx/core/app/NotificationCompat.Builder#build()` process, after `apply()` has been called. This means that you only need to add data which won't be populated by the framework Notification which was built so far. Moreover, recovering builders and styles is only supported at API 19 and above, no implementation is required for current BigTextStyle, BigPictureStyle, or InboxStyle.

### addMetric

Added in [1.19.1](https://developer.android.com/jetpack/androidx/releases/core#1.19.1)

```
public @NonNull NotificationCompat.MetricStyle addMetric(@NonNull NotificationCompat.Metric metric)
```

Adds a `https://developer.android.com/reference/androidx/core/app/NotificationCompat.Metric` to this `https://developer.android.com/reference/androidx/core/app/NotificationCompat.MetricStyle`.

### equals

```
public boolean equals(Object obj)
```

### getCriticalMetric

Added in [1.19.1](https://developer.android.com/jetpack/androidx/releases/core#1.19.1)

```
public @Nullable NotificationCompat.Metric getCriticalMetric()
```

Returns which, if any, of the metrics is considered the "most important", or `null` if none are. This may be used when the notification is displayed in other surfaces (such as a status bar chip).

By default, unless `https://developer.android.com/reference/androidx/core/app/NotificationCompat.MetricStyle#setCriticalMetric(int)` has been set, the first metric in the list is considered the critical one.

### getMetrics

Added in [1.19.1](https://developer.android.com/jetpack/androidx/releases/core#1.19.1)

```
public @NonNull List<NotificationCompat.Metric> getMetrics()
```

Returns an immutable view of the list of `https://developer.android.com/reference/androidx/core/app/NotificationCompat.Metric` instances in this `https://developer.android.com/reference/androidx/core/app/NotificationCompat.MetricStyle`.

### hashCode

```
public int hashCode()
```

### setCriticalMetric

Added in [1.19.1](https://developer.android.com/jetpack/androidx/releases/core#1.19.1)

```
public @NonNull NotificationCompat.MetricStyle setCriticalMetric(int index)
```

Indicates which of the metrics is considered the "most important". This may be used when the notification is displayed in other surfaces (such as a status bar chip).

| Parameters |
|---|---|
| `int index` | either the index (0-based) of an item in `https://developer.android.com/reference/androidx/core/app/NotificationCompat.MetricStyle#getMetrics()`, or `https://developer.android.com/reference/androidx/core/app/NotificationCompat.MetricStyle#METRIC_INDEX_NONE()` to indicate no `https://developer.android.com/reference/androidx/core/app/NotificationCompat.Metric` should be used for this purpose |

### setMetrics

Added in [1.19.1](https://developer.android.com/jetpack/androidx/releases/core#1.19.1)

```
public @NonNull NotificationCompat.MetricStyle setMetrics(@NonNull List<NotificationCompat.Metric> metrics)
```

Sets the list of `https://developer.android.com/reference/androidx/core/app/NotificationCompat.Metric` instances for this `https://developer.android.com/reference/androidx/core/app/NotificationCompat.MetricStyle`, replacing whatever was added before.

### toString

```
public @NonNull String toString()
```

## Protected methods

### clearCompatExtraKeys

Added in [1.19.1](https://developer.android.com/jetpack/androidx/releases/core#1.19.1)

```
protected void clearCompatExtraKeys(@NonNull Bundle extras)
```

### restoreFromCompatExtras

Added in [1.19.1](https://developer.android.com/jetpack/androidx/releases/core#1.19.1)

```
protected void restoreFromCompatExtras(@NonNull Bundle extras)
```