Last active
March 30, 2020 21:14
-
-
Save MohammadSamandari/6e32a7823fdd6b0b80bcfb0656645885 to your computer and use it in GitHub Desktop.
Working With Spinners
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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