{"id":416,"date":"2015-12-30T12:59:00","date_gmt":"2015-12-30T12:59:00","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?page_id=416"},"modified":"2018-06-05T10:03:18","modified_gmt":"2018-06-05T10:03:18","slug":"listview","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/ui\/listview","title":{"rendered":"ListView Tutorial With Example In Android Studio"},"content":{"rendered":"<p>List of scrollable items can be displayed in Android using ListView. It helps you to displaying the data in the form of a scrollable list. Users can then select any list item by clicking on it. ListView is default scrollable so we do not need to use scroll View or anything else with ListView.<\/p>\n<p>ListView is widely used in android applications. A very common example of ListView is your phone contact book, where you have a list of your contacts displayed in a ListView and if you click on it then user information is displayed.<\/p>\n<p><span style=\"color: #008000;\"><strong>Adapter:<\/strong><\/span> To fill the data in a ListView we simply use adapters. List items are automatically inserted to a list using an Adapter that pulls the content from a source such as an arraylist, array or database.<\/p>\n<p><span style=\"color: #008000;\"><strong>ListView in Android Studio:<\/strong><\/span> Listview is present inside Containers. From there you can drag and drop on virtual mobile screen to create it. Alternatively you can also XML code to create it.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-421\" src=\"\/ui\/wp-content\/uploads\/2015\/12\/ListView-in-Android-Studio.jpg\" alt=\"ListView in Android Studio\" width=\"504\" height=\"145\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/ListView-in-Android-Studio.jpg 504w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/ListView-in-Android-Studio-300x86.jpg 300w\" sizes=\"auto, (max-width: 504px) 100vw, 504px\" \/><\/center><strong>Here is Android ListView XML Code: <\/strong><\/p>\n<pre>&lt;ListView xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\n    android:id=\"@+id\/simpleListView\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"wrap_content\"\r\n    tools:context=\"abhiandroid.com.listexample.MainActivity\"&gt;\r\n&lt;\/ListView&gt;<\/pre>\n<p><strong>Listview look in Design:<\/strong><\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-420\" src=\"\/ui\/wp-content\/uploads\/2015\/12\/Listview-design.jpg\" alt=\"Listview design\" width=\"259\" height=\"447\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/Listview-design.jpg 259w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/Listview-design-174x300.jpg 174w\" sizes=\"auto, (max-width: 259px) 100vw, 259px\" \/><\/center><\/p>\n<hr \/>\n<h4><strong>Attributes of ListView:<\/strong><\/h4>\n<p>Lets see some different attributes\u00a0of ListView which will be used while designing a custom list:<\/p>\n<p><span style=\"color: #008000;\"><strong>1. id:<\/strong><\/span> id is used to uniquely identify a ListView.<\/p>\n<p>Below is the id attribute\u2019s example code with explanation included.<\/p>\n<pre>&lt;!-- Id of a list view uniquely identify it--&gt;\r\n&lt;ListView\r\nandroid:id=\"@+id\/simpleListView\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\n\/&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>2. divider: <\/strong><\/span>This is a drawable or color to draw between different list items.<\/p>\n<p>Below is the divider example code with explanation included, where we draw red color divider between different views.<\/p>\n<pre>&lt;!--Divider code in ListView--&gt;\r\n&lt;ListView\r\nandroid:id=\"@+id\/simpleListView\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:divider=\"#f00\"\r\nandroid:dividerHeight=\"1dp\"\r\n\/&gt;\r\n<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-429\" src=\"\/ui\/wp-content\/uploads\/2015\/12\/divider-in-list-view-example.jpg\" alt=\"divider in list view example\" width=\"214\" height=\"349\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/divider-in-list-view-example.jpg 214w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/divider-in-list-view-example-184x300.jpg 184w\" sizes=\"auto, (max-width: 214px) 100vw, 214px\" \/><\/center><span style=\"color: #008000;\"><strong>3. dividerHeight:<\/strong><\/span> This specify the height of the divider between list items. This could be in dp(density pixel),sp(scale independent pixel) or px(pixel).<\/p>\n<p>In above example of divider we also set the divider height 1dp between the list items. The height should be in dp,sp or px.<\/p>\n<p><span style=\"color: #008000;\"><strong>4. listSelector: <\/strong><\/span>listSelector property is used to set the selector of the listView. It is generally orange or Sky blue color mostly but you can also define your custom color or an image as a list selector as per your design.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-431\" src=\"\/ui\/wp-content\/uploads\/2015\/12\/listSelector-in-Listview-Android-Example.jpg\" alt=\"listSelector in Listview Android Example\" width=\"218\" height=\"253\" \/><\/center>Below is listSelector example code with explanation includes, where list selector color is green, when you select any list item then that item\u2019s background color is green .<\/p>\n<pre><strong>&lt;!-- List Selector Code in ListView --&gt;<\/strong>\r\n&lt;ListView\r\nandroid:id=\"@+id\/simpleListView\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:divider=\"#f00\"\r\nandroid:dividerHeight=\"1dp\" \r\nandroid:listSelector=\"#0f0\"\/&gt; &lt;!--list selector in green color--&gt;\r\n<\/pre>\n<hr \/>\n<h4><strong>Adapters Use in ListView:<\/strong><\/h4>\n<p>An adapter is a bridge between UI component and data source that helps us to fill data in UI component. It holds the data and send the data to adapter view then view can takes the data from the adapter view and shows the data on different views like as list view, grid view, spinner etc.<\/p>\n<p>ListView is a subclass of\u00a0AdapterView\u00a0and it can be populated by binding\u00a0 to an\u00a0Adapter, which retrieves the data from an external source and creates a View that represents each data entry.<\/p>\n<p><strong>In android commonly used adapters are:<\/strong><\/p>\n<ol>\n<li>Array Adapter<\/li>\n<li>Base Adapter<\/li>\n<\/ol>\n<p>Now we explain these two adapter in detail:<\/p>\n<p><span style=\"color: #008000;\"><strong>1.Array Adapter:<\/strong><\/span><\/p>\n<p>Whenever you have a list of single items which is backed by an array, you can use ArrayAdapter. For instance, list of phone contacts, countries or names.<\/p>\n<p><strong><span style=\"color: #ff0000;\">Important Note:<\/span><\/strong> By default, ArrayAdapter expects a Layout with a single TextView, If you want to use more complex views means more customization in list items, please avoid ArrayAdapter and use custom adapters.<\/p>\n<p>Below is Array Adapter code:<\/p>\n<pre>ArrayAdapter adapter = new ArrayAdapter&lt;String&gt;(this,R.layout.ListView,R.id.textView,StringArray);\r\n<\/pre>\n<p><span style=\"text-decoration: underline;\"><span style=\"color: #008000; text-decoration: underline;\"><strong>Example of list view using Array Adapter<\/strong>:<\/span><\/span><\/p>\n<p>In this example, we display a list of countries by using simple array adapter. Below is the final output we will create:<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-425\" src=\"\/ui\/wp-content\/uploads\/2015\/12\/Listview-ArrayAdapter-Example.jpg\" alt=\"Listview ArrayAdapter Example\" width=\"350\" height=\"672\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/Listview-ArrayAdapter-Example.jpg 350w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/Listview-ArrayAdapter-Example-156x300.jpg 156w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><\/center><span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span> Create a new project Listexample and activity Main Activity. Here we will create a ListView in LinearLayout. Below is the code of activity_main.xml or content_main.xml:<strong><br \/>\n<\/strong><\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"match_parent\"\r\nandroid:orientation=\"vertical\"&gt;\r\n\r\n&lt;ListView\r\nandroid:id=\"@+id\/simpleListView\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:divider=\"@color\/material_blue_grey_800\"\r\nandroid:dividerHeight=\"1dp\" \/&gt;\r\n&lt;\/LinearLayout&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span> Create a new activity name Listview and below is the code of activity_listview.xml<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"match_parent\"\r\nandroid:orientation=\"vertical\"&gt;\r\n\r\n&lt;TextView\r\nandroid:id=\"@+id\/textView\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_gravity=\"center\"\r\nandroid:padding=\"@dimen\/activity_horizontal_margin\"\r\nandroid:textColor=\"@color\/black\" \/&gt;\r\n&lt;\/LinearLayout&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3:<\/strong><\/span> Now in this final step we will use ArrayAdapter to display the country names in UI. <strong>Below is the code of MainActivity.java<\/strong><\/p>\n<pre>package abhiandroid.com.listexample;\r\n\r\nimport android.os.Bundle;\r\nimport android.app.Activity;\r\nimport android.view.Menu;\r\nimport android.widget.ArrayAdapter;import android.widget.ListView;\r\n\r\npublic class MainActivity extends Activity\r\n{\r\n    \/\/ Array of strings...\r\n    ListView simpleList;\r\n    String countryList[] = {\"India\", \"China\", \"australia\", \"Portugle\", \"America\", \"NewZealand\"};\r\n\r\n    @Override   protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);      setContentView(R.layout.activity_main);\r\n        simpleList = (ListView)findViewById(R.id.simpleListView);\r\n        ArrayAdapter&lt;String&gt; arrayAdapter = new ArrayAdapter&lt;String&gt;(this, R.layout.activity_listview, R.id.textView, countryList);\r\n        simpleList.setAdapter(arrayAdapter);\r\n    }\r\n}<\/pre>\n<p><span style=\"text-decoration: underline;\"><strong>Output Screen:<\/strong><\/span><\/p>\n<p>Now run the App in Emulator. You will see the below output screen where list of country names will be printed:<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-424\" src=\"\/ui\/wp-content\/uploads\/2015\/12\/Listview-ArrayAdapter-Example-Output.jpg\" alt=\"Listview ArrayAdapter Example Output\" width=\"351\" height=\"410\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/Listview-ArrayAdapter-Example-Output.jpg 351w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/Listview-ArrayAdapter-Example-Output-257x300.jpg 257w\" sizes=\"auto, (max-width: 351px) 100vw, 351px\" \/><\/center><span style=\"color: #008000;\"><strong>2.Base Adapter:<\/strong><\/span><\/p>\n<p>BaseAdapter is a common base class of a general implementation of an Adapter that can be used in ListView. Whenever you need a customized list you create your own adapter and extend base adapter in that. Base Adapter can be extended to create a custom Adapter for displaying a custom list item. \u00a0ArrayAdapter is also an implementation of BaseAdapter.<\/p>\n<p><strong>\u00a0<\/strong><strong>Example of list view using Custom adapter(Base adapter):<\/strong><\/p>\n<p>In this example we display a list of countries with flags. For this, we have to use custom adapter as shown in example:<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-428\" src=\"\/ui\/wp-content\/uploads\/2015\/12\/List-view-base-custom-adapter-example-output-245x300.jpg\" alt=\"List view base custom adapter example output\" width=\"245\" height=\"300\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/List-view-base-custom-adapter-example-output-245x300.jpg 245w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/List-view-base-custom-adapter-example-output.jpg 371w\" sizes=\"auto, (max-width: 245px) 100vw, 245px\" \/><\/center><span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span> Create a new project Listebasexample and activity Main Activity. Here we will create a ListView in LinearLayout. <strong>Below is the code of activity_main.xml or content_main.xml:<br \/>\n<\/strong><\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"<em>?&gt;\r\n<\/em>&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"match_parent\"\r\nandroid:orientation=\"vertical\"&gt;\r\n\r\n&lt;ListView\r\nandroid:id=\"@+id\/simpleListView\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:divider=\"@color\/material_blue_grey_800\"\r\nandroid:dividerHeight=\"1dp\"\r\nandroid:footerDividersEnabled=\"false\" \/&gt;\r\n&lt;\/LinearLayout&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span> Create a new activity name Listview and below is the code of activity_listview.xml<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"match_parent\"\r\nandroid:orientation=\"horizontal\"&gt;\r\n\r\n&lt;ImageView\r\nandroid:id=\"@+id\/icon\"\r\nandroid:layout_width=\"50dp\"\r\nandroid:layout_height=\"50dp\"\r\nandroid:src=\"@drawable\/ic_launcher\" \/&gt;\r\n\r\n&lt;TextView\r\nandroid:id=\"@+id\/textView\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_gravity=\"center\"\r\nandroid:padding=\"@dimen\/activity_horizontal_margin\"\r\nandroid:textColor=\"@color\/black\" \/&gt;\r\n&lt;\/LinearLayout&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3:<\/strong><\/span> In third step we will use custom adapter to display the country names in UI by coding MainActivity.java. <strong>Below is the code of MainActivity.java<\/strong><\/p>\n<p><span style=\"color: #ff0000;\"><strong>Important Note:<\/strong><\/span> Make sure flag images are stored in drawable folder present inside res folder with correct naming.<\/p>\n<pre>package com.abhiandroid.listbaseexample;\r\n\r\nimport android.app.Activity;\r\nimport android.os.Bundle;\r\nimport android.widget.ListView;\r\n\r\npublic class MainActivity extends Activity {\r\n\r\nListView simpleList;\r\nString countryList[] = {\"India\", \"China\", \"australia\", \"Portugle\", \"America\", \"NewZealand\"};\r\nint flags[] = {R.drawable.india, R.drawable.china, R.drawable.australia, R.drawable.portugle, R.drawable.america, R.drawable.new_zealand};\r\n\r\n@Override\r\nprotected void onCreate(Bundle savedInstanceState) {\r\nsuper.onCreate(savedInstanceState);\r\nsetContentView(R.layout.activity_main);\r\nsimpleList = (ListView) findViewById(R.id.simpleListView);\r\nCustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), countryList, flags);\r\nsimpleList.setAdapter(customAdapter);\r\n}\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 4: <\/strong><\/span>Now create another class Custom Adapter which will extend BaseAdapter. Below is the code of CustomAdapter.java<\/p>\n<pre>package com.abhiandroid.listbaseexample;\r\n\r\nimport android.content.Context;\r\nimport android.media.Image;\r\nimport android.view.LayoutInflater;\r\nimport android.view.View;\r\nimport android.view.ViewGroup;\r\nimport android.widget.BaseAdapter;\r\nimport android.widget.ImageView;\r\nimport android.widget.TextView;\r\n\r\nimport java.util.zip.Inflater;\r\n\r\npublic class CustomAdapter extends BaseAdapter {\r\nContext context;\r\nString countryList[];\r\nint flags[];\r\nLayoutInflater inflter;\r\n\r\npublic CustomAdapter(Context applicationContext, String[] countryList, int[] flags) {\r\nthis.context = context;\r\nthis.countryList = countryList;\r\nthis.flags = flags;\r\ninflter = (LayoutInflater.<em>from<\/em>(applicationContext));\r\n}\r\n\r\n@Override\r\npublic int getCount() {\r\nreturn countryList.length;\r\n}\r\n\r\n@Override\r\npublic Object getItem(int i) {\r\nreturn null;\r\n}\r\n\r\n@Override\r\npublic long getItemId(int i) {\r\nreturn 0;\r\n}\r\n\r\n@Override\r\npublic View getView(int i, View view, ViewGroup viewGroup) {\r\nview = inflter.inflate(R.layout.<em>activity_listview<\/em>, null);\r\nTextView country = (TextView) \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0view.findViewById(R.id.<em>textView<\/em>);\r\nImageView icon = (ImageView) view.findViewById(R.id.<em>icon<\/em>);\r\ncountry.setText(countryList[i]);\r\nicon.setImageResource(flags[i]);\r\nreturn view;\r\n}\r\n<\/pre>\n<p><span style=\"text-decoration: underline;\"><strong>Output:<\/strong><\/span><\/p>\n<p>Now run the App in Emulator and it will show you name of countries along with flags. Below is the output screen:<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-428\" src=\"\/ui\/wp-content\/uploads\/2015\/12\/List-view-base-custom-adapter-example-output.jpg\" alt=\"List view base custom adapter example output\" width=\"371\" height=\"455\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/List-view-base-custom-adapter-example-output.jpg 371w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/List-view-base-custom-adapter-example-output-245x300.jpg 245w\" sizes=\"auto, (max-width: 371px) 100vw, 371px\" \/><\/center><\/p>\n","protected":false},"excerpt":{"rendered":"<p>List of scrollable items can be displayed in Android using ListView. It helps you to displaying the data in the form of a scrollable list. Users can then select any list item by clicking on it. ListView is default scrollable so we do not need to use scroll View or anything else with ListView. ListView &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/listview\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">ListView Tutorial With Example In Android Studio<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"_acf_changed":false,"footnotes":""},"class_list":["post-416","page","type-page","status-publish","hentry"],"acf":[],"psp_head":"<title>ListView Tutorial With Example In Android Studio \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Tutorial on list view, adapters and attributes with example, images and code in Android Studio. Also find details about Array Adapter and Base Adapter\/Custom Adapter.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/listview\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/416","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/comments?post=416"}],"version-history":[{"count":2,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/416\/revisions"}],"predecessor-version":[{"id":2639,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/416\/revisions\/2639"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}