# SharedPreferencesView

Artifact: [androidx.datastore:datastore](https://developer.android.com/jetpack/androidx/releases/datastore) [View Source](https://cs.android.com/search?q=file:androidx/datastore/migrations/SharedPreferencesMigration.android.kt+class:androidx.datastore.migrations.SharedPreferencesView) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/datastore#1.0.0)

*** ** * ** ***


```
public final class SharedPreferencesView
```

<br />

*** ** * ** ***

Read-only wrapper around SharedPreferences. This will be passed in to your migration.

## Summary

| ### Public methods |
|---|---|
| `final boolean` | `https://developer.android.com/reference/androidx/datastore/migrations/SharedPreferencesView#contains(kotlin.String)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key)` Checks whether the preferences contains a preference. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/Map.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html, https://developer.android.com/reference/java/lang/Object.html>` | `https://developer.android.com/reference/androidx/datastore/migrations/SharedPreferencesView#getAll()()` Retrieve all values from the preferences that are in the specified keySet. |
| `final boolean` | `https://developer.android.com/reference/androidx/datastore/migrations/SharedPreferencesView#getBoolean(kotlin.String,kotlin.Boolean)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key, boolean defValue)` Retrieves a boolean value from the preferences. |
| `final float` | `https://developer.android.com/reference/androidx/datastore/migrations/SharedPreferencesView#getFloat(kotlin.String,kotlin.Float)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key, float defValue)` Retrieves a float value from the preferences. |
| `final int` | `https://developer.android.com/reference/androidx/datastore/migrations/SharedPreferencesView#getInt(kotlin.String,kotlin.Int)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key, int defValue)` Retrieves a int value from the preferences. |
| `final long` | `https://developer.android.com/reference/androidx/datastore/migrations/SharedPreferencesView#getLong(kotlin.String,kotlin.Long)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key, long defValue)` Retrieves a long value from the preferences. |
| `final https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/datastore/migrations/SharedPreferencesView#getString(kotlin.String,kotlin.String)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key, https://developer.android.com/reference/java/lang/String.html defValue)` Retrieves a string value from the preferences. |
| `final https://developer.android.com/reference/java/util/Set.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html>` | `https://developer.android.com/reference/androidx/datastore/migrations/SharedPreferencesView#getStringSet(kotlin.String,kotlin.collections.Set)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key, https://developer.android.com/reference/java/util/Set.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html> defValues)` Retrieves a string set value from the preferences. |

## Public methods

### contains

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

```
public final boolean contains(@NonNull String key)
```

Checks whether the preferences contains a preference.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key` | the name of the preference to check |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` | if `key` wasn't specified as part of this migration |

### getAll

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

```
public final @NonNull Map<@NonNull String, Object> getAll()
```

Retrieve all values from the preferences that are in the specified keySet.

### getBoolean

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

```
public final boolean getBoolean(@NonNull String key, boolean defValue)
```

Retrieves a boolean value from the preferences.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key` | the name of the preference to retrieve |
| `boolean defValue` | value to return if this preference does not exist |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` | if `key` wasn't specified as part of this migration |

### getFloat

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

```
public final float getFloat(@NonNull String key, float defValue)
```

Retrieves a float value from the preferences.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key` | the name of the preference to retrieve |
| `float defValue` | value to return if this preference does not exist |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` | if `key` wasn't specified as part of this migration |

### getInt

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

```
public final int getInt(@NonNull String key, int defValue)
```

Retrieves a int value from the preferences.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key` | the name of the preference to retrieve |
| `int defValue` | value to return if this preference does not exist |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` | if `key` wasn't specified as part of this migration |

### getLong

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

```
public final long getLong(@NonNull String key, long defValue)
```

Retrieves a long value from the preferences.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key` | the name of the preference to retrieve |
| `long defValue` | value to return if this preference does not exist |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` | if `key` wasn't specified as part of this migration |

### getString

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

```
public final String getString(@NonNull String key, String defValue)
```

Retrieves a string value from the preferences.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key` | the name of the preference to retrieve |
| `https://developer.android.com/reference/java/lang/String.html defValue` | value to return if this preference does not exist |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` | if `key` wasn't specified as part of this migration |

### getStringSet

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

```
public final Set<@NonNull String> getStringSet(@NonNull String key, Set<@NonNull String> defValues)
```

Retrieves a string set value from the preferences.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key` | the name of the preference to retrieve |
| `https://developer.android.com/reference/java/util/Set.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html> defValues` | value to return if this preference does not exist |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` | if `key` wasn't specified as part of this migration |