How to set default value to spinner dropdown in Android?
Asked Answered
T

5

12

I need to set the default value text "select your Device" value to my spinner drop-down list, I put it as list item so it acts as a drop-down list items and I hide the drop-down list, because of that I’m getting a blank space in my drop-down.

How should I do this?

Image

this is the code I’m used

mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        /*Setup a Spinner*/
        mToolbarSpinner = (Spinner) findViewById(R.id.spinner_toolbar);

        /*Manage the spinner*/
        mToolbarSpinnerNavigationItems = new ArrayList<SpinnerNavItem>();

        mToolbarSpinnerNavigationItems.add(new SpinnerNavItem("Select your Seebo", R.drawable.deviceonline));

        /*setting the mToolbarSpinnerNavigationItems to list_item_title_navigation.xml*/
        mArrayAdapter = new ArrayAdapter(this, R.layout.list_item_title_navigation,mToolbarSpinnerNavigationItems){
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {

                // remember to inflate custom layout for spinner item
                // then set anything you want here
                // remember to use convertView.findViewById(), not just findViewById()

                // this is an example
                // inflate layout
                convertView = View.inflate(MainActivity.this, R.layout.list_item_title_navigation, null);
                // get view
                TextView tvText1 = (TextView) convertView.findViewById(R.id.devicetitle);
                ImageView imgIcon = (ImageView) convertView.findViewById(R.id.deviceicon);
                imgIcon.setVisibility(View.GONE);
                //imgIcon.setImageResource(mToolbarSpinnerNavigationItems.get(position).getIcon());
                // set content
                tvText1.setText(mToolbarSpinnerNavigationItems.get(position).getTitle());
                // return
                return convertView;
            }

            @Override
            public View getDropDownView(int position, View convertView, ViewGroup parent) {

                return convertView;
            }

        };

        //setting adapter to Spinner
        mToolbarSpinner.setAdapter(mArrayAdapter);
Trihedral answered 13/8, 2015 at 11:38 Comment(2)
possible duplicate of How to make an Android Spinner with initial text "Select One"Mandle
check this updated answer belowHybris
A
14

As far as my understanding of your question, what you can do is override the Spinner view by getting it start its indexing from -1 instead of 0 so that making the first entry of array to be the one you want as.

Select your Device

There are multiple questions already existing on StackOverflow which you can refer to and to do the overriding , a generalized solution has been suggested.

Refer here.

Or the other way round, you can also set the customized default position of spinner,your way, using this snippet of code :

ArrayAdapter<String> spinnerAdap = (ArrayAdapter<String>) mySpinner.getAdapter();

int spinnerPosition = messgAdap.getPosition(theDefaultValue);

mySpinner.setSelection(spinnerPosition);
Anorthite answered 13/8, 2015 at 11:58 Comment(0)
H
7

you can use like,

final ArrayList<String> strDeviceArr = new ArrayList<>();
      strDeviceArr.add("Select device");
      strDeviceArr.add("Nexus X");
      strDeviceArr.add("Nexus XL");

      spnrDevice.setAdapter(new ArrayAdapter<>(MainActivity.this, R.layout
        .layout_spinner_textview, R.id.textView, strDeviceArr));

      spnrDevice.setSelection(0); //set default selection to 0

      spnrDevice.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
         @Override
         public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String valueSelected = strDeviceArr.get(position);
         }

         @Override
         public void onNothingSelected(AdapterView<?> parent) {

         }
      });
Hybris answered 13/8, 2015 at 11:46 Comment(2)
I tried this but I is not worked for me.I'm using dropdownTrihedral
go through this link it will give you clear idea...mkyong.com/android/android-spinner-drop-down-list-exampleHybris
B
3

For selecting an item from the drop-down: Just after setting adapter

spinner.setAdapter(yourAdapter);
spinner.setSelection(position);
//position is integer starting from 0 to n-1 (n is number of items in the list or array). This will set nth item as the selected and visible item to the user. 
Bronez answered 24/8, 2017 at 6:53 Comment(1)
It should be spinner.setSelection(position) instead of spinner.setSelected(position).Horne
E
0

Use this sequence:

dropdown                = (Spinner) findViewById(R.id.target_dropdown);
List<String> targetList = new ArrayList<>(targetSet);
targetList.add(0, "Select a device");
ArrayAdapter<String> adapter = new ArrayAdapter<>(
  this,
  android.R.layout.simple_spinner_dropdown_item,
  targetList
);
dropdown.setAdapter(adapter);
dropdown.setSelection(0);
Erivan answered 12/9, 2018 at 20:33 Comment(0)
P
-1

Please find below code

 val arrayAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, gender_array)
            genderSpinner!!.adapter = arrayAdapter as SpinnerAdapter?
            **genderSpinner!!.setSelection(2)**
            genderSpinner!!.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
                override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                    select_gender = parent?.getItemAtPosition(position).toString()
                }

                override fun onNothingSelected(parent: AdapterView<*>?) {

                }
            }
Pursue answered 13/8, 2015 at 11:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.