Open In App
Related Articles

ArrayAdapter in Android with Example

Improve Article
Save Article
Like Article

The Adapter acts as a bridge between the UI Component and the Data Source. It converts data from the data sources into view items that can be displayed into the UI Component. Data Source can be Arrays, HashMap, Database, etc. and UI Components can be ListView, GridView, Spinner, etc. ArrayAdapter is the most commonly used adapter in android. When you have a list of single type items which are stored in an array you can use ArrayAdapter. Likewise, if you have a list of phone numbers, names, or cities. ArrayAdapter has a layout with a single TextView. If you want to have a more complex layout instead of ArrayAdapter use CustomArrayAdapter. The basic syntax for ArrayAdapter is given as:

public ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)




context The current context. This value can not be null.
resource The resource ID for the layout file containing a layout to use when instantiating views.
textViewResourceId The id of the TextView within the layout resource to be populated.
objects The objects to represent in the ListView. This value cannot be null.

context: It is used to pass the reference of the current class. Here ‘this’ keyword is used to pass the current class reference. Instead of ‘this’ we could also use the getApplicationContext() method which is used for the Activity and the getApplication() method which is used for Fragments.

public ArrayAdapter(this, int resource, int textViewResourceId, T[] objects)

resource: It is used to set the layout file(.xml files) for the list items.

public ArrayAdapter(this, R.layout.itemListView, int textViewResourceId, T[] objects)

textViewResourceId: It is used to set the TextView where you want to display the text data.

public ArrayAdapter(this, R.layout.itemListView,, T[] objects)

objects: These are the array object which is used to set the array element into the TextView.

String courseList[] = {“C-Programming”, “Data Structure”, “Database”, “Python”,

                      “Java”, “Operating System”,”Compiler Design”, “Android Development”};

ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.itemListView,, courseList[]);


In this example, the list of courses is displayed using a simple array adapter. 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: Working with the activity_main.xml file

Go to the layout folder and in activity_main.xml file change the ConstraintLayout to RelativeLayout and insert a ListView with id simpleListView. Below is the code for the activity_main.xml file.


<?xml version="1.0" encoding="utf-8"?>
        android:layout_height="wrap_content" />

Step 3: Create a new layout file

Go to app > res > layout > right-click > New > Layout Resource File and create a new layout file and name this file as item_view.xml and make the root element as a LinearLayout. This will contain a TextView that is used to display the array objects as output.


<?xml version="1.0" encoding="utf-8"?>
        android:layout_gravity="center" />

Step 4: Working with the file

Now go to the java folder and in and provide the implementation to the ArrayAdapter. Below is the code for the file. 


import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
    ListView simpleListView;
    // array objects
    String courseList[] = {"C-Programming", "Data Structure", "Database", "Python",
                            "Java", "Operating System", "Compiler Design", "Android Development"};
    protected void onCreate(Bundle savedInstanceState) {
        simpleListView = (ListView) findViewById(;
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
                R.layout.item_view,, courseList);

Output: Run on Emulator


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 23 Oct, 2020
Like Article
Save Article
Similar Reads
Complete Tutorials