Implementing events Spinner with Butter Knife
Asked Answered
D

1

10

First: We need to reference the spinner

@Bind(R.id.field_type_id)
Spinner mTypeIdSpinner;

Second: Create string array

<string-array name="type_id_array">
    <item>One</item>
    <item>Two</item>
    <item>Three</item>
    <item>Four</item>
    <item>Five</item>
</string-array>

Third: Load the adapter in your activity (onCreate method for example)

private void loadSpinnerIdTypes() {
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.type_id_array, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mTypeIdSpinner.setAdapter(adapter);
}

Fourth: Listen the events

@OnItemSelected(R.id.field_type_id)
void onItemSelected(int position) {
    Timber.d("Element selected %s ", mTypeIdSpinner.getItemAtPosition(position));
}
Davit answered 6/6, 2016 at 0:58 Comment(0)
M
41

You can use @OnItemSelected with method arguments Spinner and int see example:

@OnItemSelected(R.id.my_spinner)
public void spinnerItemSelected(Spinner spinner, int position) {
    // code here
}

(works with ButterKnife v. 7.0.1)

Micky answered 11/6, 2016 at 21:8 Comment(1)
Also works with ButterKnife v6.1.0 (just confirmed with a test in my device)Womera

© 2022 - 2024 — McMap. All rights reserved.