Search code examples
androidspinner

Android Spinner is unable to scroll


I use Spinner in my app, with keyboard opened.
This Spinner has 9 items (from 1 to 9).
However if keyboard is opened, spinner cannot be scrolled!
Thanks to it, some of items are out of screen, and I cannot select them.

Spinner Item cannot be scrolled


Dialog Layout here:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.material.widget.FloatingEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/fet_productName"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="@dimen/floating_edittext_margin"
        android:layout_marginRight="@dimen/floating_edittext_margin"
        android:hint="@string/product_name"
        android:inputType="text" />
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/fet_productName"
        android:layout_alignLeft="@+id/fet_productName"
        android:layout_alignStart="@+id/fet_productName"
        android:layout_alignRight="@+id/fet_productName"
        android:layout_alignEnd="@+id/fet_productName"
        android:layout_marginTop="@dimen/space_20dp"
        android:id="@+id/linearLayout2">
        <com.material.widget.FloatingEditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/fet_productUnit"
            android:layout_weight="1"
            android:hint="@string/product_unit"
            android:inputType="text"
            android:layout_marginRight="@dimen/space_6dp" />
        <Spinner
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:id="@+id/sP_dialog_productNumber"
            android:entries="@array/spinner_cart_item_number"
            android:layout_marginLeft="@dimen/space_6dp" />
    </LinearLayout>
</RelativeLayout>

Java code here:

public class CartFragment extends Fragment {

    private Spinner spNum;
    private MaterialDialog dialog;
    private static String[] msITEMS;
    private ArrayList<CartItemData> itemDatas;
    private ArrayAdapter<String> strAdapter;

    public CartFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_placeholder_cart, container, false);

        msITEMS = rootView.getContext().getResources().getStringArray(R.array.spinner_dialog_item_number);
        strAdapter = new ArrayAdapter<String>(rootView.getContext(), R.layout.support_simple_spinner_dropdown_item, msITEMS);
        strAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
        final FloatingActionButton fabAdd = (FloatingActionButton)rootView.findViewById(R.id.fabAdd);
        fabAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog = new MaterialDialog.Builder(getActivity())
                    .title(R.string.product_title)
                    .customView(R.layout.dialog_add_cartitem, false)
                    .positiveText(R.string.dialog_positive_add_cartitem)
                    .negativeText(R.string.dialog_negative_add_cartitem)
                    .show();
                View view = dialog.getCustomView();
                dialog.getActionButton(DialogAction.POSITIVE).setEnabled(false);
                spNum = (Spinner)view.findViewById(R.id.sP_dialog_productNumber);
                fetName = (FloatingEditText)view.findViewById(R.id.fet_productName);
                fetUnit = (FloatingEditText)view.findViewById(R.id.fet_productUnit);
                spNum.setAdapter(strAdapter);
            dialog.getActionButton(DialogAction.POSITIVE).setOnClickListener(onPositiveClick());
        }
    });

        return rootView;
    }

    public void onActivityCreated (Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

}


Solution

  • Create a custom scroll:

    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
    import org.holoeverywhere.widget.ListPopupWindow;
    import org.holoeverywhere.widget.ListView;
    import org.holoeverywhere.widget.Spinner;
    import android.content.Context;
    import android.graphics.drawable.Drawable;
    import android.util.AttributeSet;
    import android.view.View;
    
    public class CustomSpinner extends Spinner
    {
    public CustomSpinner(Context context)
    {
        super(context);
    }
    
    public CustomSpinner(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }
    
    public CustomSpinner(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
    }
    
    public CustomSpinner(Context context, AttributeSet attrs, int defStyle, int mode)
    {
        super(context, attrs, defStyle, mode);
    }
    
    public CustomSpinner(Context context, int mode)
    {
        super(context, mode);
    }
    
    @Override
    public boolean performClick()
    {
        boolean bClicked = super.performClick();
    
        try
        {
            Field mPopupField = Spinner.class.getDeclaredField("mPopup");
            mPopupField.setAccessible(true);
            ListPopupWindow pop = (ListPopupWindow) mPopupField.get(this);
            ListView listview = pop.getListView();
    
            Field mScrollCacheField = View.class.getDeclaredField("mScrollCache");
            mScrollCacheField.setAccessible(true);
            Object mScrollCache = mScrollCacheField.get(listview);
            Field scrollBarField = mScrollCache.getClass().getDeclaredField("scrollBar");
            scrollBarField.setAccessible(true);
            Object scrollBar = scrollBarField.get(mScrollCache);
            Method method = scrollBar.getClass().getDeclaredMethod("setVerticalThumbDrawable", Drawable.class);
            method.setAccessible(true);
            method.invoke(scrollBar, getResources().getDrawable(R.drawable.scrollbar_style));
    
            if(VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB)
            {
                Field mVerticalScrollbarPositionField = View.class.getDeclaredField("mVerticalScrollbarPosition");
                mVerticalScrollbarPositionField.setAccessible(true);
                mVerticalScrollbarPositionField.set(listview, SCROLLBAR_POSITION_LEFT);
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    
        return bClicked;
    }
    }
    

    As shown on this other post by End.Fouad