Skip to content
Related Articles

Related Articles

Improve Article

How to Create a Credit Card Form in Android?

  • Last Updated : 02 Jul, 2021

Many of the apps are having functionality that provides a beautiful UI for adding your Credit Cards inside their application and saving them. This type of functionality is seen in the CRED application where we can save our Credit Cards inside that application. In this article, we will be simply creating a Credit Card Form that is seen in that application. We will be creating a UI for the credit card in our app. 

What we are going to build in this article? 

We will be building a simple application in which we will be displaying a credit card and some text fields to enter the data in that text fields. After entering that data we will get to see the data being displayed in the card which is displayed above. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language. 

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Add dependency and JitPack Repository



Navigate to the Gradle Scripts > build.gradle(Module:app) and add the below dependency in the dependencies section.   

implementation ‘com.github.fevziomurtekin:PayView:1.0.3’

Add the JitPack repository to your build file. Add it to your root build.gradle at the end of repositories inside the allprojects{ } section.

allprojects {

 repositories {

   …

   maven { url “https://jitpack.io” }

     }

}

After adding this dependency sync your project and now we will move towards its implementation.  

Important Attributes used for PayView 

Attributes

Description

cardBgColor This attribute is used to provide background color to our card.
cardFgColor This attribute is used to provide foreground color to our card. 
cardTextColorThis attribute is used to provide text color which is used in our card. 
cardAnimationTypeThere are 2 animations used in card which are horizontal and vertical. They are used when we add cvv.
cardNameTextSizeThis attribute is to specify the text size of the name which we add for our card. 
cardNoTextSizeThis attribute is to specify the text size of the card number which we add for our card. 
cardYearTextSizeThis attribute is to specify the text size of the card year which we add for our card. 
cardMonthTextSizeThis attribute is to specify the text size of the card Month which we add for our card. 
cardCvTextSizeThis attribute is to specify the text size of the card cvv which we add for our card. 
cardNumberHelperTextThis attribute is used to provide hint for entering card number. 
cardNameHelperTextThis attribute is used to provide hint for entering card name. 
cardCvErrorTextThis attribute is used to provide hint for entering card cvv. 
cardMonthErrorTextThis attribute is used to provide hint for entering card month. 
cardYearErrorTextThis attribute is used to provide hint for entering card year. 
cardExpiredErrorTextThis attribute is used to provide hint when the entered card year is invalid. 

Step 3: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 



XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
 
    <!--on below line we are creating a pay view-->
    <com.fevziomurtekin.payview.Payview
        android:id="@+id/payview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:cardAnimationType="vertical"
        app:cardBgColor="@color/purple_200"
        app:cardCvErrorText="You must enter 3-digit characters"
        app:cardCvTextSize="14"
        app:cardExpiredErrorText="Your card has expired. Please enter the usage date correctly."
        app:cardFgColor="@android:color/white"
        app:cardMonthErrorText="You must enter 2-digit characters and you'll enter to number the most digit-value is '12'"
        app:cardMonthTextSize="13"
        app:cardNameHelperText="Enter to name on Your Card."
        app:cardNameTextSize="15"
        app:cardNoTextSize="14"
        app:cardNumberHelperText="Enter your 16-digit card number."
        app:cardTextColor="@color/purple_200"
        app:cardYearErrorText="You must enter 2-digit characters and you'll enter to number the most digit-value is '99'"
        app:cardYearTextSize="13" />
     
</RelativeLayout>

 

 

Step 4: Working with the MainActivity.java file

 

Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.

 

Java




import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
 
import androidx.appcompat.app.AppCompatActivity;
 
import com.fevziomurtekin.payview.Payview;
 
public class MainActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         
        // on below line we are creating a variable
        // for our pay view and initializing it.
        Payview payview = findViewById(R.id.payview);
         
        // on below line we are setting pay on listener for our card.
        payview.setPayOnclickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // after clicking on pay we are displaying toast message as card added.
                Toast.makeText(MainActivity.this, "Card Added. ", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

 

 

Now run your app and see the output of the code. 

 

Output:

 

 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :