Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set default value to spinner dropdown in Android?

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);
like image 709
Nuwan Indika Avatar asked Aug 13 '15 11:08

Nuwan Indika


2 Answers

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);
like image 166
Prerna Jain Avatar answered Oct 31 '22 08:10

Prerna Jain


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) {

         }
      });
like image 45
Amit Vaghela Avatar answered Oct 31 '22 07:10

Amit Vaghela