How to reset multiple spinner values to default

clear spinner selection android
how to refresh spinner in android
spinner setonitemselectedlistener
android spinner
how to display selected value in spinner in android
android spinner values
androidx spinner
spinner layout

I'm building the a app which has more than 20 spinners. I'm trying to reset all spinners with one click. is that possible? could you please guide me on how to achieve this. below is my codes.

If possible i want the reset to be selected from menu ( 3 dots on top right) can someone please assist me. thank you in advance :)

package com.example.hfacs_test09;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;


public class Unsafe extends Fragment implements OnItemSelectedListener  {

private Spinner uhs1a,uhs1b,uhs1c;
private TextView unsaferesult;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.unsafe, container, false);    

    unsaferesult = (TextView) rootView.findViewById(R.id.unsaferesult);

    uhs1a = (Spinner) rootView.findViewById(R.id.uhs1a);
    uhs1b = (Spinner) rootView.findViewById(R.id.uhs1b);
    uhs1c = (Spinner) rootView.findViewById(R.id.uhs1c);

    uhs1a.setOnItemSelectedListener(this);
    uhs1b.setOnItemSelectedListener(this);
    uhs1c.setOnItemSelectedListener(this);

    return rootView;
}

public void onItemSelected(AdapterView<?> parent, View view, int position,
        long id){

   //uhs1a.setSelection(position);


   int r1 = Integer.valueOf((String) uhs1a.getSelectedItem());

   int r2 = Integer.valueOf((String) uhs1b.getSelectedItem());

   int r3 = Integer.valueOf((String) uhs1c.getSelectedItem());

   int total = r1 + r2 + r3;

   //int myresult = (Integer) uhs1a.getSelectedItem() ;

   unsaferesult.setText("Total Score is " + total);

 }
 public void onNothingSelected(AdapterView<?> parent) {

}
}

To reset a spinner to default value:

uhs1a = (Spinner) rootView.findViewById(R.id.uhs1a); // Ignore this if you already did that in onCreateView 
uhs1a.setSelection(0); // Assuming the default position is 0.

To do that for all the spinners, either you do that manually for every spinner, or you add all spinners to an ArrayList and then

for(int i=0; i < myArrayList.size(); i++)
    myArrayList.get(i).setSelection(0);

To do that from the Menu (3 dots) button:

  @Override
  public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.your_menu_xml_file, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.reset_button:

        // Reset spinners here.

        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

How to get back to default value in spinner onclick?, How can i reset Spinner to default ? How can i set Default item selected programmatically after initializing adapter? spinner.setSelection(0)� As John has so kindly suggested, you can simply change the default value of your spinner fields to a hyphen (-) character: This is because our Spinner fields were designed to have some number in them, and if you leave them empty, they will be populated with the default value of zero.

It is same as single spinner.

spinner.setSelection(position);

just do this for all the spinners in your onclick of the button method.

How to Reset Spinner and how to select default item after , int : An attribute in the current theme that contains a reference to a style resource that supplies default values for the view. Can be 0 to not look for� How to reset multiple spinners at the same time in the itemselected event of spinners? I have multiple spinners in a horizontal scroll view. When i set selection in the itemselected event of one spinner, all the other spinners in the horizontal scroll view should get reset to their default values (setselection(0)).

your_spinner.setSelection(0); // it will set your spinner to position 1. // if you pass 1 it will set to position 2.....

Spinner, In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values,� This Video will teach you How to set first value (default value) in spinner as empty. Means, until the user selects value from list, spinner will have no value. android,android programming,android

Spinners, I want to preselect the value that is stored in the database for a Spinner but I can' . .. .indexOf(value); getSpinnerField().setSelection(position); } The spinner_count will be 0 when the first time the activity is rendered, hence as soon as it displays first item (Delhi in this case) by default, it will trigger onItemSelected, but since we have handled it, nothing will happen and the UI will wait for the user to select an Item.

Setting selected item of Spinner by value rather than by position , This Video will teach you How to set first value (default value) in spinner as empty . Means Duration: 9:45 Posted: May 10, 2018 The spinner provides a way to set the selected valued based on the position using the setSelection(int position) method. Now to get the position based on a value you have to loop thru the spinner and get the position. Here is an example

Spinner in android with default selected value as empty/null, In the default state, it shows 0 index value from a set. Touching the spinner displays a dropdown menu or dialog with all other available values in� The Spinner will always have a selection. You can place the "Select Option" in the spinner as item. If it's the first item it will be choosen by default. "The Spinner will always have a selection", think about it.

Comments
  • Thank you for the accurate information! it works after following your code. I really appreciate your time. Thank you again.
  • @Alexis you're absolutely welcome! Please accept this answer as the correct answer. If you need any more help, please ask me. Thanks! :-)
  • Thank you for your reply :)