Android Create Payment Form
1. Project Structure
2. Gradle
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.payment.form"
minSdkVersion 19
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'com.balysv:material-ripple:1.0.2'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'com.crystal:crystalrangeseekbar:1.1.3'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
3. Main Activity
package com.payment.form;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.appcompat.widget.Toolbar;
import android.view.MenuItem;
import android.widget.Toast;
import com.payment.form.utils.Tools;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_payment_form);
initToolbar();
}
private void initToolbar() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Add new card");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Tools.setSystemBarColor(this);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
} else {
Toast.makeText(getApplicationContext(), item.getTitle(), Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
}
4. Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey_5"
android:fitsSystemWindows="true"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include layout="@layout/toolbar" />
</com.google.android.material.appbar.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end|right"
android:orientation="horizontal"
android:paddingBottom="@dimen/spacing_middle"
android:paddingLeft="@dimen/spacing_large"
android:paddingRight="@dimen/spacing_large"
android:paddingTop="@dimen/spacing_mxlarge">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="end|right"
android:src="@drawable/ic_visa_new" />
<View
android:layout_width="@dimen/spacing_middle"
android:layout_height="0dp" />
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="end|right"
android:src="@drawable/ic_mastercard_new" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="vertical"
android:padding="@dimen/spacing_large">
<View
android:layout_width="0dp"
android:layout_height="@dimen/spacing_mlarge" />
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="-4dp"
android:layout_marginRight="-4dp"
android:hint="Credit card number"
android:inputType="number"
android:maxLength="19"
android:maxLines="1"
android:singleLine="true"
android:text="2258 9913 0286 5521"
android:textColor="@color/blue_grey_800"
android:textColorHint="@color/grey_10"
android:theme="@style/EditText.GreySoft" />
<View
android:layout_width="0dp"
android:layout_height="@dimen/spacing_large" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="-4dp"
android:layout_marginRight="-4dp"
android:layout_weight="1"
android:hint="Valid until (MM/YY)"
android:inputType="number"
android:maxLength="16"
android:maxLines="1"
android:singleLine="true"
android:text="08/25"
android:textColor="@color/blue_grey_800"
android:textColorHint="@color/grey_10"
android:theme="@style/EditText.GreySoft" />
<View
android:layout_width="@dimen/spacing_large"
android:layout_height="0dp" />
<com.google.android.material.textfield.TextInputEditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="-4dp"
android:layout_marginRight="-4dp"
android:layout_weight="1"
android:hint="3-digit CVV"
android:inputType="number"
android:maxLength="16"
android:maxLines="1"
android:singleLine="true"
android:text="381"
android:textColor="@color/blue_grey_800"
android:textColorHint="@color/grey_10"
android:theme="@style/EditText.GreySoft" />
</LinearLayout>
<View
android:layout_width="0dp"
android:layout_height="@dimen/spacing_large" />
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="-4dp"
android:layout_marginRight="-4dp"
android:hint="Name on card"
android:inputType="text"
android:maxLength="16"
android:maxLines="1"
android:singleLine="true"
android:text="Betty L"
android:textColor="@color/blue_grey_800"
android:textColorHint="@color/grey_10"
android:theme="@style/EditText.GreySoft" />
<View
android:layout_width="0dp"
android:layout_height="@dimen/spacing_mlarge" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/spacing_large"
android:orientation="vertical"
android:padding="@dimen/spacing_large">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/spacing_middle"
android:text="By tapping the button, you agree to MaterialX Term & Condition and Privacy Policy."
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
android:textColor="@color/grey_60" />
<Button
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_marginLeft="-4dp"
android:layout_marginRight="-4dp"
android:background="@drawable/btn_orange"
android:text="Add Card"
android:textAllCaps="false"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
android:textColor="@android:color/white"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
5. Value
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Base.TextAppearance.AppCompat.Small" parent="android:TextAppearance.Material.Small"/>
<style name="Base.TextAppearance.AppCompat.Medium" parent="android:TextAppearance.Material.Medium"/>
<style name="EditText.GreySoft" parent="@style/AppTheme">
<item name="colorControlActivated">@color/grey_40</item>
<item name="colorControlHighlight">@color/grey_40</item>
<item name="colorControlNormal">@color/grey_20</item>
</style>
<color name="blue_grey_800">#37474F</color>
<color name="grey_10">#e6e6e6</color>
<color name="grey_60">#666666</color>
</resources>