Search code examples
androidheightspinner

How do I set the maximum length for a spinner's drop-down list?


I have a spinner which currently obscures some text below the spinner when opened. I need to limit the maximum drop-down length of the spinner, either through java code or through XML, so that it does not obscure this text.

The current design is the left example while the desired design is on the right. explanation

How do I go about limiting how far the spinner drops down to when opened? At present, it drops down to fill the entire portion of screen below it.


Solution

  • One way to achieve this is to use ActionBarSherlock's IcsSpinner. I made the needed modifications to limit the size of the spinner and that seems to work nicely.

    Make the following modifications to IcsListPopupWindow.

    Add an instance variable:

    private int mDropDownVerticalOffsetBottom;
    

    Add a method to set this variable:

    public void setVerticalOffsetBottom(int offsetBottom) {
        mDropDownVerticalOffsetBottom = offsetBottom;
    }
    

    Alter the call to getMaxAvailableHeight to (mDropDownVerticalOffsetBottom was added):

    final int maxHeight = /*mPopup.*/getMaxAvailableHeight(
            mDropDownAnchorView, mDropDownVerticalOffset, mDropDownVerticalOffsetBottom, ignoreBottomDecorations);
    

    Change the method's signature to include that variable:

    private int getMaxAvailableHeight(View anchor, int yOffset, int yOffsetBottom, boolean ignoreBottomDecorations) {
    

    And consider that offset when computing the distance to the bottom:

    final int distanceToBottom = bottomEdge - (anchorPos[1] + anchor.getHeight()) - yOffset - yOffsetBottom;
    

    Now modify IcsSpinner.java to implement the setter method for the offset:

    private DropdownPopup mPopup;   // <- needs to be a DropdownPopup instead of a SpinnerPopup
    
    public void setVerticalOffsetBottom(int offsetBottom) {
        mPopup.setVerticalOffsetBottom(offsetBottom);
    }
    

    Now "all" you need to do is to set the offset to the correct value. Here's how I did it (I tested it and it worked on two test devices):

    final View root = findViewById(R.id.root_layout);
    final View view = findViewById(R.id.view_not_to_be_obscured);
    root.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        public void onGlobalLayout() {
            root.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            int[] locations = new int[2];
            root.getLocationOnScreen(locations);
            int y1 = locations[1];
            int h1 = root.getHeight();
            view.getLocationOnScreen(locations);
            int y2 = locations[1];
            int offset = y1 + h1 - y2;
            // here we initialize the spinner
        }
    });
    

    The assumption is that root_layout fills the whole window excluding all decorating elements.

    The final step is to create the spinner itself:

        // actionDropDownStyle is an attribute set in the theme to e.g. @style/Widget.Sherlock.Spinner.DropDown.ActionBar or @style/Widget.Sherlock.Light.Spinner.DropDown.ActionBar for light theme
        IcsSpinner spinner = new IcsSpinner(context, null, R.attr.actionDropDownStyle);
    
        // yes here we set the offset!
        spinner.setVerticalOffsetBottom(offset);
    
        spinner.setPadding(spinner.getPaddingLeft(), 0, spinner.getPaddingRight(), 0);
        spinner.setId(R.id.some_id);
        spinner.setAdapter(yourAdapter); // you can use a simple ArrayAdapter or whatever you need
    
        // create ICS LinearLayout
        LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        IcsLinearLayout linearLayout = (IcsLinearLayout) inflater.inflate(R.layout.abs__action_bar_tab_bar_view, null);
        linearLayout .setPadding(listNavLayout.getPaddingLeft(), 0, listNavLayout.getPaddingRight(), 0);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        params.gravity = Gravity.CENTER;
        linearLayout .addView(spinner, params);
    

    Now that might look complicated but as someone else mentioned, you'll not be able to achieve this without your own spinner implementation and since ActionBarSherlock comes with one, why not use it? It's certainly less work than writing your own one. If you don't use the library for the ActionBar strip away all resource files you don't need and use Proguard to strip away all unused classes. You could probably achieve the same using AppCompat (see here: https://github.com/android/platform_frameworks_support/tree/master/v7/appcompat/src/android/support).