
This pattern of specifyng an Object in the Layout resource then referencing from the Java/Kotlin code is common in android as a platform and is also the one recommended. We can use the Spinner object to specify the spinner in an xml Layout. This allows Spinner to set and return a SpinnerAdapter. For example, Spinner is an adapterview since it derives from the AbsSpinner. public class SpinnerĪbsSpinner provides to Spinner much of the capabilities it has. Spinner is a public class that’s why we can access and use it. Hence we can customize the views shown in the spinner without affecting data. This makes Spinner like other adapterviews decoupled from data source. This means that it relies on an Adapter for its data.Īdapters act as the bridge between adapterviews and the underlying data source. Spinner is a view that displays items in a dropdown fashion, allowing user to pick one at a time.

This class also resides in the android.widget package.ĪbsSpinner provides to Spinner much of the capabilities it has. Spinner class inherits from an abstract class called AbsSpinner. Public classes are visible even from other packages apart from those they’ve been defined. This solution seems way cleaner, with no dirty hacks - just add popupWindow?.dismiss() when the header clicked.Spinner is a concrete public class residing in the android.widget package. Thus… Not a lot of work was required to transition 8) That’s what we all love. Note that you can use the very same CountryAdapter but rename the getDropDownView() method to getView(), and get rid of existing getView(). We have full control of its UI! I chose ListView, could be anything else! All rendered by layout_country_dropdown.xml. So here we handle the dropdown view by ourselves. The magic of adding a header will be done in our CountryAdapter class:ĬountryAdapter.kt class CountryAdapter( context: Context ) : ArrayAdapter(context, 0, OperatedCountry.values()) With property android:background we can add a blue outline when Spinner is in the selected state and with android:popupBackground the background of dropdown view - easy so far.

Thanks to we can see in Android Studio Designer how the Spinner will look like and adjust paddings/margins properly :) In this way, we’ve just built a Spinner widget on our UI. OK, so let’s begin with implementing the layout: Is it possible to implement using Spinner Widget? Let’s see. So the design for selection was not looking like a default Android Spinner as it had a header included when going to dropdown mode. Spinner Design ||| Spinner Design - dropdown
