java – How to Integrate reCAPTCHA 2.0 in Android-ThrowExceptions

Exception or error:

Is there any way I can integrate reCAPTCHA 2.0 in Android?
I found this library and got it working. However, server side verification of the CAPTCHA is not supported (it needs me to provide the private key in the code then verify it within the app instead of talking to my own server).

  1. Is there a way to integrate reCAPTCHA 2.0 in Android?
  2. Or is there a way for me to verify the CAPTCHA on my own server with that library?
How to solve:

Fork this android library and modify the server-side logic :

The reCAPTCHA Android Library provides a simple way to show a CAPTCHA as an ImageView in your Android app, helping you stop bots from abusing it. The library wraps the reCAPTCHA API.


repositories {

dependencies {
    compile 'android.lib.recaptcha:reCAPTCHA:+'

The Layout

To show CAPTCHA image, you need to add a <android.lib.recaptcha.ReCaptcha /> element to your layout XML:

    android:scaleType="centerInside" />

It is important to use android:scaleType="centerInside" to ensure the entire CAPTCHA image can be displayed.

Alternatively, you can create an instance of android.lib.recaptcha.ReCaptcha at runtime:

ReCaptcha reCaptcha = new ReCaptcha(context);

How to show CAPTCHA

In your activity/fragment/view containing android.lib.recaptcha.ReCaptcha, you need display a CAPTCHA image for the user to response:

ReCaptcha reCaptcha = (ReCaptcha)findViewById(;
reCaptcha.showChallengeAsync("your-public-key", onShowChallengeListener);

showChallengeAsync downloads and shows CAPTCHA image asynchronously. It is safe to invoke in UI thread. No exception will be thrown in case of any error by this call. All errors will be treated as unsuccessful in showing CAPTCHA image.

onShowChallengeListener is an instance of ReCaptcha.OnShowChallengeListener, which is called when an attempt to show a CAPTCHA is completed.

The synchronous version of this method is showChallenge.

How to verify user input

To verify user input, pass the input string to ReCaptcha.verifyAnswerAsync (or ReCaptcha.verifyAnswer):

reCaptcha.verifyAnswerAsync("your-private-key", "user-input", onVerifyAnswerListener);

verifyAnswerAsync asynchronously submits the user input string to reCAPTCHA server for verification. It is safe to invoke in UI thread. No exception will be thrown in case of any error by this call. All errors will be treated as verification failure.

onVerifyAnswerListener is an instance of ReCaptcha.OnVerifyAnswerListener, which is called when an attempt to verify the user input is completed.

The synchronous version of this method is verifyAnwser.

Specify a locale

You can force the widget to render in a specific language. Please refer to this page.



One approach would be to create an HTML file with a working reCaptcha 2.0 form (reCAPTCHA Docs) and host that on a web site (make it responsive so it looks nice).

then load the URL on a WebView and make a bridge so you can interact between Java and Javascript (addJavascriptInterface)

Android Activity:

WebView mWebView = (WebView) findViewById(;

mWebView.addJavascriptInterface(new BridgeWebViewClass(this), "BridgeWebViewClass");

Bridge Class:

public class BridgeWebViewClass {

    public void reCaptchaCallbackInAndroid(String g_response){
        log.d("reCaptcha", "token" + g_response);

And now from your HTML file you can run the Bridge Class as a Javascript function:

<div class="g-recaptcha" data-sitekey="YOUR_CAPTCHA_SITE_KEY" data-callback="captchaResponse"></div>

<script type="text/javascript">
    function captchaResponse(token){

Now you can verify the response from Android calling

Hope this helps.


You can add Google reCaptcha in your android application with the help of SafetyNet google library it provides Google reCaptcha API.

  1. Create Google ReCaptcha API from google ReCaptcha website.
  2. Add SafetyNet library in your android project

    implementation ‘’

  3. Call reCaptcha on click on the button and add below code to validate users using below code

For complete tutorial please check this post:-

Leave a Reply

Your email address will not be published. Required fields are marked *