{"id":681,"date":"2016-01-18T13:02:30","date_gmt":"2016-01-18T13:02:30","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?p=681"},"modified":"2018-06-05T07:59:38","modified_gmt":"2018-06-05T07:59:38","slug":"custom-spinner-examples","status":"publish","type":"post","link":"https:\/\/abhiandroid.com\/ui\/custom-spinner-examples.html","title":{"rendered":"Custom Spinner Tutorial With Examples In Android Studio"},"content":{"rendered":"<p>In Android, Whenever we need to display a spinner item with image, text etc (i.e. creating more custom list) then we have to implement a custom adapter like base adapter. For customization we need to create a custom adapter class and then extends our default adapter in that class. Here we create a custom list using the overrided functions of adapter and display custom spinner. For more clarification about how&#8217;s an custom adapter implemented\u00a0firstly we need to understand spinner.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-686\" src=\"\/ui\/wp-content\/uploads\/2016\/01\/Custom-Spinner-in-Android.jpg\" alt=\"Custom Spinner in Android\" width=\"243\" height=\"229\" \/><\/center>In Android, Spinners provides you a quick way to select one value from a set of values. Android spinners are nothing but the drop-downlist seen in other programming languages. In a default state, a spinner shows its currently selected value. It provides a easy way to select\u00a0a value from a list of values.<\/p>\n<p><span style=\"color: #ff0000;\"><strong>Important Note: <\/strong><\/span>Spinner is associated with Adapter view so to fill the data in spinner we need to use one of the adapter class.<\/p>\n<p><strong>XML code of Spinner in Android:<\/strong><\/p>\n<pre>&lt;Spinner\r\nandroid:id=\"@+id\/simpleSpinner \"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\" \/&gt;\r\n<\/pre>\n<p>In above code snippet we implement a simple spinner in our xml file. Now to fill the data in this spinner we need to use one of the adapter class. Here we will use custom adapters so as to fill customize data with images in spinner.<\/p>\n<p><strong>Here is how\u2019s CustomAdapter class extends BaseAdapter:<\/strong><\/p>\n<pre>public class CustomAdapter extends BaseAdapter {\r\n\r\n@Override\r\npublic int getCount() {\r\nreturn 0;\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\n\r\nreturn null;\r\n}<\/pre>\n<p>BaseAdapter class in Android is <a href=\"\/java\/abstraction\">Abstract<\/a> class. So while extending it in CustomAdapter we override its methods based on requirement. In above code snippet the overridden functions of base adapter are used to set the data in spinner, listview or a gridview. These functions are already described in <a href=\"\/ui\/baseadapter-tutorial-example.html\">BaseAdapter tutorial<\/a>.<\/p>\n<p><strong>Sample code of Custom Adapter class when we extends ArrayAdapter in that:<\/strong><\/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 above code snippet we see the overrided functions of ArrayAdapter which are used to set the data in a list, grid or a spinner. These functions are already described in <a href=\"\/ui\/custom-arrayadapter-tutorial-example.html\">Custom ArrayAdapter tutorial<\/a>.<\/p>\n<hr \/>\n<h4><strong>Custom Spinner Example in Android Studio<\/strong><\/h4>\n<p><span style=\"color: #008000;\"><strong>Example 1:<\/strong><\/span> Example of custom spinner using BaseAdapter<\/p>\n<p>Below is the example in which we displayed the country names with images in a spinner and whenever you click on a item, country name will be displayed as a message using toast. Below is the final output and code:<\/p>\n<p>[cp_modal id=&#8221;cp_id_e51eb&#8221;]<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2143\" src=\"\/ui\/wp-content\/uploads\/2016\/08\/Download-Code1.png\" alt=\"Download Code\" width=\"428\" height=\"85\" \/>[\/cp_modal]<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-684\" src=\"\/ui\/wp-content\/uploads\/2016\/01\/Custom-Spinner-Example-using-BaseAdapter-in-Android.jpg\" alt=\"Custom Spinner Example using BaseAdapter in Android\" width=\"251\" height=\"416\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/01\/Custom-Spinner-Example-using-BaseAdapter-in-Android.jpg 251w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/01\/Custom-Spinner-Example-using-BaseAdapter-in-Android-181x300.jpg 181w\" sizes=\"auto, (max-width: 251px) 100vw, 251px\" \/><\/center><strong>Step 1:<\/strong> Create a new project in Android Studio and name it CustomSpinnerExample.<\/p>\n<pre>Select File -&gt; New -&gt; New Project. Fill the requirements and click \"Finish\" button.<\/pre>\n<p><strong>Step 2:<\/strong> Open res -&gt; layout -&gt;\u00a0xml (or) activity_main.xml\u00a0and add following code. Here we are creating Spinner inside Relative Layout.<\/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    android:paddingBottom=\"@dimen\/activity_vertical_margin\"\r\n    android:paddingLeft=\"@dimen\/activity_horizontal_margin\"\r\n    android:paddingRight=\"@dimen\/activity_horizontal_margin\"\r\n    android:paddingTop=\"@dimen\/activity_vertical_margin\"\r\n    tools:context=\".MainActivity\"&gt;\r\n\r\n    &lt;Spinner\r\n        android:id=\"@+id\/simpleSpinner\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_centerHorizontal=\"true\"\r\n        android:layout_marginTop=\"50dp\" \/&gt;\r\n\r\n&lt;\/RelativeLayout&gt;<\/pre>\n<p><strong>Step 3:<\/strong> Create a new layout activity in res-&gt; layout and name it custom_spinner_items.xml. Add following code. Here we are defining basic layout for custom items that will be displayed inside Spinner.<\/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;&lt;!--Make sure image is present in Drawable folder--&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><strong>Step 4:<\/strong> Open app -&gt; java -&gt; package -&gt;\u00a0MainActivity.java and add the following code. Explanation is included in the code itself.<strong><br \/>\n<\/strong><\/p>\n<pre>package example.abhiandriod.customspinnerexample;\r\n\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.os.Bundle;\r\nimport android.view.View;\r\nimport android.widget.AdapterView;\r\nimport android.widget.Spinner;\r\nimport android.widget.Toast;\r\n\r\npublic class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{\r\n\r\n\r\n    String[] countryNames={\"India\",\"China\",\"Australia\",\"Portugle\",\"America\",\"New Zealand\"};\r\n    int 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\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n        \/\/Getting the instance of Spinner and applying OnItemSelectedListener on it\r\n        Spinner spin = (Spinner) findViewById(R.id.simpleSpinner);\r\n        spin.setOnItemSelectedListener(this);\r\n\r\n        CustomAdapter customAdapter=new CustomAdapter(getApplicationContext(),flags,countryNames);\r\n        spin.setAdapter(customAdapter);\r\n    }\r\n\r\n\r\n    \/\/Performing action onItemSelected and onNothing selected\r\n    @Override\r\n    public void onItemSelected(AdapterView&lt;?&gt; arg0, View arg1, int position,long id) {\r\n        Toast.makeText(getApplicationContext(), countryNames[position], Toast.LENGTH_LONG).show();\r\n    }\r\n\r\n    @Override\r\n    public void onNothingSelected(AdapterView&lt;?&gt; arg0) {\r\n        \/\/ TODO Auto-generated method stub\r\n    }\r\n}\r\n   <\/pre>\n<p><strong>Step 5:<\/strong> Create a new Class app -&gt; java-&gt; package and new class name CustomAdapter.java and add the following code. Here we will <a href=\"\/java\/method-overriding\">override the methods<\/a> of BaseAdapter to fill data in Spinner.<strong><br \/>\n<\/strong><\/p>\n<pre>package example.abhiandriod.customspinnerexample;\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.BaseAdapter;\r\nimport android.widget.ImageView;\r\nimport android.widget.TextView;\r\n\r\npublic class CustomAdapter extends BaseAdapter {\r\n    Context context;\r\n    int flags[];\r\n    String[] countryNames;\r\n    LayoutInflater inflter;\r\n\r\n    public CustomAdapter(Context applicationContext, int[] flags, String[] countryNames) {\r\n        this.context = applicationContext;\r\n        this.flags = flags;\r\n        this.countryNames = countryNames;\r\n        inflter = (LayoutInflater.from(applicationContext));\r\n    }\r\n\r\n    @Override\r\n    public int getCount() {\r\n        return flags.length;\r\n    }\r\n\r\n    @Override\r\n    public Object getItem(int i) {\r\n        return null;\r\n    }\r\n\r\n    @Override\r\n    public long getItemId(int i) {\r\n        return 0;\r\n    }\r\n\r\n    @Override\r\n    public View getView(int i, View view, ViewGroup viewGroup) {\r\n        view = inflter.inflate(R.layout.custom_spinner_items, null);\r\n        ImageView icon = (ImageView) view.findViewById(R.id.imageView);\r\n        TextView names = (TextView) view.findViewById(R.id.textView);\r\n        icon.setImageResource(flags[i]);\r\n        names.setText(countryNames[i]);\r\n        return view;\r\n    }\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>Now run the app in Emulator\/AVD and you will see the custom spinner displaying flags and country names together. Change country names inside spinner and you will see Toast displaying message which country is selected.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-685\" src=\"\/ui\/wp-content\/uploads\/2016\/01\/Custom-Spinner-Example-Output-in-BaseAdapter.jpg\" alt=\"Custom Spinner Example Output in BaseAdapter\" width=\"237\" height=\"406\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/01\/Custom-Spinner-Example-Output-in-BaseAdapter.jpg 237w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/01\/Custom-Spinner-Example-Output-in-BaseAdapter-175x300.jpg 175w\" sizes=\"auto, (max-width: 237px) 100vw, 237px\" \/><\/center><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Android, Whenever we need to display a spinner item with image, text etc (i.e. creating more custom list) then we have to implement a custom adapter like base adapter. For customization we need to create a custom adapter class and then extends our default adapter in that class. Here we create a custom list &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/custom-spinner-examples.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Custom Spinner Tutorial With Examples In Android Studio<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":684,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[62,1],"tags":[64,65],"class_list":["post-681","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-adapter","category-archieve","tag-adding-custom-images-in-spinner","tag-spinner-customization"],"acf":[],"psp_head":"<title>Custom Spinner Tutorial With Examples In Android Studio \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Tutorial on Custom Spinner to display a spinner item with image, text etc using custom Adapter like base adapter. Also find example code for project to understand the topic.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/custom-spinner-examples.html\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/posts\/681","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=681"}],"version-history":[{"count":1,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/posts\/681\/revisions"}],"predecessor-version":[{"id":2588,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/posts\/681\/revisions\/2588"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media\/684"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=681"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/categories?post=681"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/tags?post=681"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}