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.
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.
I think you have to use custom spinner for your requirement .There is not any inbuilt method or xml property that can help.
See this example for help .
https://github.com/ankitthakkar/DropDownSpinner
Hope this will work.
Note: This is changed answer.
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
orAlertDialog
and populate aListView
with theArrayAdapter
. For theonClick
method, push the value of the selected option and populate astatic
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
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:
Add a method to set this variable:
Alter the call to getMaxAvailableHeight to (mDropDownVerticalOffsetBottom was added):
Change the method's signature to include that variable:
And consider that offset when computing the distance to the bottom:
Now modify IcsSpinner.java to implement the setter method for the offset:
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):
The assumption is that root_layout fills the whole window excluding all decorating elements.
The final step is to create the spinner itself:
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).