{"id":610,"date":"2016-01-15T08:18:41","date_gmt":"2016-01-15T08:18:41","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?p=610"},"modified":"2019-06-12T13:19:10","modified_gmt":"2019-06-12T13:19:10","slug":"custom-arrayadapter-tutorial-example","status":"publish","type":"post","link":"https:\/\/abhiandroid.com\/ui\/custom-arrayadapter-tutorial-example.html","title":{"rendered":"Custom ArrayAdapter Tutorial With Example In Android Studio"},"content":{"rendered":"<p>ArrayAdapter is a type of Adapter which acts a bridge between UI component and data source that helps us to fill data in UI component. It expects a Layout with a single TextView and for more customization in grid items or list items, we use custom adapters.<\/p>\n<ul>\n<li>ArrayAdapter is also an implementation of BaseAdapter so if we want more customization then we create a custom adapter and extend ArrayAdapter in that.<\/li>\n<li>We override all the function\u2019s of BaseAdapter in our custom adapter to give more customization to ArrayAdapter.<\/li>\n<\/ul>\n<hr \/>\n<p><span style=\"color: #008000;\"><strong>Android Custom adapter code when we extends ArrayAdapter in that:<\/strong><\/span><\/p>\n<pre>public class MyAdapter extends ArrayAdapter {\r\n\r\npublic MyAdapter(Context context, int resource, int textViewResourceId, List objects) {\r\nsuper(context, resource, textViewResourceId, objects);\r\n}\r\n\r\n@Override\r\npublic int getCount() {\r\nreturn super.getCount();\r\n}\r\n\r\n@Override\r\npublic View getView(int position, View convertView, ViewGroup parent) {\r\nreturn super.getView(position, convertView, parent);\r\n}\r\n}\r\n<\/pre>\n<p>In the above code snippet we see the overrided functions of ArrayAdapter which are used to set the data in a list, grid or a spinner. From there we mainly used two functions getCount() and getView() .<\/p>\n<p><span style=\"color: #008000;\"><strong> 1. getCount():<\/strong><\/span><\/p>\n<p>The getCount() function returns the total number of items to be displayed in a list. It counts the value from arraylist size or an array\u2019s length. For example if we have an list of elements in a array list and we have to display the items in a list view then we can count the total number of elements using the size function and then that integer value is returned by the function getCount() as shown below.<\/p>\n<pre>@Override\r\npublic int getCount() {\r\n\r\nint count=arrayList.size(); \/\/counts the total number of elements from the arrayList.\r\nreturn count;\/\/returns the total count to adapter\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>2. getView(int i, View view, ViewGroup viewGroup):<\/strong><\/span><\/p>\n<p>This function is automatically called when the list item view is ready to be displayed or about to be displayed. In this function we set the layout for list items using LayoutInflater class and then add the data to the views like ImageView, TextView etc.<\/p>\n<p>Below is the getView function\u2019s example code with explanation included in which we set the layout using LayoutInflater and then get the view\u2019s id and implement them.<\/p>\n<pre>@Override\r\npublic View getView(int i, View view, ViewGroup viewGroup) {\r\nview = inflter.inflate(R.layout.<em>activity_list_view<\/em>, null);\/\/set layout for displaying items\r\nImageView icon = (ImageView) view.findViewById(R.id.<em>icon<\/em>);\/\/get id for image view\r\nicon.setImageResource(countryFlags[i]);\/\/set image of the item\u2019s\r\nreturn view;\r\n}\r\n<\/pre>\n<hr \/>\n<h4><strong>Example of Custom ArrayAdapter:<\/strong><\/h4>\n<p>Below is the example, in which we displays a list of animal names with images in a list view using simple array adapter. Below is the final output and code:<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"https:\/\/github.com\/abhisheksaini4\/CustomArrayAdapterExample\" target=\"_blank\" rel=\"nofollow\">Download Code<\/a><a class=\"help\" title=\"Learn How To Download Code And Import In Android Studio\" href=\"\/androidstudio\/download-code-abhiandroid\" target=\"_blank\" rel=\"nofollow\"> ? <\/a><\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-617\" src=\"\/ui\/wp-content\/uploads\/2016\/01\/Custom-ArrayAdapter-Example-in-Listview.jpg\" alt=\"Custom ArrayAdapter Example in Listview\" width=\"342\" height=\"421\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/01\/Custom-ArrayAdapter-Example-in-Listview.jpg 342w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/01\/Custom-ArrayAdapter-Example-in-Listview-244x300.jpg 244w\" sizes=\"auto, (max-width: 342px) 100vw, 342px\" \/><\/center><span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span> Create a new project and name it ArrayAdapterExample.<\/p>\n<pre>Select File -&gt; New -&gt; New Project -&gt; and Fill the forms and click \"Finish\" button\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span> Now open res -&gt; layout -&gt;\u00a0<strong>xml (or) activity_main.xml<\/strong>\u00a0and add following code:<\/p>\n<pre>&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    tools:context=\".MainActivity\"&gt;\r\n\r\n    &lt;ListView\r\n        android:id=\"@+id\/simpleListView\"\r\n        android:layout_width=\"match_parent\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:divider=\"#000\"\r\n        android:dividerHeight=\"2dp\"\/&gt;\r\n\r\n&lt;\/RelativeLayout&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3:<\/strong><\/span> Create a new layout in res-&gt; layout and name it list_view_items.xml. Here add the following code:<\/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\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:orientation=\"horizontal\"&gt;\r\n\r\n    &lt;ImageView\r\n        android:id=\"@+id\/imageView\"\r\n        android:layout_width=\"50dp\"\r\n        android:layout_height=\"50dp\"\r\n        android:padding=\"5dp\"\r\n        android:src=\"@drawable\/ic_launcher\" \/&gt;\r\n\r\n    &lt;TextView\r\n        android:id=\"@+id\/textView\"\r\n        android:layout_width=\"fill_parent\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_gravity=\"center\"\r\n        android:padding=\"@dimen\/activity_horizontal_margin\"\r\n        android:text=\"Demo\"\r\n        android:textColor=\"#000\" \/&gt;\r\n&lt;\/LinearLayout&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 4:<\/strong><\/span> Now open app &#8211; &gt; java -&gt; package -&gt;\u00a0MainActivity.java and add the below code.<\/p>\n<pre>package example.abhiandriod.customarrayadapterexample;\r\n\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.os.Bundle;\r\nimport android.widget.ArrayAdapter;\r\nimport android.widget.ListView;\r\n\r\nimport java.util.ArrayList;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n\r\n    ListView simpleList;\r\n    ArrayList&lt;Item&gt; animalList=new ArrayList&lt;&gt;();\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n        simpleList = (ListView) findViewById(R.id.simpleListView);\r\n        animalList.add(new Item(\"Lion\",R.drawable.lion));\r\n        animalList.add(new Item(\"Tiger\",R.drawable.tiger));\r\n        animalList.add(new Item(\"Monkey\",R.drawable.monkey));\r\n        animalList.add(new Item(\"Elephant\",R.drawable.elephant));\r\n        animalList.add(new Item(\"Dog\",R.drawable.dog));\r\n        animalList.add(new Item(\"Cat\",R.drawable.cat));\r\n\r\n        MyAdapter myAdapter=new MyAdapter(this,R.layout.list_view_items,animalList);\r\n        simpleList.setAdapter(myAdapter);\r\n       }\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 5:<\/strong><\/span> Create a new Class app -&gt; java -&gt;package-&gt;MyAdapter.java and add the below code. Here in Custom Adapter class we override the function\u2019s of BaseAdapter.<\/p>\n<pre>package example.abhiandriod.customarrayadapterexample;\r\n\r\nimport android.content.Context;\r\nimport android.view.LayoutInflater;\r\nimport android.view.View;\r\nimport android.view.ViewGroup;\r\nimport android.widget.ArrayAdapter;\r\nimport android.widget.ImageView;\r\nimport android.widget.TextView;\r\n\r\nimport java.util.ArrayList;\r\nimport java.util.HashMap;\r\n\r\npublic class MyAdapter extends ArrayAdapter&lt;Item&gt; {\r\n\r\n    ArrayList&lt;Item&gt; animalList = new ArrayList&lt;&gt;();\r\n\r\n    public MyAdapter(Context context, int textViewResourceId, ArrayList&lt;Item&gt; objects) {\r\n        super(context, textViewResourceId, objects);\r\n        animalList = objects;\r\n    }\r\n\r\n    @Override\r\n    public int getCount() {\r\n        return super.getCount();\r\n    }\r\n\r\n    @Override\r\n    public View getView(int position, View convertView, ViewGroup parent) {\r\n\r\n        View v = convertView;\r\n        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);\r\n        v = inflater.inflate(R.layout.list_view_items, null);\r\n        TextView textView = (TextView) v.findViewById(R.id.textView);\r\n        ImageView imageView = (ImageView) v.findViewById(R.id.imageView);\r\n        textView.setText(animalList.get(position).getAnimalName());\r\n        imageView.setImageResource(animalList.get(position).getAnimalImage());\r\n        return v;\r\n\r\n    }\r\n\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 6:<\/strong><\/span> Create a new Class src-&gt; package-&gt; Item.java and add the below code:<\/p>\n<pre>package example.abhiandriod.customarrayadapterexample;\r\n\r\n\r\npublic class Item {\r\n\r\n    String animalName;\r\n    int animalImage;\r\n\r\n    public Item(String animalName,int animalImage)\r\n    {\r\n        this.animalImage=animalImage;\r\n        this.animalName=animalName;\r\n    }\r\n    public String getAnimalName()\r\n    {\r\n        return animalName;\r\n    }\r\n    public int getAnimalImage()\r\n    {\r\n        return animalImage;\r\n    }\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong><br \/>\n<\/strong><strong>Output:<\/strong><\/span><\/p>\n<p>Now run the App in Emulator and you will see the animals name listed with images. For this we have used custom ArrayAdapter.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-617\" src=\"\/ui\/wp-content\/uploads\/2016\/01\/Custom-ArrayAdapter-Example-in-Listview-244x300.jpg\" alt=\"Custom ArrayAdapter Example in Listview\" width=\"244\" height=\"300\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/01\/Custom-ArrayAdapter-Example-in-Listview-244x300.jpg 244w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/01\/Custom-ArrayAdapter-Example-in-Listview.jpg 342w\" sizes=\"auto, (max-width: 244px) 100vw, 244px\" \/><\/center><\/p>\n<hr \/>\n<p><span style=\"color: #008000;\"><strong>Example 2: <\/strong><\/span>Below is the example in which we display the animal images in grid\u2019s of a grid view by using custom ArrayAdapter. Below is the final output and code:<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"https:\/\/github.com\/abhisheksaini4\/CustomArrayAdapterExample\" target=\"_blank\" rel=\"nofollow\">Download Code<\/a> <a class=\"help\" title=\"Learn How To Download Code And Import In Android Studio\" href=\"\/androidstudio\/download-code-abhiandroid\" target=\"_blank\" rel=\"nofollow\"> ? <\/a><\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-619\" src=\"\/ui\/wp-content\/uploads\/2016\/01\/Custom-ArrayAdapter-Example-in-Gridview.jpg\" alt=\"Custom ArrayAdapter Example in Gridview\" width=\"355\" height=\"615\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/01\/Custom-ArrayAdapter-Example-in-Gridview.jpg 355w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/01\/Custom-ArrayAdapter-Example-in-Gridview-173x300.jpg 173w\" sizes=\"auto, (max-width: 355px) 100vw, 355px\" \/><\/center><span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span> Create a new project and name it ArrayAdapterExample.<\/p>\n<pre>Select File -&gt; New -&gt; New Project. Fill the forms and click \"Finish\" button.<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span> Now open app -&gt; res -&gt; layout -&gt; activity_main.xml (or) main.xml\u00a0and add following code :<\/p>\n<pre>&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    tools:context=\".MainActivity\"&gt;\r\n\r\n    &lt;GridView\r\n        android:id=\"@+id\/simpleGridView\"\r\n        android:layout_width=\"match_parent\"\r\n        android:layout_height=\"wrap_content\"\r\n       android:numColumns=\"2\"\/&gt;\r\n\r\n&lt;\/RelativeLayout&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3:<\/strong><\/span> Create a new layout Activity in app -&gt; res-&gt; layout-&gt; new activity and name it grid_view_items.xml and add following code:<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    android:orientation=\"horizontal\"&gt;\r\n\r\n    &lt;ImageView\r\n        android:id=\"@+id\/imageView\"\r\n        android:layout_width=\"150dp\"\r\n        android:layout_height=\"150dp\"\r\n        android:layout_centerInParent=\"true\"\r\n        android:padding=\"5dp\"\r\n        android:scaleType=\"fitXY\"\r\n        android:src=\"@drawable\/ic_launcher\" \/&gt;\r\n\r\n    &lt;TextView\r\n        android:id=\"@+id\/textView\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_centerInParent=\"true\"\r\n        android:padding=\"@dimen\/activity_horizontal_margin\"\r\n        android:text=\"Demo\"\r\n        android:textColor=\"#000\" \/&gt;\r\n&lt;\/RelativeLayout&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 4:<\/strong><\/span> Now open app -&gt; java -&gt; package -&gt;\u00a0MainActivity.java<\/p>\n<pre>package example.abhiandriod.customarrayadapterexample;\r\n\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.os.Bundle;\r\nimport android.widget.GridView;\r\nimport java.util.ArrayList;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n\r\n    GridView simpleList;\r\n    ArrayList&lt;Item&gt; animalList=new ArrayList&lt;&gt;();\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n        simpleList = (GridView) findViewById(R.id.simpleGridView);\r\n        animalList.add(new Item(\"Lion\",R.drawable.lion));\r\n        animalList.add(new Item(\"Tiger\",R.drawable.tiger));\r\n        animalList.add(new Item(\"Monkey\",R.drawable.monkey));\r\n        animalList.add(new Item(\"Elephant\",R.drawable.elephant));\r\n        animalList.add(new Item(\"Dog\",R.drawable.dog));\r\n        animalList.add(new Item(\"Cat\",R.drawable.cat));\r\n\r\n        MyAdapter myAdapter=new MyAdapter(this,R.layout.grid_view_items,animalList);\r\n        simpleList.setAdapter(myAdapter);\r\n    }\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 5:<\/strong><\/span> Create a new Class src -&gt; package -&gt; MyAdapter.java and add the following code:<strong><br \/>\n<\/strong><\/p>\n<pre>package example.abhiandriod.customarrayadapterexample;\r\n\r\nimport android.content.Context;\r\nimport android.view.LayoutInflater;\r\nimport android.view.View;\r\nimport android.view.ViewGroup;\r\nimport android.widget.ArrayAdapter;\r\nimport android.widget.ImageView;\r\nimport android.widget.TextView;\r\n\r\nimport java.util.ArrayList;\r\n\r\npublic class MyAdapter extends ArrayAdapter&lt;Item&gt; {\r\n\r\n    ArrayList&lt;Item&gt; animalList = new ArrayList&lt;&gt;();\r\n\r\n    public MyAdapter(Context context, int textViewResourceId, ArrayList&lt;Item&gt; objects) {\r\n        super(context, textViewResourceId, objects);\r\n        animalList = objects;\r\n    }\r\n\r\n    @Override\r\n    public int getCount() {\r\n        return super.getCount();\r\n    }\r\n\r\n    @Override\r\n    public View getView(int position, View convertView, ViewGroup parent) {\r\n\r\n        View v = convertView;\r\n        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);\r\n        v = inflater.inflate(R.layout.grid_view_items, null);\r\n        TextView textView = (TextView) v.findViewById(R.id.textView);\r\n        ImageView imageView = (ImageView) v.findViewById(R.id.imageView);\r\n        textView.setText(animalList.get(position).getAnimalName());\r\n        imageView.setImageResource(animalList.get(position).getAnimalImage());\r\n        return v;\r\n\r\n    }\r\n\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 6:<\/strong><\/span> Create a new Class src -&gt; package -&gt; Item.java and add the below code:<strong><br \/>\n<\/strong><\/p>\n<pre>package example.abhiandriod.customarrayadapterexample;\r\n\r\n\/**\r\n * Created by Gourav on 10-01-2016.\r\n *\/\r\npublic class Item {\r\n\r\n    String animalName;\r\n    int animalImage;\r\n\r\n    public Item(String animalName,int animalImage)\r\n    {\r\n        this.animalImage=animalImage;\r\n        this.animalName=animalName;\r\n    }\r\n    public String getAnimalName()\r\n    {\r\n        return animalName;\r\n    }\r\n    public int getAnimalImage()\r\n    {\r\n        return animalImage;\r\n    }\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>ArrayAdapter is a type of Adapter which acts a bridge between UI component and data source that helps us to fill data in UI component. It expects a Layout with a single TextView and for more customization in grid items or list items, we use custom adapters. ArrayAdapter is also an implementation of BaseAdapter so &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/custom-arrayadapter-tutorial-example.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Custom ArrayAdapter Tutorial With Example In Android Studio<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":617,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[62,1],"tags":[],"class_list":["post-610","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-adapter","category-archieve"],"acf":[],"psp_head":"<title>Custom ArrayAdapter Tutorial With Example In Android Studio \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Learn how to use Custom ArrayAdapter in Android with examples and code which give more customization to ArrayAdapter. ArrayAdapter is also an implementation of BaseAdapter so if we want more customization then we create a custom adapter and extend ArrayAdapter in that.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/custom-arrayadapter-tutorial-example.html\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/posts\/610","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/types\/post"}],"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=610"}],"version-history":[{"count":3,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/posts\/610\/revisions"}],"predecessor-version":[{"id":2838,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/posts\/610\/revisions\/2838"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media\/617"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/categories?post=610"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/tags?post=610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}