‫Welcome back.
‫In this video we're going to learn about spinors.
‫In Android.
‫A spinner is a UI component that provides a drop down menu with a list of items.
‫Users can select one item from the list.
‫The selected item is displayed in the spinner when it is closed, and to create a spinner, let's move
‫to the layout.
‫And here I start with a spinner tag.
‫Wrap content, wrap content.
‫I'll give it an ID called My Spinner.
‫You can see our spinner appears here in the layout.
‫I'll drag and drop it to here and infer the constraints.
‫Here we go.
‫This is our spinner.
‫Now let's move to the main activity and initialize this spinner.
‫I'll start with val spinner.
‫It's of type spinner.
‫Import the class of the spinner.
‫Find view by id or id dot my spinner.
‫This is how we initialize the spinner in Android.
‫We need to create an array of strings in order to populate and display the items of the spinner.
‫So this array will act as a data source for the spinner.
‫To display the items in the dropdown menu, I created this array called Operating Systems.
‫It's an array of windows, iOS, Android, and Linux strings.
‫These are the items that I want to display in the dropdown menu in the spinner.
‫Again guys, the first step is to create the view which is the spinner.
‫The second step is to create the data source which is the array.
‫The third step is to link the view with the data source.
‫And this is done with the help of adapters.
‫An adapter is a crucial component that acts as a bridge between the data and the UI components.
‫Adapters are commonly used to bind data to the views in widgets like list, view, recycler, view,
‫spinners, and others.
‫In our example, we're going to use an array adapter, which is an adapter, and this type of adapters.
‫The array adapter is used to bind an array of data with the spinner.
‫It converts each item in the array to a view that can be displayed in the UI, and which is in this
‫case spinner.
‫So I need to populate and display this list inside the spinner.
‫Inside this dropdown menu, you can create an instance of array adapter by providing a reference to
‫the context, a layout resource for the appearance of each item in the list, and the data source,
‫an array or a list.
‫And in this case it's an array called operating systems.
‫Val adapter equals to array adapter.
‫And here I need to pass the parameters.
‫The first parameter is the context.
‫The second parameter.
‫This because we are working here in the main activity.
‫The second parameter is the layout resource.
‫And I'll specify android dot r dot layout dot simple spinner.
‫Right.
‫This layout is used for the appearance of each item when the spinner is closed.
‫And the third parameter is the array that you want to publish, which is in this case called operating
‫systems.
‫Here how we create an adapter, we pass the context at the first parameter.
‫The second parameter is the layout for every item, and this is a ready made and included in Android
‫packages.
‫Operate system is the array that you want to use to populate its items in the view.
‫The last step is to set the adapter to a view.
‫So after creating the array adapter, you need to set it to the spinner or a list view or any other
‫views.
‫But in this case we need to set it to the spinner.
‫I'll use spinner dot adapter equals to adapter.
‫So I'm setting this adapter to the spinner dot adapter method.
‫Let's run our application and call this function called Display Spinner in the Oncreate method.
‫Let me run again.
‫And here we go.
‫This is our drop down menu.
‫This is our spinner.
‫Click on it windows, iOS, Android and Linux.
‫And you can select and it will appear here.
‫Now let's handle the click events and the selection of these items in the spinner.
‫I want from you to focus with me.
‫Because in this spinner handling the item selection is different.