0

I have found some threads about this issue, but no solution works in my case. I just want to set the height of the spinner popup e.g. to 200dp, or to limit the displayed items in dropdown popup and make it scrollable.

mainactivity.xml:

 <FrameLayout
        android:id="@+id/frame1"
        android:layout_below="@+id/contact_form_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="@drawable/custom_spn_background">

        <Spinner
            android:id="@+id/spinner1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:dropDownSelector="@color/colorAccent"
            android:popupBackground="@drawable/custombg"
            style="@style/Myspinner"
            android:layout_margin="5dp"
            android:gravity="center_vertical"
            android:spinnerMode="dropdown"/>
    </FrameLayout>

my_spinnerlist.xml

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:paddingStart="8dp"
    android:paddingEnd="8dp"
    android:singleLine="true"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="18sp"
    android:gravity="start"
    android:textColor="#000000"
    android:paddingTop="2dip"
    android:paddingBottom="2dip"
    android:ellipsize="marquee"/>

spinner_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="18sp"
    android:gravity="center_vertical"
    android:textColor="#000000"
    android:paddingStart="8dp"
    android:paddingEnd="8dp"
    android:paddingTop="2dp"
    android:paddingBottom="4dp"
    android:textStyle="normal"
    />

MainActivity.java:

final Spinner sp1 = findViewById(R.id.spinner1);
    String[] arrayItems = myList.categories;

    ArrayAdapter<String> adp1 = new ArrayAdapter<>(this, R.layout.spinner_item, arrayItems);
    adp1.setDropDownViewResource(R.layout.my_spinnerlist);
    sp1.setAdapter(adp1);
    sp1.setSelected(false);
    sp1.setSelection(0, true);

    sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
        }
    });

I tried to set 200dp height in all spinner related XML layout files, but it affects only the item height, not the whole popup. I also tried a suggested solution from here with Reflection, example:

Spinner spinner = (Spinner) findViewById(R.id.spinner);
    try {
        Field popup = Spinner.class.getDeclaredField("mPopup");
        popup.setAccessible(true);

        android.widget.ListPopupWindow popupWindow = (android.widget.ListPopupWindow) popup.get(spinner);

        popupWindow.setHeight(200);
    }
    catch (NoClassDefFoundError | ClassCastException | NoSuchFieldException | IllegalAccessException e) {
    }

but this did not change the height of the dropdown list and also I have a warning : Reflective access to mPopup, which is not part of the public SDK and therefore likely to change in future Android releases.

I also tried to set <item name="android:dropDownHeight">200dp</item> in Myspinner style in styles file, but it did not affect the height.

I really don't have any solution for this. I have a list of 50 items in my dropdown and it overlaps the whole display area of my device.

Darksymphony
  • 2,155
  • 30
  • 54
  • You'd use `AutoCompleteTextView` instead of the spinner and set `android:dropDownHeight` – Zain Mar 24 '22 at 00:02
  • it is not what I want, because the user doesn't see the options. It shows only the options beginning with the letters user enters. As my dropdown has generated content, user doesn't know what to type. The solution below with Material spinner solved the issue perfectly. – Darksymphony Mar 26 '22 at 15:25

1 Answers1

1

Ok, the only solution that works for me is to use third party MaterialSpinner library. Maybe it will be useful also for others as there is no native way to change the dropdown height.

Just to implement: implementation 'com.jaredrummler:material-spinner:1.3.1'

The spinner.xml:

 <com.jaredrummler.materialspinner.MaterialSpinner
            android:id="@+id/spinner1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:ms_dropdown_max_height="200dp"
            android:dropDownSelector="@color/colorAccent"
            android:popupBackground="@drawable/custombg"
            style="@style/Myspinner"
            android:layout_margin="5dp"
            android:gravity="center_vertical"
            android:spinnerMode="dropdown"/>

Java code:

 final MaterialSpinner sp1 = (MaterialSpinner) findViewById(R.id.spinner1);

        String[] arrayItems = myList.categories;

        ArrayAdapter<String> adp1 = new ArrayAdapter<>(this, R.layout.spinner_item, arrayItems);
        adp1.setDropDownViewResource(R.layout.my_spinnerlist);
        sp1.setAdapter(adp1);

        sp1.setSelected(false);
 
        sp1.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(MaterialSpinner view, int position, long id, Object item) {

            }
        } );

Works like a charm, see the app:ms_dropdown_max_height="200dp" in XML file.

Darksymphony
  • 2,155
  • 30
  • 54