Android Spinner data binding

from the CommonsWare Community archives

At March 25, 2020, 6:46am, dcc88508332 asked:

Is there a way to data-binding current select value of spinner then pass it to button click method.

<layout ... >

    <data>
        <variable
            name="viewModel"
            type="..." />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        ... >

        <Spinner
            android:id="@+id/user_add_type"
            android:entries="@array/types"
            app:onItemSelected="@={viewModel.typePosition}" // two-way binding spinner current select value 
            ... />

        <Button
            android:id="@+id/user_add_submit"
            android:onClick="@{() -> viewModel.addType(userAddType.currentValue)}" // get the current value of spinner and pass it to button method
            ... />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

At March 25, 2020, 11:52am, mmurphy replied:

I have not used Spinner in ~4 years. I do not like two-way data binding, so other than my few book examples showing it, I have skipped it. As a result, I do not have a recipe for two-way data binding of a Spinner.

Sorry!