{"id":655,"date":"2016-01-18T13:20:30","date_gmt":"2016-01-18T13:20:30","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?page_id=655"},"modified":"2019-06-12T10:40:40","modified_gmt":"2019-06-12T10:40:40","slug":"spinner","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/ui\/spinner","title":{"rendered":"Spinner Tutorial With Examples In Android Studio"},"content":{"rendered":"<p>In Android, Spinner provides a quick way to select one value from a set of values. Android spinners are nothing but the drop down-list 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><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-652\" src=\"\/ui\/wp-content\/uploads\/2016\/01\/Spinner-in-Android.jpg\" alt=\"Spinner in Android\" width=\"352\" height=\"250\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/01\/Spinner-in-Android.jpg 352w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/01\/Spinner-in-Android-300x213.jpg 300w\" sizes=\"auto, (max-width: 352px) 100vw, 352px\" \/><\/center>In Simple Words we can say that a spinner is like a combo box of AWT or swing where we can select a particular item from a list of items. Spinner is a sub class of AsbSpinner class.<\/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>Here is the XML basic code for Spinner:<br \/>\n<\/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><span style=\"color: #ff0000;\"><strong>Important Note:<\/strong><\/span> To\u00a0fill the data in a\u00a0spinner we need to implement\u00a0an\u00a0adapter class. A spinner is mainly used to display only text field so we can implement Array Adapter for that. We can also use <a href=\"\/ui\/baseadapter-tutorial-example.html\">Base Adapter<\/a> and other custom adapters to display a spinner with more customize list. Suppose if we need\u00a0to display a textview and a imageview in spinner item list then <a href=\"\/ui\/arrayadapter-tutorial-example.html\">array adapter<\/a> is not enough for that. Here we have to implement custom adapter in our class. Below image of Spinner and Custom Spinner will make it more clear.<\/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><span style=\"color: #008000;\"><strong>ArraryAdapter:<\/strong><\/span><\/p>\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. Whenever you have a list of single items which is backed by an array, you can use Array Adapter.<\/p>\n<p><strong>Here is code of ArrayAdapter in Android:<\/strong><\/p>\n<pre>ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)\r\n<\/pre>\n<p>For more details read <a href=\"\/ui\/arrayadapter-tutorial-example.html\">ArrayAdapter Tutorial<\/a> as here we will use it in the below example to explain how Spinner is created in Android.<\/p>\n<hr \/>\n<h4><strong>Example of Spinner In Android Studio:<\/strong><\/h4>\n<p><span style=\"color: #008000;\"><strong>Example 1:<\/strong><\/span> Below is the example in which we display a list of bank names in a spinner and whenever you select an item the value will be displayed using toast on Mobile screen. Below is the final output and code:<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"https:\/\/github.com\/abhisheksaini4\/SpinnerExamples\" 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-653\" src=\"\/ui\/wp-content\/uploads\/2016\/01\/Spinner-Example-in-Android-Using-ArrayAdapter.jpg\" alt=\"Spinner Example in Android Using ArrayAdapter\" width=\"234\" height=\"365\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/01\/Spinner-Example-in-Android-Using-ArrayAdapter.jpg 234w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/01\/Spinner-Example-in-Android-Using-ArrayAdapter-192x300.jpg 192w\" sizes=\"auto, (max-width: 234px) 100vw, 234px\" \/><\/center><strong>Step 1:<\/strong> Create a new project in Android Studio and name it SpinnerExample.<\/p>\n<pre>Select File -&gt; New -&gt; New Project -&gt;. Fill the forms and click \"Finish\" button.<\/pre>\n<p><strong>Step 2:<\/strong> Open res -&gt; layout -&gt; activity_main.xml (or) main.xml\u00a0and add following code. Here we will create a Spinner inside Relative Layout.<\/p>\n<pre>&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\nxmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"match_parent\"\r\nandroid:paddingBottom=\"@dimen\/activity_vertical_margin\"\r\nandroid:paddingLeft=\"@dimen\/activity_horizontal_margin\"\r\nandroid:paddingRight=\"@dimen\/activity_horizontal_margin\"\r\nandroid:paddingTop=\"@dimen\/activity_vertical_margin\"\r\ntools:context=\".MainActivity\"&gt;\r\n\r\n&lt;Spinner\r\nandroid:id=\"@+id\/simpleSpinner\"\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_centerHorizontal=\"true\"\r\nandroid:layout_marginTop=\"100dp\" \/&gt;\r\n\r\n&lt;\/RelativeLayout&gt;<\/pre>\n<p><strong>Step 3:<\/strong> Now open app-&gt; java -&gt; package -&gt;\u00a0MainActivity.java and add the following code. Here we will use ArrayAdapter to fill the data in Spinner. Also we are using Toast to display when the item in Spinner is selected.<strong><br \/>\n<\/strong><\/p>\n<pre>package example.abhiandriod.spinnerexample;\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.ArrayAdapter;\r\nimport android.widget.Spinner;\r\nimport android.widget.Toast;\r\n\r\npublic class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{\r\n\r\nString[] bankNames={\"BOI\",\"SBI\",\"HDFC\",\"PNB\",\"OBC\"};\r\n@Override\r\nprotected void onCreate(Bundle savedInstanceState) {\r\nsuper.onCreate(savedInstanceState);\r\nsetContentView(R.layout.<em>activity_main<\/em>);\r\n<em>\/\/Getting the instance of Spinner and applying OnItemSelectedListener on it\r\n<\/em>Spinner spin = (Spinner) findViewById(R.id.<em>simpleSpinner<\/em>);\r\nspin.setOnItemSelectedListener(this);\r\n\r\n<em>\/\/Creating the ArrayAdapter instance having the bank name list\r\n<\/em>ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.<em>simple_spinner_item<\/em>,bankNames);\r\naa.setDropDownViewResource(android.R.layout.<em>simple_spinner_dropdown_item<\/em>);\r\n<em>\/\/Setting the ArrayAdapter data on the Spinner\r\n<\/em>spin.setAdapter(aa);\r\n}\r\n\r\n\r\n<em>\/\/Performing action onItemSelected and onNothing selected\r\n<\/em>@Override\r\npublic void onItemSelected(AdapterView&lt;?&gt; arg0, View arg1, int position,long id) {\r\nToast.<em>makeText<\/em>(getApplicationContext(), bankNames[position], Toast.<em>LENGTH_LONG<\/em>).show();\r\n}\r\n\r\n@Override\r\npublic void onNothingSelected(AdapterView&lt;?&gt; arg0) {\r\n<em>\/\/ <\/em><em>TODO Auto-generated method stub\r\n<\/em>\r\n}\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<p>Now the run the program in Emulator and you will see options to choose among bank names present inside drop down list. You will also see Toast message displaying on the screen when you will select the particular bank.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-654\" src=\"\/ui\/wp-content\/uploads\/2016\/01\/Spinner-Example-Output-in-Android-Studio.jpg\" alt=\"Spinner Example Output in Android Studio\" width=\"502\" height=\"285\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/01\/Spinner-Example-Output-in-Android-Studio.jpg 502w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/01\/Spinner-Example-Output-in-Android-Studio-300x170.jpg 300w\" sizes=\"auto, (max-width: 502px) 100vw, 502px\" \/><\/center><\/p>\n<hr \/>\n<h4><strong>Custom Spinner:<\/strong><\/h4>\n<p>Custom Spinner is used to display a spinner item with image, text etc (i.e. creating more custom list item). It is achieved in Android using custom adapter like base adapter. For more details read <a href=\"\/ui\/custom-spinner-examples.html\">Custom Spinner Tutorial<\/a>.<\/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><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Android, Spinner provides a quick way to select one value from a set of values. Android spinners are nothing but the drop down-list 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. In Simple &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/spinner\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Spinner Tutorial With Examples 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-655","page","type-page","status-publish","hentry"],"acf":[],"psp_head":"<title>Spinner Tutorial With Examples In Android Studio \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Tutorial on Spinner which provides a quick way to select one value from a set of values. Learn it with examples in Android Studio and code.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/spinner\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/655","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=655"}],"version-history":[{"count":3,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/655\/revisions"}],"predecessor-version":[{"id":2799,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/655\/revisions\/2799"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=655"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}