> For the complete documentation index, see [llms.txt](https://docs.hyperswitch.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.hyperswitch.io/integration-guide/payment-experience/pay-then-vault/mobile/android/widgets/card-element.md).

# Card Element

**Purpose:** Card payments with Juspay Hyperswitch

**Add Card Widget to Layout**

```xml
<io.hyperswitch.view.BasePaymentWidget
    android:id="@+id/cardElement"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:paymentMethod="card" />

<Button
    android:id="@+id/confirmButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Pay with Card" />
```

**Initialize Card Launcher**

```kotlin
private lateinit var cardPaymentLauncher: UnifiedPaymentLauncher

private fun setupCardPayment() {
    cardPaymentLauncher = UnifiedPaymentLauncher.createCardLauncher(
        activity = this,
        resultCallback = ::onPaymentResult
    )
}
```

**Handle Card Payment**

```kotlin
private fun processCardPayment() {
    val cardInputWidget: BasePaymentWidget = findViewById(R.id.cardElement)
    val params: PaymentMethodCreateParams = cardInputWidget.paymentMethodCreateParams
    val confirmParams = ConfirmPaymentIntentParams.createWithPaymentMethodCreateParams(
        params,
        paymentIntentClientSecret
    )

    if (::cardPaymentLauncher.isInitialized) {
        cardPaymentLauncher.confirmCardPayment(confirmParams)
    } else {
        Toast.makeText(this, "SDK is not initialized", Toast.LENGTH_SHORT).show()
    }
}

// Handle card payment results
private fun onPaymentResult(paymentResult: PaymentResult) {
    when (paymentResult) {
        is PaymentResult.Completed -> {
            Toast.makeText(this, "Payment completed: ${paymentResult.data}", Toast.LENGTH_SHORT).show()
        }
        is PaymentResult.Canceled -> {
            Toast.makeText(this, "Payment canceled: ${paymentResult.data}", Toast.LENGTH_SHORT).show()
        }
        is PaymentResult.Failed -> {
            Toast.makeText(this, "Payment failed: ${paymentResult.throwable.message}", Toast.LENGTH_SHORT).show()
        }
    }
}
```

#### Best Practices

**Error Handling**

Always check if launchers are initialized before using them:

```kotlin
if (::cardPaymentLauncher.isInitialized) {
    cardPaymentLauncher.confirmCardPayment(confirmParams)
} else {
    Toast.makeText(this, "SDK is not initialized", Toast.LENGTH_SHORT).show()
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.hyperswitch.io/integration-guide/payment-experience/pay-then-vault/mobile/android/widgets/card-element.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
