Spinner in Android Studio using Kotlin | Android Knowledge

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign welcome back to my channel in this video we will be creating a spinner so basically in simple world it's like a drop down list this is our fifth video from the series of Android kotlin Basics to advance so make sure that you save the playlist now back to the video so in Spinners the user will have different options to choose from for like example there is a form and in that form it asks for the country so the spinner will have a list of countries through which user will choose from you got my point right again Spinners have different types simple spinner custom spinner or exposed drop down but in this video we will cover a simple spinner with that said let's create the project choose empty views activity and click on next name the activity as spinner then click on finish let's first do the prerequisites so go to colors.xml press Ctrl D to duplicate the line name the color as blue and hex code AS 1a9df1 now let's go to themes.xml this is all optional if you want you can directly skip to activity main.xml as well so inside the themes add the three parameters color primary color primary variant and status bar color so let me quickly do that and it's done now I will add a background in the drawable I created this background in canva just to make the app look more happening and also to give you an example where exactly we can use Spinners got it we are done with the prerequisite now let's move on to activity main.xml so instead of constraint layout I'll be using relative layout then I'll give the padding for start as 34 DP and for end I study for DP then I will add the background again as I said it is just for the show purpose next of course a spinner I'll give it an ideal spinner then margin vertical is 590 DP and that's it now go to main activity let me quickly tell you what we are going to do here so the code might look similar like the list view code okay if you haven't watched the video click on the I button to watch so first we will create a list of all the items that will be present in the spinner then we will create an area adapter then we will set on item selected listener and then lastly add a toast got it first declare the spinner and initialize it so let me quickly do that and it's done now let's divide it in three step to understand it clearly step number one to create a list so well list item equals to list of in our previous video we have already used array of and arraylist of right this time we will go for list of list of is basically immutable which means fixed size but we do have something called as mutable list in code link that is basically the opposite of it which means Dynamic size so let me quickly add all the items in the list and done now let's move to our step number two that is creating an adapter let me quickly write the code and then I'll explain you so first we created an array adapter which has three parameters first is the context that means our current activity that is main activity and how do we represent our current activity by using this then we have layout as I previously said it's a simple spinner but if it will have been a custom spinner then we would have created a separate layout for it but for now we'll be using the default spinner layout provided by the IDE that is simple spinner item then the last parameter is list items which we just created so I have mentioned that okay next we will set drop down view resource on the adapter and again give it predefined layout as simple spinner drop down item so what is the difference between both of them so simple spinner item is basically the layout of items present inside the spinner and simple spinner drop down item is the layout of drop down itself see to understand more about the predefined files you can keep the cursor on that file and press Ctrl enter it will lead you to that particular file you can read it explore it but do not modify it okay now let's come back to our code once the adapter is created then set the array adapter on the spinner using adapter now our last method that is Step number three so let me quickly write the code and then I'll explain you foreign [Music] so the on item selected listener is an interface that defines two callback methods to be invoked when an item in the spinner is selected or when no item is selected we will be implementing this interface using an anonymous object got it then in on item selected method we have four parameters parent view position and ID I have rename it parent is basically the adapter view in our case it is the spinner then view is selected item view then position obviously the position of the selected item and then ID is the row ID of the selected item got it now why we need to know this because we will be giving a toast of the selected item and to do that we will require the parent that is Spinner and obviously the selected item position correct so parent dot get item and position that is the method we'll be using to retrieve the position of the selected item and then we will convert it to string using two string method then selected item is then displayed in a toast message using toast dot make tests this at the right main activity refers to the context of the main activity and text as you have selected whatever the selected item is will be using the dollar sign and then the duration of it cut it lastly we will call on nothing selected method when no item is selected and in that case we are not supposed to do anything hence I will leave it as blank and that's it now let's run the app it looks great let me click on the spinner and it provides myth for ice cream flavors to choose from perfect I'll choose maybe a mango and see there is a toast as you have selected mango flavor great so yeah that is it for the video if you are new to this Channel please consider subscribing to my channel and I'll see you in the next video [Music] foreign
Info
Channel: Android Knowledge
Views: 5,253
Rating: undefined out of 5
Keywords: spinner in android studio, spinner android studio firebase, android studio, spinner android beginner, text spinner android, spinner android example, basic spinner android, custom spinner android, android studio tutorial, android tutorial, spinner tutorial, basic spinner tutorial android, basic android app, android spinner, android apps, android dropdown, android spinner tutorial, android drop down, android spinner adapter, spinner, android, how to make android apps, kotlin
Id: JDbsuACIoQE
Channel Id: undefined
Length: 11min 17sec (677 seconds)
Published: Thu Jun 15 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.