How to setup spinner in android application.
Date : 23/05/2019
Spinners provide a quick way to select one value from a set. In the default, a spinner shows its currently selected value. Touching the spinner, it displays a drop down menu with all other available values, from which the user can select a new one.
Step 1 : You can implement spinner in XML file or by start designing spinner in code. In this article explained the spinner implementation with XML file. Necessary elements to create spinner in UI is an adapter and spinner object. Also mentioned the spinner declaration in layout file.
Step 2 : .To populate the list in spinner, you have to implement adapter in code. There is two way on creating array, one is string or char-sequence, integrate with array-adapter and another way is a resource category called string-array, also mention the array name in array adopter creation. You can find a snapshot of this below. Additionally we need to mention the type spinner. There are few built-in types available in android resource.It is appropriate for most of the conditions. You can also create your own style of spinner list view.
Step 3 : Here i have mentioned how to declare string- array in values.xml catogory for the content in spinner list.
Step 4 : The declaration and setup with array-adapter is simple in code. In case it’s in fragment, you have to declare the spinner from XML file.then create array-adapter using type of spinner and content of spinner from either resource file or manual creation of list of items. At last setup adapter.
Step 5 : To implement code upon clicking spinner item, you need to implement built-in AdapterView. OnItemSelectedListener interface in you fragment class declaration and over-ride two methods in that interface. Those two methods are
Step 6 : If you implement Adapterview. OnItemSelectedListener interface in your activity, you can declare this as instance of the interface in your class.
Thank you using pheonixsolutions!
If you find it useful, share it on public forums.