Andy Lobel
Andy Lobel

Reputation: 3416

How to create a spinner in android?

this works but when the activity starts it automatically toasts 'One' because it is selected by default. How to make it so the spinner box contains a default value that isnt in the actual dialog such as 'Please choose a catagory', or at least so that 'one' is not auto selected. Thanks

final String[] items = new String[] {"One", "Two", "Three"};
        final Spinner catagorySpinner = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(Expense1.this,
                    android.R.layout.simple_spinner_item, items);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        catagorySpinner.setAdapter(adapter);



        catagorySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {               
            Toast.makeText(getApplicationContext(), items[position], Toast.LENGTH_SHORT).show();


     }

        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub

        }});

Upvotes: 1

Views: 911

Answers (3)

Andy Lobel
Andy Lobel

Reputation: 3416

this will make it so the array adapter is changed when the spinner is clicked making it so the value that was origionally there is gone; in this case "select a catagory"

        //final String selected;
        final int a; 
        final int x = 1;
        final ArrayList<String> items = new ArrayList<String>();
        items.add("Select A Category");
        final Spinner catagorySpinner = (Spinner) findViewById(R.id.spinner);
        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(Expense1.this,
                    android.R.layout.simple_spinner_item, items);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        catagorySpinner.setAdapter(adapter);

        final ArrayList<String> itemsTwo = new ArrayList<String>();
        itemsTwo.add("one"); itemsTwo.add("two"); itemsTwo.add("three");

        final ArrayAdapter<String> adapterTwo = new ArrayAdapter<String>(Expense1.this,
                    android.R.layout.simple_spinner_item, itemsTwo);
        adapterTwo.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        /* on spinner click listener (not items inside) */
        catagorySpinner.setOnTouchListener(new OnTouchListener(){
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_UP){
                    catagorySpinner.setAdapter(adapterTwo);
                            catagorySpinner.setSelection(a);
                    x++;
                }

                return false;
            }
        });

        catagorySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
                // if the spinner has been opened or not 
                 if(x!=1){      
                     a = position;
                    //code to execute if spinner has been clicked and arrayAdapter has been updated
                    //in my case selected = myArray.get(position);

            } else {

                //code to execute if "choose a catagory" is still there
                //in my case selected = "novalue";
            }
        }

        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub
            }
        });

Upvotes: 0

Caner
Caner

Reputation: 59318

The spinner in Android by default shows the first value in the adapter if nothing is selected as default. There is no way to change it unfortunately.

In your case, you can add Choose a Category to your array:

final String[] items = new String[] {"Choose a category", "One", "Two", "Three"};

But inside onItemSelected you have to handle this, ie:

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    if(position != 0)  {
        Toast.makeText(getApplicationContext(), items[position], Toast.LENGTH_SHORT).show();
    }
}

Upvotes: 2

vik
vik

Reputation: 324

You can manually set the default selected item in the spinner.

        catagorySpinner.setSelection(2);

Upvotes: 0

Related Questions