List View
Displays a vertically-scrollable collection of views, where each view is positioned immediately below the previous view in the list.
To display a list, you can include a list view in your layout XML file:
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
A list view is an adapter view that does not know the details, such as type and contents, of the views it contains. Instead, list view requests views on demand from a ListAdapter
as needed, such as to display new views as the user scrolls up or down.
In order to display items in the list, call setAdapter(android.widget.ListAdapter)
to associate an adapter with the list. For a simple example, see the discussion of filling an adapter view with text in the Layouts guide.
To display a more custom view for each item in your dataset, implement a ListAdapter. For example, extend BaseAdapter
and create and configure the view for each data item in getView(...)
:
private class MyAdapter extends BaseAdapter {
// override other abstract methods here
@Override
public View getView(int position, View convertView, ViewGroup container) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.list_item,
container, false);
}
((TextView) convertView.findViewById(android.R.id.text1))
.setText(getItem(position));
return convertView;
}
}
Example
Start by creating a new project, select empty activity as shown below:


in activity_main.xml remove the hello world text view, and add a listview, your layout XML file should look like this:
...
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Now we will treat every row in the text view as a separate layout, add a new layout and call it my_list.xml

Since this is a simple layout all we need is a TextView your my_list.xml should look like the following:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textColor="#4d4d4d"
android:layout_marginStart="10dp" />
We need some data to fill the list, one way of doing this is using the string resources XML file as following:
<resources>
<string name="app_name">SimpleList</string>
<string-array name="array_technology">
<item>Android</item>
<item>Java</item>
<item>Php</item>
<item>Hadoop</item>
<item>Sap</item>
<item>Python</item>
<item>Ajax</item>
<item>C++</item>
<item>Ruby</item>
<item>Rails</item>
<item>.Net</item>
<item>Perl</item>
</string-array>
</resources>
Now in the activity class, we need to inflate the list with data, first we define all the resources we need to use in our class:
ListView listView;
TextView textView;
String[] listItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listView);
textView = findViewById(R.id.textView);
listItem = getResources().getStringArray(R.array.array_technology);
}
Note how to get the array string list using:
getResources().getStringArray(R.array.array_technology);
array_technology is the name of the string-array as stated in the resource file.
it is always a good idea to testing logging the information to make sure that everything is working as it should:
private static final String TAG = "MainActivity";
ListView listView;
TextView textView;
String[] listItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listView);
textView = findViewById(R.id.textView);
listItem = getResources().getStringArray(R.array.array_technology);
for (String s : listItem) {
Log.d(TAG, s);
}
}
Building Layouts with an Adapter
When the content for your layout is dynamic or not pre-determined, you can use a layout that subclasses AdapterView to populate the layout with views at runtime. A subclass of the AdapterView class uses an Adapter to bind data to its layout. The Adapter behaves as a middleman between the data source and the AdapterView layout—the Adapter retrieves the data (from a source such as an array or a database query) and converts each entry into a view that can be added into the AdapterView layout.
Common layouts backed by an adapter include:
List View and Grid View


Filling an adapter view with data
You can populate an AdapterView such as ListView or GridView by binding the AdapterView instance to an Adapter, which retrieves data from an external source and creates a View that represents each data entry.
Android provides several subclasses of Adapter that are useful for retrieving different kinds of data and building views for an AdapterView. The two most common adapters are:
ArrayAdapter
Use this adapter when your data source is an array. By default, ArrayAdapter creates a view for each array item by calling toString() on each item and placing the contents in a TextView. For example, if you have an array of strings you want to display in a ListView, initialize a new ArrayAdapter using a constructor to specify the layout for each string and the string array:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.my_list,
listItem);
The arguments for this constructor are:
Then simply call setAdapter() on your ListView:
listView.setAdapter(adapter);

Handling click events
You can respond to click events on each item in an AdapterView by implementing the AdapterView.OnItemClickListener interface. For example:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String value=adapter.getItem(position);
Toast.makeText(getApplicationContext(),value,Toast.LENGTH_SHORT).show();
}
});
On Item Click Method
Callback method to be invoked when an item in this AdapterView has been clicked.
Implementers can call getItemAtPosition(position) if they need to access the data associated with the selected item.
public abstract void onItemClick (AdapterView<?> parent,
View view,
int position,
long id)

Last updated
Was this helpful?