Skip to content
Related Articles

Related Articles

How to Get Bank Details from IFSC Code in Android?
  • Last Updated : 15 Jan, 2021

Many apps such as the E-commerce app requires to accept payments from their users for providing different products or services or for their users. So this apps requires the users to enter bank details for payments. In this payment gateway, users are asked to add their banks IFSC code to get the details of their banks. So many apps have features inside their app that while entering the bank IFSC code the user’s bank details such as Bank address, bank city, and other common details are fetched from that IFSC code. So in this article, we will take a look at How we can get the common bank details from the IFSC code in Android. 

What we are going to build in this article? 

We will be building a simple application in which we will be getting IFSC code from the user via an EditText and after that, the user has to click on a simple button to get the data from that IFSC code such as Bank address, bank MICR code, contact number, and other details. For performing this task we will be using a simple API which we will be added to our application. This application will provide us the basic data from API which is related to the bank. Below is the GIF image in which we will get to see what we are going to build in this article. Note that we are going to implement this project using the Java language. 

 Get Bank Details from IFSC Code in Android Sample GIF

Step by Step Implementation

Step 1: Create a new project in Android Studio

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 the below dependency in your build.gradle file  

Below is the dependency for Volley which we will be using to get the data from API. For adding this dependency navigate to the app > Gradle Scripts > build.gradle(app) and add the below dependency in the dependencies section.    

implementation ‘com.android.volley:volley:1.1.1’

After adding this dependency sync your project and now move toward the XML part.  

Step 3: Working with the activity_main.xml file

Go to the activity_main.xml file and refer to the following code. Below is the code for the activity_main.xml file.

XML




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
  
    <!--heading text view-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="IFSC Code Validator"
        android:textAlignment="center"
        android:textColor="@color/purple_500"
        android:textSize="30sp" />
      
    <!-- edit text for entering our IFSC code
         we are specifying input type as number 
         and we are also mentioning our input type
         as textcapCharacters because IFSC code is
         having all capital characters-->
    <EditText
        android:id="@+id/idedtIfscCode"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:hint="Enter IFSC code"
        android:importantForAutofill="no"
        android:inputType="textCapCharacters"
        android:maxLines="1"
        android:singleLine="true"
        android:textAllCaps="true" />
      
    <!--button to get the data from IFSC code-->
    <Button
        android:id="@+id/idBtnGetBankDetails"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="50dp"
        android:text="Get Bank Details"
        android:textAllCaps="false" />
      
    <!--text view to display the 
        data received from IFSC code-->
    <TextView
        android:id="@+id/idTVBankDetails"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:padding="10dp"
        android:textAlignment="center"
        android:textAllCaps="false"
        android:textColor="@color/purple_500"
        android:textSize="15sp" />
      
</LinearLayout>

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.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
  
import androidx.appcompat.app.AppCompatActivity;
  
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
  
import org.json.JSONException;
import org.json.JSONObject;
  
public class MainActivity extends AppCompatActivity {
  
    // creating variables for edit text 
    // and our text views.
    private EditText ifscCodeEdt;
    private TextView bankDetailsTV;
      
    // creating a variable for 
    // our ifsc code string.
    String ifscCode;
      
    // creating a variable for request queue.
    private RequestQueue mRequestQueue;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          
        // initializing our variables.
        ifscCodeEdt = findViewById(R.id.idedtIfscCode);
        Button getBankDetailsBtn = findViewById(R.id.idBtnGetBankDetails);
        bankDetailsTV = findViewById(R.id.idTVBankDetails);
          
        // initializing our request que variable with request queue 
        // and passing our context to it.
        mRequestQueue = Volley.newRequestQueue(MainActivity.this);
  
        // initialing on click listener for our button.
        getBankDetailsBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // getting string  from edittext.
                ifscCode = ifscCodeEdt.getText().toString();
                  
                // validating if the edit text
                // is empty or not.
                if (TextUtils.isEmpty(ifscCode)) {
                    // displaying a toast message if the text field is empty
                    Toast.makeText(MainActivity.this, "Please enter valid IFSC code", Toast.LENGTH_SHORT).show();
                } else {
                    // calling a method to display 
                    // our ifsc code details.
                    getDataFromIFSCCode(ifscCode);
                }
            }
        });
    }
  
    private void getDataFromIFSCCode(String ifscCode) {
  
        // clearing our cache of request queue.
        mRequestQueue.getCache().clear();
          
        // below is the url from where we will be getting 
        // our response in the json format.
        String url = "http://api.techm.co.in/api/v1/ifsc/" + ifscCode;
          
        // below line is use to initialize our request queue.
        RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
  
        // creating a json object request for our API.
        JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                // this method is used to get 
                // the response from the API.
                try {
                    if (response.getString("status").equals("failed")) {
                        // checking if the response is not loaded and
                        // status for the repose is fail.
                        // if response status is failure we are displaying 
                        // an invalid IFSC code in our text view.
                        bankDetailsTV.setText("Invalid IFSC Code");
                    } else {
                        // if the status is successful we are
                        // extracting data from JSON file
                        JSONObject dataObj = response.getJSONObject("data");
                        String state = dataObj.optString("STATE");
                        String bankName = dataObj.optString("BANK");
                        String branch = dataObj.optString("BRANCH");
                        String address = dataObj.optString("ADDRESS");
                        String contact = dataObj.optString("CONTACT");
                        String micrcode = dataObj.optString("MICRCODE");
                        String city = dataObj.optString("CITY");
  
                        // after extracting this data we are displaying
                        // that data in our text view.
                        bankDetailsTV.setText("Bank Name : " + bankName + "\nBranch : " + branch + "\nAddress : " + address + "\nMICR Code : " + micrcode + "\nCity : " + city + "\nState : " + state + "\nContact : " + contact);
                    }
                } catch (JSONException e) {
                    // if we get any error while loading data 
                    // we are setting our text as invalid IFSC code.
                    e.printStackTrace();
                    bankDetailsTV.setText("Invalid IFSC Code");
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                // if we get any error while loading json
                // data we are setting our text to invalid IFSC code.
                bankDetailsTV.setText("Invalid IFSC Code");
            }
        });
        // below line is use for adding object 
        // request to our request queue.
        queue.add(objectRequest);
    }
}

Step 5: Add permission for the internet in the Manifest file 

Navigate to the app > AndroidManifest.xml file and add the below permissions to it. 

XML




<uses-permission android:name="android.permission.INTERNET"/>

After adding this permission. Run your project and see the output on the below screen.  

Output:

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!
My Personal Notes arrow_drop_up
Recommended Articles
Page :