Search code examples
javaandroidspinnerandroid-wheel

How to get selected text using android spinner wheel?


I am using Android Spinner Wheel and I already set code for that.

AbstractWheel wheelHorizontalView1 = (AbstractWheel) findViewById(R.id.wheelHorizontalView1);
NumericWheelAdapter minAdapter = new NumericWheelAdapter(this, 1, 15,"%01d");
minAdapter.setItemResource(R.layout.wheel_text_centered_dark_back);
minAdapter.setItemTextResource(R.id.text);
wheelHorizontalView1.setViewAdapter(minAdapter);

Here is how it looks like:

spinner wheel image

So, my question is how to get 3 as my selected text ?

Here is xml if needed,

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="20dp">

    <View
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="65dp"
        android:background="#007704" />

    <com.spinnerwheel.WheelHorizontalView
        android:id="@+id/wheelHorizontalView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        app:selectionDivider="@android:color/white"
        app:selectionDividerWidth="2dp"
        app:visibleItems="6" />
</RelativeLayout>

Solution

  • You should call setCurrentItem(int index) function:

    wheelHorizontalView1.setCurrentItem(2);
    

    If you want to know when the user changed the value you have to implement this listener.

        wheelHorizontalView1.addChangingListener(changedListener);
        ...
        ...  
        // Wheel changed listener
        private OnWheelChangedListener changedListener = new OnWheelChangedListener() {
            public void onChanged(AbstractWheel wheel, int oldValue, int newValue) {
                // newValue is the currently selected item
            }
        };