How to Create a Credit Card Form in Android? - GeeksforGeeks

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. 
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 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.  
Attributes
Description
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:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    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:
 
 
ruhelaa48
Android
Java
Java
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Resource Raw Folder in Android Studio
Flutter - Custom Bottom Navigation Bar
How to Read Data from SQLite Database in Android?
How to Post Data to API using Retrofit in Android?
Retrofit with Kotlin Coroutine in Android
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Object Oriented Programming (OOPs) Concept in Java
Arrays.sort() in Java with examples