Android HOW TO

ANDROID CARD.IO LIBRARY INTEGRATION

Google+ Pinterest LinkedIn Tumblr

Step 1: Creating a new Android Studio project

 

You have to open Android Studio and create a new project by going to File >> New >> New Project. Enter the Application Name as here I have named, MYFlashLight and company domain and package name.

Now, click ‘Next’ and choose Minimum SDK. We have kept the default setting and click Next.

After that, you have to choose Empty Activity and click ‘next’.

 

In the next screen, enter Activity Name like I wrote here MainActivity and remember to check the Generate Layout Button and then click Finish.

2 UPDATE ANDROIDMANIFEST.XML

[code language=”java”] <span class="hljs-attribute">compile</span> <span class="hljs-string">’io.card:android-sdk:5.4.0′</span>
[/code] [code language=”java”]

<span class="php"><span class="hljs-meta"><?</span>xml version=<span class="hljs-string">"1.0"</span> encoding=<span class="hljs-string">"utf-8"</span><span class="hljs-meta">?></span></span> <span class="hljs-tag"><<span class="hljs-name">manifest</span> <span class="hljs-attr">xmlns:android</span>=<span class="hljs-string">"http://schemas.android.com/apk/res/android"</span> <span class="hljs-attr">package</span>=<span class="hljs-string">"com.inducesmile.androidcardio"</span>></span> <span class="hljs-comment"><!– Permission to vibrate – recommended, allows vibration feedback on scan –></span> <span class="hljs-tag"><<span class="hljs-name">uses-permission</span> <span class="hljs-attr">android:name</span>=<span class="hljs-string">"android.permission.VIBRATE"</span> /></span> <span class="hljs-comment"><!– Permission to use camera – required –></span> <span class="hljs-tag"><<span class="hljs-name">uses-permission</span> <span class="hljs-attr">android:name</span>=<span class="hljs-string">"android.permission.CAMERA"</span> /></span> <span class="hljs-comment"><!– Camera features – recommended –></span> <span class="hljs-tag"><<span class="hljs-name">uses-feature</span> <span class="hljs-attr">android:name</span>=<span class="hljs-string">"android.hardware.camera"</span> <span class="hljs-attr">android:required</span>=<span class="hljs-string">"false"</span> /></span> <span class="hljs-tag"><<span class="hljs-name">uses-feature</span> <span class="hljs-attr">android:name</span>=<span class="hljs-string">"android.hardware.camera.autofocus"</span> <span class="hljs-attr">android:required</span>=<span class="hljs-string">"false"</span> /></span> <span class="hljs-tag"><<span class="hljs-name">uses-feature</span> <span class="hljs-attr">android:name</span>=<span class="hljs-string">"android.hardware.camera.flash"</span> <span class="hljs-attr">android:required</span>=<span class="hljs-string">"false"</span> /></span> <span class="hljs-tag"><<span class="hljs-name">application</span> <span class="hljs-attr">android:allowBackup</span>=<span class="hljs-string">"true"</span> <span class="hljs-attr">android:icon</span>=<span class="hljs-string">"@mipmap/ic_launcher"</span> <span class="hljs-attr">android:label</span>=<span class="hljs-string">"@string/app_name"</span> <span class="hljs-attr">android:supportsRtl</span>=<span class="hljs-string">"true"</span> <span class="hljs-attr">android:theme</span>=<span class="hljs-string">"@style/AppTheme"</span>></span> <span class="hljs-tag"><<span class="hljs-name">activity</span> <span class="hljs-attr">android:name</span>=<span class="hljs-string">".MainActivity"</span>></span> <span class="hljs-tag"><<span class="hljs-name">intent-filter</span>></span> <span class="hljs-tag"><<span class="hljs-name">action</span> <span class="hljs-attr">android:name</span>=<span class="hljs-string">"android.intent.action.MAIN"</span> /></span> <span class="hljs-tag"><<span class="hljs-name">category</span> <span class="hljs-attr">android:name</span>=<span class="hljs-string">"android.intent.category.LAUNCHER"</span> /></span> <span class="hljs-tag"></<span class="hljs-name">intent-filter</span>></span> <span class="hljs-tag"></<span class="hljs-name">activity</span>></span> <span class="hljs-comment"><!– Activities responsible for gathering payment info –></span> <span class="hljs-tag"><<span class="hljs-name">activity</span> <span class="hljs-attr">android:name</span>=<span class="hljs-string">"io.card.payment.CardIOActivity"</span> <span class="hljs-attr">android:configChanges</span>=<span class="hljs-string">"keyboardHidden|orientation"</span> /></span> <span class="hljs-tag"><<span class="hljs-name">activity</span> <span class="hljs-attr">android:name</span>=<span class="hljs-string">"io.card.payment.DataEntryActivity"</span> /></span> <span class="hljs-tag"></<span class="hljs-name">application</span>></span> <span class="hljs-tag"></<span class="hljs-name">manifest</span>></span>

[/code]

3UPDATE DEPENDENCIES IN BUILD.GRADLE FILE

 

[code language=”java”]

compile <b>‘io.card:android-sdk:5.4.0’</b>

[/code] [code language=”java”]

<span class="hljs-attribute">apply</span> plugin: <span class="hljs-string">’com.android.application'</span> android { <span class="hljs-attribute">compileSdkVersion</span> <span class="hljs-number">24</span> buildToolsVersion <span class="hljs-string">"24.0.0"</span> defaultConfig { <span class="hljs-attribute">applicationId</span> <span class="hljs-string">"com.inducesmile.androidcardio"</span> minSdkVersion <span class="hljs-number">14</span> targetSdkVersion <span class="hljs-number">24</span> versionCode <span class="hljs-number">1</span> versionName <span class="hljs-string">"1.0"</span> } buildTypes { <span class="hljs-section">release</span> { <span class="hljs-attribute">minifyEnabled</span> <span class="hljs-literal">false</span> proguardFiles getDefaultProguardFile(<span class="hljs-string">’proguard-android.txt'</span>), <span class="hljs-string">’proguard-rules.pro'</span> } } } dependencies { <span class="hljs-attribute">compile</span> fileTree(dir: <span class="hljs-string">’libs'</span>, include: [<span class="hljs-string">’*.jar'</span>]) testCompile <span class="hljs-string">’junit:junit:4.12′</span> compile <span class="hljs-string">’com.android.support:appcompat-v7:24.1.1′</span> compile <span class="hljs-string">’io.card:android-sdk:5.4.0′</span> }

[/code]

4 STRINGS.XML

[code language=”java”]

</pre>
<span class="hljs-tag"><<span class="hljs-name">resources</span>></span> <span class="hljs-tag"><<span class="hljs-name">string</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"app_name"</span>></span>Android Cardio<span class="hljs-tag"></<span class="hljs-name">string</span>></span> <span class="hljs-tag"><<span class="hljs-name">string</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"scan_credit_card"</span>></span>Scan Credit Card<span class="hljs-tag"></<span class="hljs-name">string</span>></span> <span class="hljs-tag"></<span class="hljs-name">resources</span>> [/code]

 

5COLORS.XML

[code language=”java”]

<span class="php"><span class="hljs-meta"><?</span>xml version=<span class="hljs-string">"1.0"</span> encoding=<span class="hljs-string">"utf-8"</span><span class="hljs-meta">?></span></span> <span class="hljs-tag"><<span class="hljs-name">resources</span>></span> <span class="hljs-tag"><<span class="hljs-name">color</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"colorPrimary"</span>></span>#3F51B5<span class="hljs-tag"></<span class="hljs-name">color</span>></span> <span class="hljs-tag"><<span class="hljs-name">color</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"colorPrimaryDark"</span>></span>#303F9F<span class="hljs-tag"></<span class="hljs-name">color</span>></span> <span class="hljs-tag"><<span class="hljs-name">color</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"colorAccent"</span>></span>#FF4081<span class="hljs-tag"></<span class="hljs-name">color</span>></span> <span class="hljs-tag"><<span class="hljs-name">color</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"colorWhite"</span>></span>#ffffff<span class="hljs-tag"></<span class="hljs-name">color</span>></span> <span class="hljs-tag"></<span class="hljs-name">resources</span>></span>
[/code]

6ACTIVITY_MAIN.XML

[code language=”java”]

<?xml version=<span class="hljs-string">"1.0"</span> encoding=<span class="hljs-string">"utf-8"</span>?> <RelativeLayout xmlns:android=<span class="hljs-string">"http://schemas.android.com/apk/res/android"</span> xmlns:tools=<span class="hljs-string">"http://schemas.android.com/tools"</span> android:layout_width=<span class="hljs-string">"match_parent"</span> android:layout_height=<span class="hljs-string">"match_parent"</span> android:paddingBottom=<span class="hljs-string">"@dimen/activity_vertical_margin"</span> android:paddingLeft=<span class="hljs-string">"@dimen/activity_horizontal_margin"</span> android:paddingRight=<span class="hljs-string">"@dimen/activity_horizontal_margin"</span> android:paddingTop=<span class="hljs-string">"@dimen/activity_vertical_margin"</span> tools:context=<span class="hljs-string">"com.inducesmile.androidcardio.MainActivity"</span>> <Button android:id=<span class="hljs-string">"@+id/scan_credit_card"</span> android:layout_width=<span class="hljs-string">"match_parent"</span> android:layout_height=<span class="hljs-string">"wrap_content"</span> android:layout_marginTop=<span class="hljs-string">"32dp"</span> android:background=<span class="hljs-string">"@color/colorPrimaryDark"</span> android:text=<span class="hljs-string">"@string/scan_credit_card"</span> android:textColor=<span class="hljs-string">"@color/colorWhite"</span> android:layout_alignParentTop=<span class="hljs-string">"true"</span>/> <<span class="hljs-regexp">/RelativeLayout></span>

[/code]

7MAINACTIVITY.JAVA

[code language=”java”] <span class="hljs-keyword">import</span> android.content.Intent; <span class="hljs-keyword">import</span> android.os.Bundle; <span class="hljs-keyword">import</span> android.support.v7.app.AppCompatActivity; <span class="hljs-keyword">import</span> android.util.Log; <span class="hljs-keyword">import</span> android.view.View; <span class="hljs-keyword">import</span> android.widget.Button; <span class="hljs-keyword">import</span> io.card.payment.CardIOActivity; <span class="hljs-keyword">import</span> io.card.payment.CreditCard; <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MainActivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">AppCompatActivity</span> </span>{ <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> String TAG = MainActivity.class.getSimpleName(); <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> MY_SCAN_REQUEST_CODE = <span class="hljs-number">99</span>; <span class="hljs-meta">@Override</span> <span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onCreate</span><span class="hljs-params">(Bundle savedInstanceState)</span> </span>{ <span class="hljs-keyword">super</span>.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button scanCreditCard = (Button)findViewById(R.id.scan_credit_card); <span class="hljs-keyword">assert</span> scanCreditCard != <span class="hljs-keyword">null</span>; scanCreditCard.setOnClickListener(<span class="hljs-keyword">new</span> View.OnClickListener() { <span class="hljs-meta">@Override</span> <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onClick</span><span class="hljs-params">(View view)</span> </span>{ Intent scanIntent = <span class="hljs-keyword">new</span> Intent(MainActivity.<span class="hljs-keyword">this</span>, CardIOActivity.class); <span class="hljs-comment">// customize these values to suit your needs.</span> scanIntent.putExtra(CardIOActivity.EXTRA_REQUIRE_EXPIRY, <span class="hljs-keyword">true</span>); <span class="hljs-comment">// default: false</span> scanIntent.putExtra(CardIOActivity.EXTRA_REQUIRE_CVV, <span class="hljs-keyword">false</span>); <span class="hljs-comment">// default: false</span> scanIntent.putExtra(CardIOActivity.EXTRA_REQUIRE_POSTAL_CODE, <span class="hljs-keyword">false</span>); <span class="hljs-comment">// default: false</span> <span class="hljs-comment">// MY_SCAN_REQUEST_CODE is arbitrary and is only used within this activity.</span> startActivityForResult(scanIntent, MY_SCAN_REQUEST_CODE); } }); } <span class="hljs-meta">@Override</span> <span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onActivityResult</span><span class="hljs-params">(<span class="hljs-keyword">int</span> requestCode, <span class="hljs-keyword">int</span> resultCode, Intent data)</span> </span>{ <span class="hljs-keyword">super</span>.onActivityResult(requestCode, resultCode, data); <span class="hljs-keyword">if</span> (requestCode == MY_SCAN_REQUEST_CODE) { String resultDisplayStr; <span class="hljs-keyword">if</span> (data != <span class="hljs-keyword">null</span> && data.hasExtra(CardIOActivity.EXTRA_SCAN_RESULT)) { CreditCard scanResult = data.getParcelableExtra(CardIOActivity.EXTRA_SCAN_RESULT); <span class="hljs-comment">// Never log a raw card number. Avoid displaying it, but if necessary use getFormattedCardNumber()</span> resultDisplayStr = <span class="hljs-string">"Card Number: "</span> + scanResult.getRedactedCardNumber() + <span class="hljs-string">"\n"</span>; Log.d(TAG, <span class="hljs-string">"Card Number "</span> + resultDisplayStr); <span class="hljs-comment">// Do something with the raw number, e.g.:</span> <span class="hljs-comment">// myService.setCardNumber( scanResult.cardNumber );</span> <span class="hljs-keyword">if</span> (scanResult.isExpiryValid()) { resultDisplayStr += <span class="hljs-string">"Expiration Date: "</span> + scanResult.expiryMonth + <span class="hljs-string">"/"</span> + scanResult.expiryYear + <span class="hljs-string">"\n"</span>; } <span class="hljs-keyword">if</span> (scanResult.cvv != <span class="hljs-keyword">null</span>) { <span class="hljs-comment">// Never log or display a CVV</span> resultDisplayStr += <span class="hljs-string">"CVV has "</span> + scanResult.cvv.length() + <span class="hljs-string">" digits.\n"</span>; } <span class="hljs-keyword">if</span> (scanResult.postalCode != <span class="hljs-keyword">null</span>) { resultDisplayStr += <span class="hljs-string">"Postal Code: "</span> + scanResult.postalCode + <span class="hljs-string">"\n"</span>; } } <span class="hljs-keyword">else</span> { resultDisplayStr = <span class="hljs-string">"Scan was canceled."</span>; } <span class="hljs-comment">// do something with resultDisplayStr, maybe display it in a textView</span> <span class="hljs-comment">// resultTextView.setText(resultDisplayStr);</span> } <span class="hljs-comment">// else handle other activity results</span> } }

[/code]

Comments As Facebook

comments

i Am a web developer, SEO expert, Online Mentor & marketer working for last 4 years on the internet and managing several successful websites. You can contact me via Email ,Twitter on Facebook.

Pin It
error: Content is protected !!