Reputation: 634
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
Reputation: 1276
spinner.setSelection(position);
this works even in button clicks.
Upvotes: 1
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
Reputation: 1934
by default value, you mean to say the value at 0 index. Then it should be
spinner.setSelection(0);
Upvotes: 12
Reputation: 527
call this thing in your reset on click event
assetSpinner.setSelection(0);
Upvotes: 4
Reputation: 67286
You can using Spinners setSelection
attribute to reset your Spinner to its original position. spinner.setSelection(position);
Upvotes: 22