# UserData.Builder

Added in[API level 28](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

# Builder

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/android/service/autofill/UserData.Builder "View this page in Java")  

```
class Builder
```

|---|----------------------------------------------------------------------------------------------------------------------------------------|
| [kotlin.Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html)                                                         ||
| ↳ | [android.service.autofill.UserData.Builder](https://developer.android.com/reference/kotlin/android/service/autofill/UserData.Builder#) |

A builder for[UserData](https://developer.android.com/reference/kotlin/android/service/autofill/UserData)objects.

## Summary

|                                                                                                                                                                                                                                                                                                               Public constructors                                                                                                                                                                                                                                                                                                                ||
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| [Builder](https://developer.android.com/reference/kotlin/android/service/autofill/UserData.Builder#Builder(kotlin.String,%20kotlin.String,%20kotlin.String))`(`id:` `[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)`, `value:` `[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)`, `categoryId:` `[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)`)` Creates a new builder for the user data used for[field classification](https://developer.android.com/reference/kotlin/android/service/autofill/UserData.Builder#FieldClassification). |

|                                                                                                                                                                                                                                                                                                                                                                                                                 Public methods                                                                                                                                                                                                                                                                                                                                                                                                                 ||
|---------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [UserData.Builder](https://developer.android.com/reference/kotlin/android/service/autofill/UserData.Builder#) | [add](https://developer.android.com/reference/kotlin/android/service/autofill/UserData.Builder#add(kotlin.String,%20kotlin.String))`(`value:` `[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)`, `categoryId:` `[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)`)` Adds a new value for user data.                                                                                                                                                                                                                                                                                                                                                            |
| [UserData](https://developer.android.com/reference/kotlin/android/service/autofill/UserData)                  | [build](https://developer.android.com/reference/kotlin/android/service/autofill/UserData.Builder#build())`()` Creates a new[UserData](https://developer.android.com/reference/kotlin/android/service/autofill/UserData)instance.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| [UserData.Builder](https://developer.android.com/reference/kotlin/android/service/autofill/UserData.Builder#) | [setFieldClassificationAlgorithm](https://developer.android.com/reference/kotlin/android/service/autofill/UserData.Builder#setFieldClassificationAlgorithm(kotlin.String,%20android.os.Bundle))`(`name:` `[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)?`, `args:` `[Bundle](https://developer.android.com/reference/kotlin/android/os/Bundle.html#)?`)` Sets the default algorithm used for[field classification](https://developer.android.com/reference/kotlin/android/service/autofill/UserData.Builder#FieldClassification).                                                                                                                                                            |
| [UserData.Builder](https://developer.android.com/reference/kotlin/android/service/autofill/UserData.Builder#) | [setFieldClassificationAlgorithmForCategory](https://developer.android.com/reference/kotlin/android/service/autofill/UserData.Builder#setFieldClassificationAlgorithmForCategory(kotlin.String,%20kotlin.String,%20android.os.Bundle))`(`categoryId:` `[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)`, `name:` `[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)?`, `args:` `[Bundle](https://developer.android.com/reference/kotlin/android/os/Bundle.html#)?`)` Sets the algorithm used for[field classification](https://developer.android.com/reference/kotlin/android/service/autofill/UserData.Builder#FieldClassification)for the specified category. |

## Public constructors

### Builder

Added in[API level 28](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
Builder(
    id: String, 
    value: String, 
    categoryId: String)
```

Creates a new builder for the user data used for[field classification](https://developer.android.com/reference/kotlin/android/service/autofill/UserData.Builder#FieldClassification).

The user data must contain at least one pair of`value`-\>`categoryId`, and more pairs can be added through the[add(java.lang.String,java.lang.String)](https://developer.android.com/reference/kotlin/android/service/autofill/UserData.Builder#add(kotlin.String,%20kotlin.String))method. For example:  

```kotlin
new UserData.Builder("v1", "Bart Simpson", "name")
    .add("bart.simpson@example.com", "email")
    .add("el_barto@example.com", "email")
    .build();
  
```

|                                                                                                                                                                                                                                                                                                                                                                              Parameters                                                                                                                                                                                                                                                                                                                                                                              ||
|--------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `id`         | [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html):id used to identify the whole[UserData](https://developer.android.com/reference/kotlin/android/service/autofill/UserData)object. This id is also returned by[AutofillManager.getUserDataId()](https://developer.android.com/reference/kotlin/android/view/autofill/AutofillManager.html#getUserDataId()), which can be used to check if the[UserData](https://developer.android.com/reference/kotlin/android/service/autofill/UserData)is up-to-date without fetching the whole object (through[AutofillManager.getUserData()](https://developer.android.com/reference/kotlin/android/view/autofill/AutofillManager.html#getUserData())). This value cannot be`null`. |
| `value`      | [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html):value of the user data. This value cannot be`null`.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| `categoryId` | [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html):autofill field category. This value cannot be`null`.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |

|                                                                                                                                                                                                                                   Exceptions                                                                                                                                                                                                                                   ||
|--------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `java.lang.IllegalArgumentException` | if any of the following occurs: - `id`is empty - `categoryId`is empty - `value`is empty - the length of`value`is lower than[UserData.getMinValueLength()](https://developer.android.com/reference/kotlin/android/service/autofill/UserData#getMinValueLength()) - the length of`value`is higher than[UserData.getMaxValueLength()](https://developer.android.com/reference/kotlin/android/service/autofill/UserData#getMaxValueLength()) |

## Public methods

### add

Added in[API level 28](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
fun add(
    value: String, 
    categoryId: String
): UserData.Builder
```

Adds a new value for user data.

|                                                                                          Parameters                                                                                           ||
|--------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `value`      | [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html):value of the user data. This value cannot be`null`.                                            |
| `categoryId` | [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html):string used to identify the category the value is associated with. This value cannot be`null`. |

|                                                                   Return                                                                   ||
|---------------------------------------------------------------------------------------------------------------|-----------------------------|
| [UserData.Builder](https://developer.android.com/reference/kotlin/android/service/autofill/UserData.Builder#) | This value cannot be`null`. |

|                                                                                                                                                                                                                                                                                                                                                                                                  Exceptions                                                                                                                                                                                                                                                                                                                                                                                                  ||
|--------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `java.lang.IllegalStateException`    | if: - [build()](https://developer.android.com/reference/kotlin/android/service/autofill/UserData.Builder#build())already called - the`value`has already been added (**Note:** this restriction was lifted on Android[android.os.Build.VERSION_CODES#Q](https://developer.android.com/reference/kotlin/android/os/Build.VERSION_CODES.html#Q:kotlin.Int)and later) - the number of unique`categoryId`values added so far is more than[UserData.getMaxCategoryCount()](https://developer.android.com/reference/kotlin/android/service/autofill/UserData#getMaxCategoryCount()) - the number of`values`added so far is is more than[UserData.getMaxUserDataSize()](https://developer.android.com/reference/kotlin/android/service/autofill/UserData#getMaxUserDataSize()) |
| `java.lang.IllegalArgumentException` | if any of the following occurs: - `id`is empty - `categoryId`is empty - `value`is empty - the length of`value`is lower than[UserData.getMinValueLength()](https://developer.android.com/reference/kotlin/android/service/autofill/UserData#getMinValueLength()) - the length of`value`is higher than[UserData.getMaxValueLength()](https://developer.android.com/reference/kotlin/android/service/autofill/UserData#getMaxValueLength())                                                                                                                                                                                                                                                                                                                               |

### build

Added in[API level 28](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
fun build(): UserData
```

Creates a new[UserData](https://developer.android.com/reference/kotlin/android/service/autofill/UserData)instance.

You should not interact with this builder once this method is called.

|                                                                    Return                                                                    ||
|----------------------------------------------------------------------------------------------|------------------------------------------------|
| [UserData](https://developer.android.com/reference/kotlin/android/service/autofill/UserData) | The built dataset. This value cannot be`null`. |

|                                                                             Exceptions                                                                              ||
|-----------------------------------|----------------------------------------------------------------------------------------------------------------------------------|
| `java.lang.IllegalStateException` | if[build()](https://developer.android.com/reference/kotlin/android/service/autofill/UserData.Builder#build())was already called. |

### setFieldClassificationAlgorithm

Added in[API level 28](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
fun setFieldClassificationAlgorithm(
    name: String?, 
    args: Bundle?
): UserData.Builder
```

Sets the default algorithm used for[field classification](https://developer.android.com/reference/kotlin/android/service/autofill/UserData.Builder#FieldClassification).

The currently available algorithms can be retrieve through[AutofillManager.getAvailableFieldClassificationAlgorithms()](https://developer.android.com/reference/kotlin/android/view/autofill/AutofillManager.html#getAvailableFieldClassificationAlgorithms()).

If not set, the[default algorithm](https://developer.android.com/reference/kotlin/android/view/autofill/AutofillManager.html#getDefaultFieldClassificationAlgorithm())is used instead.

|                                                                       Parameters                                                                        ||
|--------|-------------------------------------------------------------------------------------------------------------------------------------------------|
| `name` | [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)?:name of the algorithm or`null`to used default.                |
| `args` | [Bundle](https://developer.android.com/reference/kotlin/android/os/Bundle.html#)?:optional arguments to the algorithm. This value may be`null`. |

|                                                                         Return                                                                          ||
|---------------------------------------------------------------------------------------------------------------|------------------------------------------|
| [UserData.Builder](https://developer.android.com/reference/kotlin/android/service/autofill/UserData.Builder#) | this builder This value cannot be`null`. |

### setFieldClassificationAlgorithmForCategory

Added in[API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
fun setFieldClassificationAlgorithmForCategory(
    categoryId: String, 
    name: String?, 
    args: Bundle?
): UserData.Builder
```

Sets the algorithm used for[field classification](https://developer.android.com/reference/kotlin/android/service/autofill/UserData.Builder#FieldClassification)for the specified category.

The currently available algorithms can be retrieved through[AutofillManager.getAvailableFieldClassificationAlgorithms()](https://developer.android.com/reference/kotlin/android/view/autofill/AutofillManager.html#getAvailableFieldClassificationAlgorithms()).

If not set, the[default algorithm](https://developer.android.com/reference/kotlin/android/view/autofill/AutofillManager.html#getDefaultFieldClassificationAlgorithm())is used instead.

|                                                                          Parameters                                                                           ||
|--------------|-------------------------------------------------------------------------------------------------------------------------------------------------|
| `categoryId` | [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html):autofill field category. This value cannot be`null`.           |
| `name`       | [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)?:name of the algorithm or`null`to used default.                |
| `args`       | [Bundle](https://developer.android.com/reference/kotlin/android/os/Bundle.html#)?:optional arguments to the algorithm. This value may be`null`. |

|                                                                         Return                                                                          ||
|---------------------------------------------------------------------------------------------------------------|------------------------------------------|
| [UserData.Builder](https://developer.android.com/reference/kotlin/android/service/autofill/UserData.Builder#) | this builder This value cannot be`null`. |