Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set text of spinner before item is selected

I have a spinner with three items and I use an XML string-array resource to feed it. When you open an activity the spinner normally shows the first item that's in the array list. I'd like to change that and show the text "Select one" in the spinner, before an item is selected.

How can I do that?

like image 929
JohnD Avatar asked Jun 03 '12 00:06

JohnD


1 Answers

You can do that one of two ways.

1) Add "Select One" as the first item in your xml and code your listener to ignore that as a selection.

2) Create a custom adapter to insert it as the first line,

EDIT

In your resources

<string-array name="listarray">
    <item>Select One</item>
    <item>Item One</item>
    <item>Item Two</item>
    <item>Item Three</item>
</string-array>

In your onItemSelected Listener:

spinnername.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
    public void onNothingSelected(AdapterView<?> parent) {
    }
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        if (pos == 0) {
        }else {
            // Your code to process the selection
        }
    }
});
like image 148
Barak Avatar answered Sep 20 '22 18:09

Barak