Android limit the no of items displayed in a spinner's dropdown list
Asked Answered
S

3

14

I have a spinner item bound to an array adapter which might have 0 or more items at any time. I want the spinner dropdown list to only show three items at a time, the rest of the items being scrollable. I have tried wrapping the spinner within a layout with a fixed width but the spinner drop down list still takes up the entire screen(if there are that many items in the array adapter) to display the list.

Stover answered 27/11, 2013 at 9:30 Comment(2)
I don't think there is a simple way way to do that without customizing your adapter. But maybe this can help: github.com/ankitthakkar/DropDownSpinnerDisenchant
how do you do it by customizing the adapter? i don't wanna make my own custom spinner item using a button and a list view as done in the list.Stover
D
6

I was looking at the Spinner source code and it seems like you can't do that with a spinner.

The Spinner has its own private interface called SpinnerPopup which defines how dropdown items can be shown. This is currently based on the spinnerMode allowing for a dropdown or dialog list.

Both options are also implemented inside the Spinner class as private classes: DialogPopup and DropdownPopup. Since you can't access them, it seems to me your only options at this point are:

  1. Implement your own custom spinner based on other widgets such as in this example.
  2. Copy the code from the Spinner class which seems pretty self-contained and implement your version of a spinner with it, modifying whatever you like in it.

I'm sorry I couldn't be of more help.

Good luck!

EDIT:

If you choose option 2, I think all you need to do is add your mode implementing the SpinnerPopup interface. Then inside the constructor Spinner(Context context, AttributeSet attrs, int defStyle, int mode) add another case to the switch checking for the modes to instantiate your own popup. Doesn't seem hard.

Disenchant answered 27/11, 2013 at 10:36 Comment(6)
i think i will go with the second option..sounds easier!Stover
Come back here with the results later. :)Disenchant
where did you download the source code?i used the following linkhttp://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/widget/Spinner.java(realized later on that it was the source code from way back 2009)..removed almost everything that showed error and in the onClick function i set the width and height of the spinner dialog box(no popup in android 1.5) which worked!Stover
Cool!! I got the code from the SDK manager, you can download it for the different API versions there. Then you just have to attach it: https://mcmap.net/q/591018/-using-adt-and-sdk-manager-attach-android-source-code-to-eclipseDisenchant
Hello pls help.. I am using same lib Is there any way to setText to spinner onItemClickCounterrevolution
_state = state[position]; System.out.println(_state+">>>>>>>>>>>>>"); _select_state_btn.setText(_state);Counterrevolution
D
2

This answer doesn't work anymore, please look elsewhere.


Here's the ultimately simple solution.. just copy this line in Spinner tag...

android:dropDownHeight="100dp"    <!--change 100dp to your requirement-->

you can also modify width...

android:dropDownWidth="100dp"   <!--change 100dp to your requirement-->

and you know what it works on AutoCompleteTextView too and with atleast api 16...

Duplicature answered 25/7, 2017 at 18:3 Comment(3)
This is very bad solution because font sizes can rendered different sizes in different phones , height must not be hard coded consider using my solution : https://mcmap.net/q/589435/-android-limit-the-no-of-items-displayed-in-a-spinner-39-s-dropdown-listHobbledehoy
android:dropDownHeight has no effectSherlock
android:dropDownHeight doesn't supported it seemsOlshausen
H
0

The recommended solutions are not really interesting because they hard code the height of drop down this is bad because font size are actually different in different phones, I handled it dynamic and reliable like this :

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                R.layout.drop_down_text_view,
                new String[]{"A", "B", "C", "D", "E"}) {

            @NonNull
            @Override
            public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
                TextView dropDownTextView = (TextView) super.getView(position, convertView, parent);

                dropDownTextView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        myDropDownMenuOrSpinnerOrAutoCompleteTextView.setDropDownHeight(dropDownTextView.getHeight() * 3);

                        dropDownTextView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    }
                });
                return dropDownTextView;
            }
        };
Hobbledehoy answered 16/5, 2019 at 7:42 Comment(2)
I couldn't get this to work as I couldn't find a setDropDownHeight method on any type of related view. Did you create a custom spinner class?Lorusso
@Lorusso Just set it to your spinner height. I was using autoCompleteTextView which had this method.Hobbledehoy

© 2022 - 2024 — McMap. All rights reserved.