This project is no longer maintained
reCAPTCHA v1 is official unsupported since May of 2016 (Link):
reCAPTCHA V1 has been deprecated since May of 2016 and will not work for new sites. reCAPTCHA v1 is no longer supported and continued functionality can not be guaranteed. Please switch to one of the options above.
reCAPTCHA v2 is purely JavaScript based which makes it Android-unfriendly.
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.
First you have to sign up for your API keys.
Installation
repositories { jcenter() } 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.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
.
Specify a locale
You can force the widget to render in a specific language. Please refer to this page.
reCaptcha.setLanguageCode("fr");
A complete sample application is available at https://github.com/ayltai/Android-Lib-reCAPTCHA/tree/master/reCAPTCHA-Samples.