I am using default android spinner in my application. how to reduce the height of drop down as default methods are not working?
Asked
Active
Viewed 665 times
3 Answers
0
check this answer
Spinner spinner = (Spinner) findViewById(R.id.spinner);
try {
Field popup = Spinner.class.getDeclaredField("mPopup");
popup.setAccessible(true);
// Get private mPopup member variable and try cast to ListPopupWindow
android.widget.ListPopupWindow popupWindow = (android.widget.ListPopupWindow) popup.get(spinner);
// Set popupWindow height to 500px
popupWindow.setHeight(500);
}
catch (NoClassDefFoundError | ClassCastException | NoSuchFieldException | IllegalAccessException e) {
// silently fail...
}

Moustafa Shahin
- 132
- 7
-
First of all thanks for your kind response, @MoustafaShahin but tried above code but not working, any other alternative then please suggest. – Ayush Vyas Aug 17 '20 at 05:36
0
check this answer
In my solution you put recycleview in spinner and then you put all the data to this rv, you can modify everything you want of the recycleview

Daniel
- 53
- 7
-
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/32629741) – mymiracl Sep 07 '22 at 10:53
-
-1
By Using Refelection you can set height of dropdown
Spinner spinner = (Spinner) findViewById(R.id.spinner);
try {
Field popup = Spinner.class.getDeclaredField("mPopup");
popup.setAccessible(true);
// Get private mPopup member variable and try cast to ListPopupWindow
android.widget.ListPopupWindow popupWindow = (android.widget.ListPopupWindow) popup.get(spinner);
// Set popupWindow height to 500px
popupWindow.setHeight(500);
}
catch (NoClassDefFoundError | ClassCastException | NoSuchFieldException | IllegalAccessException e) {
// silently fail...
}
If above does not work then Create class extend Spinner
class overriding its getWindowVisibleDisplayFrame(Rect outRect)
which is used by android.widget.PopupWindow
for calculations. Just set outRect to limit the area where drop down view can be displayed.
@Override
public void getWindowVisibleDisplayFrame(Rect outRect) {
WindowManager wm = (WindowManager) getContext.getSystemService(Context.WINDOW_SERVICE);
Display d = wm.getDefaultDisplay();
d.getRectSize(outRect);
outRect.set(outRect.left, <STATUS BAR HEIGHT>, outRect.right, outRect.bottom);
}

chand mohd
- 2,363
- 1
- 14
- 27
-
Thanks for your kind response @chandmohd but tried above code not working, I couldn't find any linking of the above Override method to the spinner, how should I connect the above code of override method with spinner, what would be the reference. Please suggest – Ayush Vyas Aug 17 '20 at 05:39
-