Spinner
Layout
<Spinner
android:id="@+id/label_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_margin="8dp"/>
Implement OnItemSelected
public class Widgets extends AppCompatActivity implements
AdapterView.OnItemSelectedListener{
String array
<string-array name="labels_array">
<item>Home</item>
<item>Work</item>
<item>Mobile</item>
<item>Other</item>
</string-array>
Java code
Spinner spinner = findViewById(R.id.label_spinner);
spinner.setOnItemSelectedListener(this);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.labels_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource
(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
Overriding methods
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String spinner_item = parent.getItemAtPosition(position).toString();
Toast.makeText(this, spinner_item, Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
Last updated