Spinner List Item

android spinner add item programmatically
android spinner style
how to display selected value in spinner in android
android spinner example
custom spinner in android example
spinner kotlin
spinner in android - stack overflow
dynamic drop down list in android

I'm currently working on pricing calculator in which the customer only need to select what they want from the spinner list, then the calculator will calculated the total price.

I have no prob in creating the spinner drop down list, but each item in the spinner have different value for example if spinner have "LORRY" "BUS" and "CAR" and for "LORRY" the price is 20, "CAR" is 30 and "BUS" was 10 dollar.

How can I add different values into the each spinner item. I use string array to create the drop list item. I have created on Android Studio platform.

MainActivity.java

package com.jika.directcalc;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Spinner mySpinner = (Spinner) findViewById(R.id.spinnervalue);
        ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(MainActivity.this,
        android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.value));
        myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mySpinner.setAdapter(myAdapter);

    }
}

at first get the value from spinner

int value = 0; String Text = String.valueOf(mySpinner.getSelectedItem());

then assign the value e.g.

if(Text == "BUS"){ value = 20; }

Spinners, R.array.planets_array, android.R.layout.simple_spinner_item ).also { adapter -> // Specify the layout to use when the list of choices appears Generally, the android spinners will provide a quick way to select one item from the list of values and it will show a dropdown menu with a list of all values when we click or tap on it. By default, the android spinner will show its currently selected value and by using Adapter we can bind the items to spinner objects.


You need to have a list of data that you want the spinner to display

    List<String> spinnerArray =  new ArrayList<String>();
    spinnerArray.add("item1");
    spinnerArray.add("item2");

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinnerArray);

    adapter.setDropDownViewResource(
    android.R.layout.simple_spinner_dropdown_item);

    Spinner sItems = (Spinner) findViewById(R.id.spinner1);
    sItems.setAdapter(adapter);

Also to find out what is selected you could do something like this

    String selected = sItems.getSelectedItem().toString();
    if (selected.equals("what ever the option was")) {

    }

Spinner List Item, at first get the value from spinner. int value = 0; String Text = String.valueOf(​mySpinner.getSelectedItem());. then assign the value e.g.. Xamarin.Android Spinner. 02/06/2018; 4 minutes to read; In this article. Spinner is a widget that presents a drop-down list for selecting items. This guide explains how to create a simple app that displays a list of choices in a Spinner, followed by modifications that display other values associated with the selected choice.


When You need while calculating the total, use getSelectedItem() or

getSelectedItemPosition()

String text = mSpinner.getSelectedItem().toString();
switch(text)
{
    case "LORRY" : value=20;break;
    case "CAR"   : value=30;break;
    case "BUS" : value=10;break;
}

You can use position too.

switch(mSpinner.getSelectedItemPosition())
{
  //cases
}

If you want to get immediately when the Item is selected, Use listener

mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
        switch(position)
        {
        case 0 : value=20;break;
        case 1 : value=30;break;
        case 2 : value=10;break;
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parentView) {
        // your code here
    }

});

How to add items to a spinner in Android?, <Spinner android:id="@+id/Spinner01" android:layout_width="wrap_content" final List<String> list = new ArrayList<String>(); list.add("Item 1"); list.add("Item  Retrieve Spinner drop down list selected item value in android. Spinner is a type of drop down list menu and you can select your item by tapping on mobile screen. OnItemSelectedListener() function is apply to get selected item value from spinner drop down list menu. So here is the complete tutorial for Get selected item value from Spinner in


How can I add items to a spinner in Android?, This example demonstrate about how to add items to a spinner . In the above code we are adding item to array list and added to ArrayAdpter  Android spinner is associated with AdapterView.So we need to set the adapter class with the Spinner. Android Drop Down List. Following xml file shows the layout of a typical spinner in android which consists of a text label and a spinner element tag.


Android - Spinner, Android - Spinner - Spinner allows you to select an item from a drop down menu. file to create a simple list view with items which are showing as spinner items. List of choices to display (as a list) Show Filter Bar. If checked, will display a Search Filter Bar above the Listpicker. After Picking. After the user has selected from the list. Selection. The selected item. Selection Index. The index of the selected item, starting at 1. If no item is selected, the value will be 0


Android Spinner - drop down list, Selecting an item from spinner would display a toast message. To pass data in the form of bundles between activities, we'll use a button to perform an intent and​  Spinner is just like a drop down button, using this button we can select a item from set of items. This example demonstrate about how to add items to a spinner in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.