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.