What is Spinner in Android ?
Now we will see how we can design Spinner inside Android, so for this we are creating an application named 'MySpinner'.
- First we will see its manifest file which is generated by default.
- After that we will design its layout, for that we will open xml inside the 'layout' folder which is inside the 'res' folder. Here we have taken a relative layout, we have set the basic properties of the relative layout, now we need to design a spinner here, so for that first we will use the spinner tag, and set some of its basic properties.
- Now after this we will design a text view here, we will use the text view to display the items of the spinner. So, we have used a spinner and a text view inside our relative layout.
- Now we will see our main activity inside our 'java' folder, which is generated by default.
- Now we will design the spinner's code, first of all we will create an array 'state' within which we have specified the names of different versions of Android.
- Now we will generate the spinner and text view instants here.
- Now we will also identify the spinner and the text view we have taken.
- Right here, we have to use an ArrayAdapter, with its we can display the items of the array in a spinner. So we have created an ArrayAdapter 'adapter_state', inside which we are passing 'this' in the argument. And then we specified 'simple_spinner_item'
- After that we will pass the array we have created.
- Now after this we have to set the dropdown list, for this we will use the instance of the adapter that we have created, and then we will use 'setDropDownViewResource', and after that we have selected 'simple_spinner_dropdown_item'.
- Now we have to set the adapter, for this we will use instance, and after that we will use 'setOnItemSelectedListener'.
- Now we have used 'OnItemSelected' with the adapter view. Now we will first specify setSelection in which we will specify the argument's position. String selState = (String)spinnerOsversions.getSelectedItemId();
- So like this with the help of an ArrayAdapter we have created a Spinner, which means that content of spinner are printed through the contents of ArrayAdapter.
- Now we'll execute it and see how our code runs. As soon as we click on 'run app' , our app runs accurately.