Android-Lib-reCAPTCHA

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.

To use reCAPTCHA with Android, you can download the reCAPTCHA Android Library here. Typically the only thing you'll need is the jar file (android-lib-recaptcha-x.x.x.jar), which you have to copy to a place where it can be loaded by your IDE. For example, if you are using Eclipse, you can put the jar file in a directory called /libs.

Quick Start

After you've signed up for your API keys and downloaded the reCAPTCHA Android Library, below are basic instructions for using CAPTCHA in your Android application.

The Layout

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

<android.lib.recaptcha.ReCaptcha
    android:id="@+id/recaptcha"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    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(R.id.recaptcha);
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.

Sample Application

A complete sample application is available at https://github.com/ayltai/Android-Lib-reCAPTCHA/tree/master/reCAPTCHA-Samples.

Built With

Share this project:

Updates