@@ -61,6 +61,8 @@ public class MaterialSpinner extends TextView {
6161 private Drawable arrowDrawable ;
6262 private boolean hideArrow ;
6363 private boolean nothingSelected ;
64+ private int popupWindowMaxHeight ;
65+ private int popupWindowHeight ;
6466 private int selectedIndex ;
6567 private int backgroundColor ;
6668 private int arrowColor ;
@@ -93,6 +95,9 @@ private void init(Context context, AttributeSet attrs) {
9395 textColor = ta .getColor (R .styleable .MaterialSpinner_ms_text_color , defaultColor );
9496 arrowColor = ta .getColor (R .styleable .MaterialSpinner_ms_arrow_tint , textColor );
9597 hideArrow = ta .getBoolean (R .styleable .MaterialSpinner_ms_hide_arrow , false );
98+ popupWindowMaxHeight = ta .getDimensionPixelSize (R .styleable .MaterialSpinner_ms_dropdown_max_height , 0 );
99+ popupWindowHeight = ta .getLayoutDimension (R .styleable .MaterialSpinner_ms_dropdown_height ,
100+ WindowManager .LayoutParams .WRAP_CONTENT );
96101 arrowColorDisabled = Utils .lighter (arrowColor , 0.8f );
97102 } finally {
98103 ta .recycle ();
@@ -183,7 +188,7 @@ private void init(Context context, AttributeSet attrs) {
183188
184189 @ Override protected void onMeasure (int widthMeasureSpec , int heightMeasureSpec ) {
185190 popupWindow .setWidth (MeasureSpec .getSize (widthMeasureSpec ));
186- popupWindow .setHeight (WindowManager . LayoutParams . WRAP_CONTENT );
191+ popupWindow .setHeight (calculatePopupWindowHeight () );
187192 super .onMeasure (widthMeasureSpec , heightMeasureSpec );
188193 }
189194
@@ -411,6 +416,40 @@ private void animateArrow(boolean shouldRotateUp) {
411416 animator .start ();
412417 }
413418
419+ /**
420+ * Set the maximum height of the dropdown menu.
421+ *
422+ * @param height
423+ * the height in pixels
424+ */
425+ public void setDropdownMaxHeight (int height ) {
426+ popupWindowMaxHeight = height ;
427+ popupWindow .setHeight (calculatePopupWindowHeight ());
428+ }
429+
430+ /**
431+ * Set the height of the dropdown menu
432+ *
433+ * @param height
434+ * the height in pixels
435+ */
436+ public void setDropdownHeight (int height ) {
437+ popupWindowHeight = height ;
438+ popupWindow .setHeight (calculatePopupWindowHeight ());
439+ }
440+
441+ private int calculatePopupWindowHeight () {
442+ float listViewHeight = adapter .getCount () * getResources ().getDimension (R .dimen .ms__item_height );
443+ if (popupWindowMaxHeight > 0 && listViewHeight > popupWindowMaxHeight ) {
444+ return popupWindowMaxHeight ;
445+ } else if (popupWindowHeight != WindowManager .LayoutParams .MATCH_PARENT
446+ && popupWindowHeight != WindowManager .LayoutParams .WRAP_CONTENT
447+ && popupWindowHeight <= listViewHeight ) {
448+ return popupWindowHeight ;
449+ }
450+ return WindowManager .LayoutParams .WRAP_CONTENT ;
451+ }
452+
414453 /**
415454 * Get the {@link PopupWindow}.
416455 *
0 commit comments