How do I set the maximum length for a spinner's drop-down list?
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).
I've searched quite a bit and it seems that I can't find a solution to this anywhere. You may have to go a slightly different route by recreating the functionality of a spinner, but using buttons and dialogs intead.
Assuming you're using an ArrayAdapter
for the spinner, try this:
Replace your Spinner with a button that has a down arrow icon.
Create a custom Dialog
or AlertDialog
and populate a ListView
with the ArrayAdapter
.
For the onClick
method, push the value of the selected option and populate a static
variable and the text of the button with the chosen value. This way, you can make the size of the dialog box any size you want and the excess choices will always be scrollable.
I know it's not ideal, but I haven't found any way to change the height of the "dropdown" portion of a spinner.
Sorry if this wasn't what you were hoping for.
EDIT: Someone else asked the exact same question here before and it turns out they went with the exact method I just described. You can see the sample code they used here: Check out the question/code