# SharedPreferencesView

# 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)

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/datastore/migrations/SharedPreferencesView "View this page in Java")

<br />

```
class SharedPreferencesView
```

<br />

*** ** * ** ***

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

## Summary

|                                                                                                                      ### Public functions                                                                                                                      |
|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `operator `[Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)                                                                                                                                                                 | [contains](https://developer.android.com/reference/kotlin/androidx/datastore/migrations/SharedPreferencesView#contains(kotlin.String))`(key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`)` Checks whether the preferences contains a preference.                                                                                                                                                                                                                         |
| [Map](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-map/index.html)`<`[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`, `[Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)`?>` | [getAll](https://developer.android.com/reference/kotlin/androidx/datastore/migrations/SharedPreferencesView#getAll())`()` Retrieve all values from the preferences that are in the specified keySet.                                                                                                                                                                                                                                                                                                             |
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)                                                                                                                                                                            | [getBoolean](https://developer.android.com/reference/kotlin/androidx/datastore/migrations/SharedPreferencesView#getBoolean(kotlin.String,kotlin.Boolean))`(key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`, defValue: `[Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)`)` Retrieves a boolean value from the preferences.                                                                                                           |
| [Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html)                                                                                                                                                                                | [getFloat](https://developer.android.com/reference/kotlin/androidx/datastore/migrations/SharedPreferencesView#getFloat(kotlin.String,kotlin.Float))`(key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`, defValue: `[Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html)`)` Retrieves a float value from the preferences.                                                                                                                       |
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)                                                                                                                                                                                    | [getInt](https://developer.android.com/reference/kotlin/androidx/datastore/migrations/SharedPreferencesView#getInt(kotlin.String,kotlin.Int))`(key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`, defValue: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Retrieves a int value from the preferences.                                                                                                                                   |
| [Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)                                                                                                                                                                                  | [getLong](https://developer.android.com/reference/kotlin/androidx/datastore/migrations/SharedPreferencesView#getLong(kotlin.String,kotlin.Long))`(key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`, defValue: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`)` Retrieves a long value from the preferences.                                                                                                                             |
| [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`?`                                                                                                                                                                           | [getString](https://developer.android.com/reference/kotlin/androidx/datastore/migrations/SharedPreferencesView#getString(kotlin.String,kotlin.String))`(key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`, defValue: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`?)` Retrieves a string value from the preferences.                                                                                                                |
| [Set](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-set/index.html)`<`[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`>?`                                                                                | [getStringSet](https://developer.android.com/reference/kotlin/androidx/datastore/migrations/SharedPreferencesView#getStringSet(kotlin.String,kotlin.collections.Set))`(key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`, defValues: `[Set](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-set/index.html)`<`[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`>?)` Retrieves a string set value from the preferences. |

## Public functions

### contains

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

```
operator fun contains(key: String): Boolean
```

Checks whether the preferences contains a preference.  

|                                        Parameters                                        |
|------------------------------------------------------------------------------------------|-------------------------------------|
| `key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html) | the name of the preference to check |

|                                                             Throws                                                             |
|--------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------|
| [kotlin.IllegalArgumentException](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)  

```
fun getAll(): Map<String, Any?>
```

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)  

```
fun getBoolean(key: String, defValue: Boolean): Boolean
```

Retrieves a boolean value from the preferences.  

|                                           Parameters                                            |
|-------------------------------------------------------------------------------------------------|---------------------------------------------------|
| `key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)        | the name of the preference to retrieve            |
| `defValue: `[Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | value to return if this preference does not exist |

|                                                             Throws                                                             |
|--------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------|
| [kotlin.IllegalArgumentException](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)  

```
fun getFloat(key: String, defValue: Float): Float
```

Retrieves a float value from the preferences.  

|                                         Parameters                                          |
|---------------------------------------------------------------------------------------------|---------------------------------------------------|
| `key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)    | the name of the preference to retrieve            |
| `defValue: `[Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html) | value to return if this preference does not exist |

|                                                             Throws                                                             |
|--------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------|
| [kotlin.IllegalArgumentException](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)  

```
fun getInt(key: String, defValue: Int): Int
```

Retrieves a int value from the preferences.  

|                                        Parameters                                        |
|------------------------------------------------------------------------------------------|---------------------------------------------------|
| `key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html) | the name of the preference to retrieve            |
| `defValue: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)  | value to return if this preference does not exist |

|                                                             Throws                                                             |
|--------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------|
| [kotlin.IllegalArgumentException](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)  

```
fun getLong(key: String, defValue: Long): Long
```

Retrieves a long value from the preferences.  

|                                        Parameters                                         |
|-------------------------------------------------------------------------------------------|---------------------------------------------------|
| `key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)  | the name of the preference to retrieve            |
| `defValue: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) | value to return if this preference does not exist |

|                                                             Throws                                                             |
|--------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------|
| [kotlin.IllegalArgumentException](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)  

```
fun getString(key: String, defValue: String? = null): String?
```

Retrieves a string value from the preferences.  

|                                               Parameters                                                |
|---------------------------------------------------------------------------------------------------------|---------------------------------------------------|
| `key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)                | the name of the preference to retrieve            |
| `defValue: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`? = null` | value to return if this preference does not exist |

|                                                             Throws                                                             |
|--------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------|
| [kotlin.IllegalArgumentException](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)  

```
fun getStringSet(key: String, defValues: Set<String>? = null): Set<String>?
```

Retrieves a string set value from the preferences.  

|                                                                                             Parameters                                                                                              |
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------|
| `key: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)                                                                                                            | the name of the preference to retrieve            |
| `defValues: `[Set](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-set/index.html)`<`[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`>? = null` | value to return if this preference does not exist |

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