This is the spinner Element with custom dialog mode and whitout promt:
<Spinner
android:id="@+id/spinner_metatag"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/search_adv"
/>
The Array element where the default value is putted at position 0:
<string-array name="search_adv">
<item>@string/search_adv_prompt</item>
<item>@string/search_adv_title</item>
<item>@string/search_adv_desc</item>
<item>@string/search_adv_autore</item>
....
</string-array>
The String elements for the array with the default value:
<string name="search_adv_prompt">Scegli un metatag</string> <!-- Default value-->
<string name="search_adv_title">Titolo</string>
<string name="search_adv_desc">Descrizione</string>
<string name="search_adv_autore">Autore</string>
...
And here the code to prevent the event fired on onCreateMethod and the work around to permit to select the same element already selected:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_adv_main);
spinner = (Spinner) findViewById(R.id.spinner_metatag);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
//prevent onCreate event fire and the loop
if(pos==0)
return;
//HERE YOUR CODE
//at the end move to the default element the spinner
spinner.setSelection(0);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {}
});
}
Hope helps. The idea come from the second solution from TreKing's answer