Skip to content

TheSpiffiest/CreditCardView

 
 

Repository files navigation

CreditCard View Build Maven Central

![Feature Image](images/Feature Image.png)

CreditCardView is an Android library that allows developers to create the UI which replicates an actual Credit Card.

Displaying and entering the credit card details inside the app has become a very common use case seen in a lot of different apps, but it is often represented in a not so intuitive manner. With Android Pay being announced at the recent Google I/O 2015, more apps would require users to input their credit card details. I created this library with the aim of making the process of displaying and entering the credit card details more visually appealing to the users of your app.

Screenshots

Screenshots

Features

  • Pre-built card backgrounds to help you get started quickly
  • Fully Customizable
  • Auto selection of drawables based on the credit card number pattern i.e. Visa, Mastercard,American Express & Discover. Will be adding more soon based on the requests I get
  • Auto selection of logo drawable based on the credit card type i.e. Visa, Mastercard and American Express
  • Editable and non-editable mode
  • 4 different card number formats

Setup

The library is pushed to Maven Central as an AAR, so you just need to add the following to your build.gradle file:

dependencies {
    compilecom.vinaygaba:creditcardview:1.0.1’
}

Usage

Using CreditCardView is extremely easy, this is how you would declare it in the layout xml:

<com.example.vinay.library.CreditCardView
        android:id="@+id/card1"
        android:layout_width="fill_parent"
        android:layout_height="225dp"
        android:background="@drawable/cardbackground_world"
        android:layout_marginBottom="16dp"
        android:layout_marginTop="16dp"
        app:cardNumber="5500005555555559"
        app:cardName="Vinay Gaba"
        app:cardNumberTextColor="#cccccc"
        app:cardNumberFormat="masked_all_but_last_four"
        app:cardNameTextColor="#cccccc"
        app:type="auto"
        app:brandLogo="@drawable/brandlogo"
        app:putChip="true"
        app:expiryDate = "02/22"
        app:expiryDateTextColor="#cccccc"
        app:isEditable="true"
        app:validTillTextColor="#cccccc"
        app:hintTextColor = "#cccccc"
        />

Remember to put this for custom attribute usage:

xmlns:app="http://schemas.android.com/apk/res-auto"

And this is how you would be adding it programatically in java:

CreditCardView creditCardView= new CreditCardView(this);

OR

CreditCardView creditCardView= (CreditCardView)findViewById(R.id.ID_OF_CARD);

Attribute Usage & Documentation

Attribute Description

I) android:background

Use this attribute to set the background of the card. This library includes 3 background by default which you can use, but feel free to put any drawable and use it as the card background as you please. If you do not want to use your own drawable and want to use the drawables available in the screenshots, do the following:

1)Sky Background

Sky

To use this background,simply use the line:

android:background = "@drawable/cardbackground_sky"

2)World Background

World

To use this background,simply use the following line:

android:background = "@drawable/cardbackground_world"
  1. Plain Background

Plain

This is a customizable plain background where you can change the background color, radius and border color of the card. To use this, add the folowing line:

android:background = "@drawable/cardbackground_plain"

To customize the corner radius of the card, add the following attribute to your dimen.xml file with the attribute name "card_corner_radius":

<dimen name="card_corner_radius">size_in_dip</dimen>      //Default value is 10dip

To customize the background color and the border color of this card, add the following attributes to your color.xml file:

<color name="card_background">color_value</color>        //Default value is #e5e5e5
<color name="card_border">color_value</color>            //Default value is #ffffff

4)Custom Background

You can essentaially set any background you want as the card background. You can set the value in xml using:

android:background="@drawable/drawable_name"

You can set the value of this attribute programmatically using:

//Set Card Background
creditCardView.setBackgroundResource(R.drawable.drawable_name);
II) app:isEditable

Use this attribute if you want to make the card number, card name and the expiry date field editable to the user.

Editable Gif

You can set the value in xml using:

app:isEditable="true/false"

You can set the value of this attribute programmatically using:

//Set Is Editable Value
creditCardView.setIsEditable(true/false);

//Get Is Editable Value 
boolean isEditable= crediCardView.getIsEditable();

Note: The card type auto detection and space after every 4 letters is added as soon as the focus is shifted from the edit field

III) app:cardNumber

Use this attribute to set the card number of the card.

You can set the value in xml using:

app:cardNumber="1234567890123456"

You can set and get the value of this attribute programmatically using:

//Set Card Number
crediCardView.setCardNumber("1234567890123456");

//Get Card Number
String cardNumber = crediCardView.getCardNumber();
IV) app:cardNumberTextColor

Use this attribute to set the text color of card number attribute.

You can set the value in xml using:

app:cardNumberTextColor="#ffffff"

You can set and get the value of this attribute programmatically using:

//Set Card Number Text Color
creditCardView.setCardNumberTextColor(Color.WHITE);

//Get Card Number Text Color
int color = crediCardView.getCardNumberTextColor();
V) app:cardNumberFormat

Use this attribute to set the card number format of card number. There are four different formats supported by the library:

Card number Format Image

  1. all_digits - This will display all the numbers of the card number.
  2. masked_all_but_last_four - This will mask all the digits except the last four of the card number.
  3. only_last_four - This will display only the last four digits of the card number.
  4. masked_all - This will mask all the digits of the card number.

You can set the value in xml using:

app:cardNumberFormat="all_digits/masked_all_but_last_four/only_last_four/masked_all"    //Use any one format type

You can set and get the value of this attribute programmatically using:

//Set Card Number Format. Chooose any one format
creditCardView.setCardNumberFormat(CardNumberFormat.ALL_DIGITS/CardNumberFormat.MASKED_ALL_BUT_LAST_FOUR/CardNumberFormat.ONLY_LAST_FOUR/CardNumberFormat.MASKED_ALL);

//Get Card Number Format
int cardFormat = crediCardView.getCardNumberFormat();

Note: Default value is all_digits

VI) app:cardName

Use this attribute to set the card name of the card.

You can set the value in xml using:

app:cardName="John Doe"

You can set and get the value of this attribute programmatically using:

//Set Card Number
crediCardView.setCardName("John Doe");

//Get Card Number
String cardNumber = crediCardView.getCardName();
VII) app:cardNameTextColor

Use this attribute to set the text color of card name attribute.

You can set the value in xml using:

app:cardNameTextColor="#ffffff"

You can set and get the value of this attribute programmatically using:

//Set Card Name Text Color
creditCardView.setCardNameTextColor(Color.WHITE);

//Get Card Name Text Color
int color = crediCardView.getCardNamerTextColor();
VIII) app:expiryDate

Use this attribute to set the expiry date of the card in MM/YY or MM/YYYY format.

You can set the value in xml using:

app:expiryDate="01/15"

You can set and get the value of this attribute programmatically using:

//Set Expiry Date
crediCardView.setExpiryDate("01/15");

//Get Card Number
String expiryDate = crediCardView.ExpiryDate();
IX) app:expiryDateTextColor

Use this attribute to set the text color of expiry date attribute.

You can set the value in xml using:

app:expiryDateTextColor="#ffffff"

You can set and get the value of this attribute programmatically using:

//Set Expiry Date Text Color
creditCardView.setExpiryDateTextColor(Color.WHITE);

//Get Expiry Date Text Color
int color = crediCardView.getExpiryDateTextColor();
X) app:putChip

Use this attribute if you want the card to display the chip on the card.

Put Chip

You can set the value in xml using:

app:putChip="true/false"

You can set the value of this attribute programmatically using:

//Set Put Chip Value
creditCardView.putChip(true/false);
XI) app:type

Use this attribute to set the type of the credit card. The library automatically places the corresponding drawable in the bottom right corner based on the type you have selected. Currectly there are 5 different types supported:

  1. visa
  2. mastercard
  3. american_express
  4. discover
  5. auto - Use auto if u want the library to automatically choose the card type based on the card number you have entered. To know more about the patterns for identifying the card type from the card number, see this link

You can set the value in xml using:

app:type="visa/mastercard/americann_express/discover/auto"

You can set the value of this attribute programmatically using:

//Set Card Type.Choose any one card tpe from the following
creditCardView.setType(CardType.VISA/CardType.MASTERCARD/CardType.AMERICAN_EXPRESS/CardType.DISCOVER/CardType.AUTO);

//Get Card Type. 
int type = crediCardView.getType();
XII) app:brandLogo

Use this attribute to set the brand logo drawable that you see in the upper right corner.

You can set the value in xml using:

app:brandLogo="@drawable/drawable_name"

You can set and get the value of this attribute programmatically using:

//Set Brand Logo
crediCardView.setBrandLogo(R.drawable.drawable_name);

If you wish to modify the default dimensions of the brand logo, add the following attribute to your dimen.xml file:

<dimen name="brand_logo_width">size_in_dp</dimen>    //Default value is 60dp
<dimen name="brand_logo_height">size_in_dp</dimen>   //Default value is 30dp
XIII) app:hintTextColor

Use this attribute to set the hint text color that is visible when card name, card number and expiry date fields are editable and blank

You can set the value in xml using:

app:hintTextColor="color_value"    //Default is White

You can set and get the value of this attribute programmatically using:

//Set Hint Text Color
crediCardView.setHintTextColor(color_value);

//Get Hint Text Color
int color = crediCardView.geHintTextColor();

Contributing

Please use the issue tracker to report any bugs or file feature requests. There are a few features that I plan to work on based on the response the library gets, some of them being:

  • Tablet Optimization. The current version is not optimized for tablets
  • Landscape Optimization. The current version is not optimized for landscape mode
  • Credit Card back view to display the CVV number
  • Animations and touch callbacks
  • Stack View to display multiple cards

I would love to get more people involved in the development of this library. A lot of times people are not sure about how they should be contributing to open source. If you are one of them, this is a great opportunity for you to get involved. You can also reach out to me for any queries that you might have about this library.

Credits

Author: Vinay Gaba (vinaygaba@gmail.com)

Follow me on Google+ Follow me on Twitter Follow me on LinkedIn

License

Copyright 2015 Vinay Gaba

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

CreditCardView is an Android library that allows developers to create the UI which replicates an actual Credit Card.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%