This library provides an elegant form for credit card entry that can be easily added to a activity or fragment. Regex is used to validate credit card types and a Luhn check is performed on the card numbers. This form was inspired by the credit entry UI on Square.
This project uses JitPack to build and release.
Add JitPack to the end of your repositories
repositories {
...
maven { url "https://jitpack.io" }
}
Add the project to your dependencies
dependencies {
...
compile 'com.github.dbachelder:CreditCardEntry:1.4.7'
}
Please see below for an example.
XML
<com.devmarvel.creditcardentry.library.CreditCardForm
android:id="@+id/credit_card_form"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:helper_text_color="@color/yellow_500"
app:include_helper="false"
app:include_zip="false"/>
app:text_color
- change the input field's text color (Color.BLACK
by default).app:hint_text_color
- change the input field's hint text color (Color.LTGRAY
by default).app:cursor_color
- change the input field's cursor color (Color.BLACK
by default).app:default_text_colors
- If true, use text colors provided by the app's theme instead of the
values provided by app:text_color
,app:hint_text_color
, and app:cursor_color
. This overrides
the values for those three text colors and causes the text inputs to use the colors provided by
the application's theme.app:include_helper
- boolean to show/hide the helper text under the widget (true
by default (i.e. helper is shown))app:helper_text_color
- change the text color of the hints that appear below the widget by default.app:include_zip
- boolean to show/hide the zip code in the form (true
by default (i.e. zip is shown))app:include_exp
- boolean to show/hide the exp in the form (true
by default (i.e. exp is shown))app:include_security
- boolean to show/hide the security code in the form (true
by default (i.e. security is shown))app:card_number_hint
- string to put in as a placeholder (hint) in the credit card number fieldapp:input_background
- the drawable to use as a background (defaults to white square with black 1px border)In code:
public class MainActivity extends Activity {
private LinearLayout linearLayout;
private CreditCardForm form;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout = (LinearLayout) findViewById(R.id.layer);
form = new CreditCardForm(this);
linearLayout.addView(form);
buttonAuthorize = (Button) findViewById(R.id.buttonAuthorize);
buttonAuthorize.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if(form.isCreditCardValid())
{
CreditCard card = form.getCreditCard();
//Pass credit card to service
}
else
{
//Alert Credit card invalid
}
}
});
}
isCreditCardValid()
returning true
when it is not