I am using a spinner whose adapter is dynamically populated.
I can't find a solution to prevent the default behavior (i.e. showing the dropdown for only one item on a click). Any ideas on how to do this? Thanks
hm... try to use setClickable(fasle)
or setEnabled(false)
if only one item in spinner
.
Try this
public class SpinnerActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
List<String> list = new ArrayList<String>();
list.add("list 1");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
if (list.size() < 2) {
spinner.setClickable(false);
spinner.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
Toast.makeText(SpinnerActivity.this, "Catch it!", Toast.LENGTH_SHORT).show();
}
return true;
}
});
}
}
}
To prevent spinner from showing drop-down list, I only return true in onTouch(...), like this:
spinner.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return true;
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With