Android
The SDK for Android embeds the widget in your app and reports the condition of the challenge with a listener.
Requirements
The following requirements are met:
| Requirement | Value |
|---|---|
| Android | From version 4.1 Jelly Bean, API level 16 |
| Language | Kotlin or Java. The SDK is written in Kotlin. |
| Credentials | Public sitekey from the Details view |
Embed the SDK
Add these entries to the build.gradle file:
repositories {
mavenCentral()
}
dependencies {
implementation "eu.eucaptcha.android:eu-captcha-android:1.0.0"
// Or for `build.gradle.kts`
// implementation("eu.eucaptcha.android:eu-captcha-android:1.0.0")
}
Make the widget
Make the SDK and the widget in your activity:
import eu.eucaptcha.android.sdk.*
const val EU_CAPTCHA_SITEKEY = "EUCAPTCHA_SITE_KEY"
class MainActivity : ComponentActivity() {
private val sdk by lazy {
EuCaptchaSDK(context = this)
}
private val widget by lazy {
sdk.createWidget(sitekey = EU_CAPTCHA_SITEKEY)
}
}
The optional theme parameter takes the values "light" and "dark". Without a value, the widget follows the light or dark mode of the device.
Show the widget
Show the widget in your interface and read the condition:
val captchaResponse = remember { mutableStateOf("") }
var buttonEnabled by remember { mutableStateOf(false) }
widget.setOnStateChangeListener { event ->
captchaResponse.value = event.response
when (event.state) {
"completed" -> buttonEnabled = true
"expired" -> buttonEnabled = false
"error" -> buttonEnabled = true
}
}
AndroidView(factory = { _ -> widget.view })
Button(onClick = { widget.reset() }, enabled = buttonEnabled) {
Text("Submit")
}
The listener reports these conditions:
| Condition | Meaning |
|---|---|
completed |
The challenge passed. The response field contains the token. |
expired |
The token expired. |
error |
An error occurred during the verification. |
destroyed |
The widget was removed with widget.destroy(). |
For single events, the setOnCompleteListener, setOnErrorListener, and setOnExpireListener receivers are also available.
The widget.reset() call starts a new challenge. As long as the widget is not loaded, the call has no effect.
The widget.destroy() call hides the widget and releases the resources. After this, the widget reports no more conditions.
Verify the token
Send the token to your server and verify it there. See Verify a client token.