0

I am using default android spinner in my application. how to reduce the height of drop down as default methods are not working?

James Z
  • 12,209
  • 10
  • 24
  • 44

3 Answers3

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...
}
  • 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
  • Okey, thanks for advice! Next time I will do it properly – Daniel Sep 07 '22 at 10:56
-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
  • overridden getWindowVisibleDisplayFrame() never called – Andrew Glukhoff Dec 15 '20 at 11:16