Skip to content

Instantly share code, notes, and snippets.

@MohammadSamandari
Last active March 30, 2020 21:14
Show Gist options
  • Save MohammadSamandari/6e32a7823fdd6b0b80bcfb0656645885 to your computer and use it in GitHub Desktop.
Save MohammadSamandari/6e32a7823fdd6b0b80bcfb0656645885 to your computer and use it in GitHub Desktop.
Working With Spinners
// A Spinner provides a drop-down menu:
// Add a Spinner to the layout. Use an ArrayAdapter to assign an array of text values as the Spinner menu items.
// Implement the AdapterView.OnItemSelectedListener interface, which requires also adding the onItemSelected() and
// onNothingSelected() callback methods to activate the Spinner and its listener.
// Use the onItemSelected() callback method to retrieve the selected item in the Spinner menu using getItemAtPosition().
final ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.spinner_values, android.R.layout.simple_spinner_dropdown_item);
final Spinner spinner = findViewById(R.id.spinner);
if (spinner != null) {
spinner.setAdapter(adapter);
}
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected (AdapterView<?> parent, View view, int position, long id) {
Log.d(TAG, "onItemClick: " + parent.getItemAtPosition(position).toString());
}
@Override
public void onNothingSelected (AdapterView<?> parent) {
Log.d(TAG, "onNothingSelected: nothing Selected");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment