Suleman Khan
Suleman Khan

Reputation: 634

How to get back to default value in spinner onclick?

My spinner coding is something like this:

    assetSpinner = (Spinner) findViewById(R.id.editAsset);
    assetAdapter = ArrayAdapter.createFromResource(
            this, R.array.asset_array, android.R.layout.simple_spinner_item);
    assetAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    assetSpinner.setAdapter(assetAdapter);

Now I have a reset button in my design. So my question is when I click on reset button how to make the spinner get back to default value or reset the spinner.

Upvotes: 7

Views: 22356

Answers (5)

dreamdeveloper
dreamdeveloper

Reputation: 1276

spinner.setSelection(position);

this works even in button clicks.

Upvotes: 1

Azurespot
Azurespot

Reputation: 3152

Here is an example of my code. I put this in my onCreate():

    // Resets all spinners
    Button resetFilters = (Button) findViewById(R.id.resetButton);
    resetFilters.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            imageType.setSelection(0);
            colorFilter.setSelection(0);
            imageSize.setSelection(0);
        }
    });

It changes all my spinners back to their default position, right before my eyes when button is clicked.

Upvotes: 2

akkilis
akkilis

Reputation: 1934

by default value, you mean to say the value at 0 index. Then it should be

spinner.setSelection(0);

Upvotes: 12

Raghav Chopra
Raghav Chopra

Reputation: 527

call this thing in your reset on click event

assetSpinner.setSelection(0);

Upvotes: 4

Lalit Poptani
Lalit Poptani

Reputation: 67286

You can using Spinners setSelection attribute to reset your Spinner to its original position. spinner.setSelection(position);

Upvotes: 22

Related Questions