4

I have a spinner, that uses an array from strings.xml

if the array has 5 strings (1,2,3,4,5), and i want the spinner to show second string (2) as default value, is this possible? I know i can re-arrange the strings order so that first one is 2, but this doesn't look very good if spinner dialog appears as (2,1,3,4,5).

Or does the array have to be created within my activity programatically and then use setPostion()? I have tried this, but get a fault when creating array in activity. Can anyone please give me an example of how to create array and use it in spinner()

I have also searched on here for answers but cant seem to find what i need.

Thanks for looking....

2 Answers 2

12

I recommend you check: http://developer.android.com/resources/tutorials/views/hello-spinner.html

You should create an ArrayAdapter in your Activity.

From the above link:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Spinner spinner = (Spinner) findViewById(R.id.spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.planets_array, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
}

You can use

spinner.setSelection(adapter.getPosition(value)));

to set the position.

Sign up to request clarification or add additional context in comments.

Comments

3

Cheers for reply.....

Didn't do what i needed, but have managed to solve my problem as follows..

First i created an array within my activity, instead of strings.xml.

    String[] NoCore_Array = new String [5];
{ 
    NoCore_Array[0] = "1";
    NoCore_Array[1] = "2";
    NoCore_Array[2] = "3";
    NoCore_Array[3] = "4";
    NoCore_Array[4] = "5";

}

Then i created the spinner using...

    Spinner spnNoCore = (Spinner) findViewById(R.id.spinner_nocore);

Then created the adapter, using above array....

    ArrayAdapter NoCoreAdapter =  new ArrayAdapter(this, 
    android.R.layout.simple_spinner_item,  NoCore_Array);
    spnNoCore.setAdapter(NoCoreAdapter);

Then set default position of the adapter as follows...

    //Set Default Selection
    spnNoCore.setSelection(1);

Then rest of spinner code for actions...

     spnNoCore.setOnItemSelectedListener(new OnItemSelectedListener(){

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

            //Get item from spinner and store in string conductorSize........
            NoCore = parent.getItemAtPosition(pos).toString();

            if (NoCore.equals(NoCore1))   { CoreNo = 1    ;  }
            if (NoCore.equals(NoCore2))   { CoreNo = 2    ;  }
            if (NoCore.equals(NoCore3))   { CoreNo = 3    ;  }
            if (NoCore.equals(NoCore4))   { CoreNo = 4    ;  }
            if (NoCore.equals(NoCore5))   { CoreNo = 5    ;  }

     }

        public void onNothingSelected(AdapterView parent) {
          // Do nothing.
        }
    });

Hope this may help other people who are having same problem with setting default selection on Spinner.

However the layout of the dialog is not as good when done this way, Radio buttons are missing, just looks like a text selection with lines dividing the sections.

2 Comments

thanx man , u dont know how much work u just saved me , i was trying so many useless solutions , like creating rotating arrays
@Coops5575, the fact that the dialog in your solution does not have radio buttons is due to you creating the adapter with simple_spinner_item instead if simple_spinner_dropdown_item, that's all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.