this works but when the activity starts it automatically toasts ‘One’ because it is selected by default. How to make it so the spinner box contains a default value that isnt in the actual dialog such as ‘Please choose a catagory’, or at least so that ‘one’ is not auto selected. Thanks
final String[] items = new String[] {"One", "Two", "Three"};
final Spinner catagorySpinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Expense1.this,
android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
catagorySpinner.setAdapter(adapter);
catagorySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), items[position], Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}});
The spinner in Android by default shows the first value in the adapter if nothing is selected as default. There is no way to change it unfortunately.
In your case, you can add
Choose a Categoryto your array:But inside
onItemSelectedyou have to handle this, ie: