# Address

Learn about customisation options for the address component.

## Styling

```kotlin
val addressComponentConfig = AddressComponentConfig(
    isRequired = Boolean,
    maxLength = Int,
    style = AddressStyle?,
    validations = AddressValidations,
    label = String,
    placeholder = String,
    showTrailingIcon = Boolean,
    showValidIcon = Boolean,
    validIcon = Int,
    showInvalidIcon = Boolean,
    invalidIcon = Int
)
```

| Property | Description |
|  --- | --- |
| `isRequired`Boolean | Whether the field is required for submission. Defaults to `true`. |
| `maxLength`Int | The maximum character length. Defaults to `100`. |
| `style`AddressStyle? | Custom styling configuration. |
| `validations`AddressValidations | Custom validation messages. |
| `label`String | The field label text. Defaults to `"Street"`. |
| `placeholder`String | The placeholder text. Defaults to `"Example Street, City"`. |
| `showTrailingIcon`Boolean | Whether to show a trailing icon. Defaults to `true`. |
| `showValidIcon`Boolean | Whether to show a checkmark when valid. Defaults to `true`. |
| `validIcon`Int | The valid state icon resource. |
| `showInvalidIcon`Boolean | Whether to show an error icon when invalid. Defaults to `true`. |
| `invalidIcon`Int | The invalid state icon resource. |


## Callbacks

```kotlin
data class AddressComponentConfig(
    val onChange: ((AddressInputEvent) -> Unit)? = null,
    val onFocus: ((AddressFocusEvent) -> Unit)? = null,
    val onBlur: ((AddressFocusEvent) -> Unit)? = null,
    val onValidationPassed: ((List<ValidationResult>) -> Unit)? = null,
    val onValidationFailed: ((List<ValidationResult>) -> Unit)? = null
)
```

| Callback | Description |
|  --- | --- |
| `onChange: (AddressInputEvent) -> Unit` | Event handler for when the input value changes. |
| `onFocus: (AddressFocusEvent) -> Unit` | Event handler for when the input receives focus. |
| `onBlur: (AddressFocusEvent) -> Unit` | Event handler for when the input loses focus. |
| `onValidationPassed: (List<ValidationResult>) -> Unit` | Event handler for when all validations pass. |
| `onValidationFailed: (List<ValidationResult>) -> Unit` | Event handler for when any validation fails. |


For more information about callbacks, see [Events](/guides/checkout/components/android/card/events).

## Example

```kotlin
enum class AddressValidationCode {
    REQUIRED,    // AD01 - Field is empty but required
    MAX_LENGTH   // AD02 - Exceeds maximum length
}
```

## Component Usage

### Basic Implementation

```kotlin
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.pxp.PxpCheckout
import com.pxp.checkout.components.address.AddressComponent
import com.pxp.checkout.components.address.AddressComponentConfig
import com.pxp.checkout.types.ComponentType

/**
 * Basic Address component implementation using pxpcheckout
 */
@Composable
fun BasicAddressExample(checkout: PxpCheckout) {
    Column(modifier = Modifier.fillMaxWidth()) {
        Text(
            text = "Address Component Example",
            modifier = Modifier.padding(bottom = 16.dp)
        )

        // Create component configuration
        val config = AddressComponentConfig().apply {
            label = "Delivery Address"
            placeholder = "Enter street address"
            isRequired = true
            maxLength = 100
            onChange = { value ->
                // Handle address changes
                println("Address changed to: $value")
            }
            onValidationFailed = { result ->
                // Handle validation failure
                println("Validation failed: ${result.errorMessage}")
            }
            onValidationPassed = { result ->
                // Handle validation success
                println("Address is valid")
            }
        }

        // Create component using pxpcheckout factory
        val component = remember {
            checkout.createComponent<AddressComponent, AddressComponentConfig>(
                type = ComponentType.ADDRESS,
                config = config
            )
        }

        // Render the component using pxpcheckout's buildComponentView
        checkout.buildComponentView(
            component = component,
            modifier = Modifier.fillMaxWidth()
        )
    }
}

/**
 * Activity implementation showing how to initialise and use the component
 */
class PaymentActivity : ComponentActivity() {
    private lateinit var pxpCheckout: PxpCheckout

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // Initialise PXP Checkout SDK (example configuration)
        val sdkConfig = PxpSdkConfig(
            environment = Environment.TEST,
            session = SessionConfig(
                sessionId = "your_session_id",
                sessionData = "your_session_data"
            ),
            transactionData = TransactionData(
                amount = 100.0,
                currency = CurrencyType.USD,
                merchant = "your_merchant_id"
            ),
            clientId = "your_client_id"
        )

        pxpCheckout = PxpCheckout.builder()
            .withConfig(sdkConfig)
            .withContext(this)
            .withDebugMode(true)
            .build()

        setContent {
            BasicAddressExample(checkout = pxpCheckout)
        }
    }
}
```