Card Form is a ready made card form layout that can be included in your app making it easy to accept credit and debit cards.
In your build.gradle
:
dependencies {
compile 'com.braintreepayments:card-form:2.3.0'
}
Card Form is a LinearLayout that you can add to your layout:
<com.braintreepayments.cardform.view.CardForm
android:id="@+id/bt_card_form"
android:layout_width="match_parent"
android:layout_height="match_parent" />
To initialize the view and change which fields are required for the user to enter, use
CardForm#setRequiredFields(Activity activity, boolean cardNumberRequired, boolean expirationRequired, boolean cvvRequired, boolean postalCodeRequired, String imeActionLabel)
.
CardForm cardForm = (CardForm) findViewById(R.id.bt_card_form);
cardForm.setRequiredFields(Activity.this, true, true, false, false, "Purchase");
To access the values in the form, there are getters for each field:
cardForm.getCardNumber();
cardForm.getExpirationMonth();
cardForm.getExpirationYear();
cardForm.getCvv();
cardForm.getPostalCode();
To check if CardForm
is valid call CardForm#isValid()
. To validate each required field
and show the user which fields are incorrect, call CardForm#validate()
.
Additionally CardForm
has 4 available listeners:
CardForm#setOnCardFormValidListener
called when the form changes state from valid to invalid or invalid to valid.CardForm#setOnCardFormSubmitListener
called when the form should be submitted.CardForm#setOnFormFieldFocusedListener
called when a field in the form is focused.CardForm#setOnCardTypeChangedListener
called when theCardType
in the form changes.
The card form is compatible with card.io.
To use card.io, add the dependency in your build.gradle
:
dependencies {
compile 'io.card:android-sdk:[5.3.0,6.0.0)'
}
Check if card.io is available for use:
cardForm.isCardScanningAvailable();
Scan a card:
cardForm.scanCard(activity);
All card form inputs use the colorAccent
theme attribute, when present, to set their focused color.
For more information on the colorAccent
attribute, see Using the Material Theme.
The included sample app has examples of a Holo theme, Material light theme and Material dark theme.
Card Form is open source and available under the MIT license. See the LICENSE file for more info.