ListView in Android Example
List View is probably one of the most, if not the most, used tools used in Android development. Therefore, learning about it is highly important for future Android developers. In addition to that, List View in Android is among the most self explanatory tools by Android developers. As a result, beginners usually do not face much difficulty while learning about List View in Android.
That being said, a few confusions have known to be arisen in the minds of the newcomers in recent times when it comes to List View in Android. In this article, everything important about Android List View that a beginner needs to know will be addressed, starting from the basics to creating a List View in Android from zero.
List View in Android
Those who have put even a little thought probably must have guessed by now what Android List View is. For those who are still struggling, an Android List View is used by Android developers to create a vertical list for a group of items or options. Apart from containing a number of items, the list can also be scrolled up and down.
The convenience of being able to scroll the list is present in the default state of List View. For this reason, Android app developers do not have to make the list scrollable with the help of other View tools, such as Scroll View.
Basically, Android app developers use the List View option when they have more than one item falling in the same category. The simplest and the most common example of List View can be seen on your own phone contacts page. This page consists of your phone contacts arranged in a List View, and selecting one of these contacts displays details about the contact.
List View vs Recycler View
Being such a fundamental tool, List View is widely used in Android applications. As a matter of fact, it would be an extremely difficult task to find an app that does not make use of List View. That being said, the usage of List View has significantly reduced with the arrival of Recycler View, which was introduced with Android 5.0, i.e. Lollipop. To avoid confusion, Recycler View is nothing but a modernized version of List View. Although Recycler View offers more options and features, it is a bit more complex to use.
Important Methods for Creating an Android List View
For an Android developer, it is not possible to create a proper and up-to-the-mark Android ListView without knowing the essentials of the code that is used to develop it. Before creating an Android ListView with many features, you must learn the following important attributes and methods and understand the purpose of each.
android:divider The android:divider attribute is a color or drawable that is used to draw between items of a list.
android:dividerHeight The android:dividerHeight is used to set a specific height for the divider. This height can be in dp, sp, in, mm or px.
getAdapter( ) The getAdapter( ) method is used to return the adapter that is currently in use in the ListView.
addHeaderView( ) The addHeaderView( ) method is used to add a fixed header view at the top of the list.
getDivider( ) The getDivider( ) method is used to return the drawable that is to be drawn between each item of the list.
getDividerHeight( ) The getDividerHeight( ) is used to return divider height that is to be drawn between each item of the list.
isOpaque( ) The isOpaque( ) method shows if the list View is opaque or not.
removeFooterView( View view) The removeFooterView( View view) method is used to remove previously added footer view from the list.
removeHeaderView( View view) The removeHeaderView( View view) method is used to remove previously added header view from the list.
Adapter and Different Types of Adapter
An adapter is a pathway between user interface (UI) element and data source. In ListView, Adapter classes are used to add the required data in the list. An Adapter pulls the data from a data source, such as array and database, and transfers the data to the list itself.
Basically, an Adapter sends the list data to AdapterView, from which View takes the data and shows it on different views, such as List View, Spinner and Grid View. In actuality, AdapterView has many subclasses, and one of those is ListView. A ListView can be filled if it is bound to an Adapter, which collects data from a foreign data source and develops a View that displays all the data.
In Android, there are four main types of Adapter
ArrayAdapter: It is used when our data source is an array or list. For example, a list of names, countries, contacts, etc. As a default, ArrayAdapter develops a separate view for each item of the list and places them in a single TextView. It is noteworthy that you can store your list items on the strings.xml file as well.
CursorAdaptor: It is used when the source of data is a cursor.
SimpleAdapter: It is mainly used to accept a static data that is defined in resources, such as database or array.
BaseAdapter: It is the parent adapter of the rest of the types of adapter. Being a generic implementation for all the above-mentioned adapter types, BaseAdapter is used in the views according to our requirements.
ListView in Android Steps
Now that you have learned all the basics of ListView, let us show you how to create a ListView from scratch.
First of all, open Android Studio IDE (Integrated development Environment) to create a new project just like how we showed you in our earlier tutorials. We have named our project “Listview,” but you may choose your own if you want. In the same way, we have set “com.androidaura.listview” as the Package Name of our project; however, you can select a suitable Package Name for your project yourself.
After this, open your MainActivity.java class file so that you can add the following code in it:
Before moving further, let us be clear about what this code means and what it does. The abovementioned code determines what data our ListView will display, what format it will use to do so and so on. In order to achieve that, we need to use a set of data and a View, into which the Adapter will convert the data. Therefore, as you can see in the file above, we have chosen a specific dataset (planet names) to be used in the project and added it in this file.
When you are done coding in your java class, you have to open your activity_main.xml layout file in order to add the following code in it:
In the code mentioned above, it is clear that we have defined the characteristics of our ListView, such as its width and height. Other than that, we have chosen a LinearLayout for our project with a vertical orientation and fixed a specific height and width for it. However, all this would be useless without our code in MainActivity.java class file, which contains the most important instructions. Now, create a new XML layout file, name it “activity_list.xml” and add the following code in it to fulfill the remaining requirements:
Because the set of data in our List View is just plain text, we would only need a regular TextView widget to display it in our ListView. Let us suppose, if our ListView data had images along with some text, then we would need to use an ImageView widget partnered with a TextView widget to display both the text and the images in the ListView.
Since we have used a TextView widget, we needed to define the characteristics of the text as well. Hence, we have used this code to set different parameters of the text, such as text size, etc.
When you have successfully entered these codes in all three files of your project, your Android ListView output will look like this:
All in all, creating a ListView is arguably one of the easiest things to do on Android Studio IDE. Although this is just the beginning, we are sure that you will be able to create your own Android application in no time if you continue to learn making new things through our tutorials.