@@ -164,6 +164,11 @@ public class NumberPicker extends LinearLayout {
164164 '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9'
165165 };
166166
167+ /**
168+ * Constant for unspecified size.
169+ */
170+ private static final int SIZE_UNSPECIFIED = -1 ;
171+
167172 /**
168173 * Use a custom NumberPicker formatting callback to use two-digit minutes
169174 * strings like "01". Keeping a static formatter etc. is the most efficient
@@ -542,16 +547,20 @@ public NumberPicker(Context context, AttributeSet attrs, int defStyle) {
542547 getResources ().getDisplayMetrics ());
543548 mSelectionDividerHeight = attributesArray .getDimensionPixelSize (
544549 R .styleable .NumberPicker_selectionDividerHeight , defSelectionDividerHeight );
545- mMinHeight = attributesArray .getDimensionPixelSize (R .styleable .NumberPicker_minHeight , 0 );
550+ mMinHeight = attributesArray .getDimensionPixelSize (R .styleable .NumberPicker_minHeight ,
551+ SIZE_UNSPECIFIED );
546552 mMaxHeight = attributesArray .getDimensionPixelSize (R .styleable .NumberPicker_maxHeight ,
547- Integer .MAX_VALUE );
548- if (mMinHeight > mMaxHeight ) {
553+ SIZE_UNSPECIFIED );
554+ if (mMinHeight != SIZE_UNSPECIFIED && mMaxHeight != SIZE_UNSPECIFIED
555+ && mMinHeight > mMaxHeight ) {
549556 throw new IllegalArgumentException ("minHeight > maxHeight" );
550557 }
551- mMinWidth = attributesArray .getDimensionPixelSize (R .styleable .NumberPicker_minWidth , 0 );
558+ mMinWidth = attributesArray .getDimensionPixelSize (R .styleable .NumberPicker_minWidth ,
559+ SIZE_UNSPECIFIED );
552560 mMaxWidth = attributesArray .getDimensionPixelSize (R .styleable .NumberPicker_maxWidth ,
553- Integer .MAX_VALUE );
554- if (mMinWidth > mMaxWidth ) {
561+ SIZE_UNSPECIFIED );
562+ if (mMinWidth != SIZE_UNSPECIFIED && mMaxWidth != SIZE_UNSPECIFIED
563+ && mMinWidth > mMaxWidth ) {
555564 throw new IllegalArgumentException ("minWidth > maxWidth" );
556565 }
557566 mComputeMaxWidth = (mMaxWidth == Integer .MAX_VALUE );
@@ -746,10 +755,10 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
746755 final int newHeightMeasureSpec = makeMeasureSpec (heightMeasureSpec , mMaxHeight );
747756 super .onMeasure (newWidthMeasureSpec , newHeightMeasureSpec );
748757 // Flag if we are measured with width or height less than the respective min.
749- final int desiredWidth = Math . max (mMinWidth , getMeasuredWidth ());
750- final int desiredHeight = Math . max ( mMinHeight , getMeasuredHeight () );
751- final int widthSize = resolveSizeAndState ( desiredWidth , newWidthMeasureSpec , 0 );
752- final int heightSize = resolveSizeAndState ( desiredHeight , newHeightMeasureSpec , 0 );
758+ final int widthSize = resolveSizeAndStateRespectingMinSize (mMinWidth , getMeasuredWidth (),
759+ widthMeasureSpec );
760+ final int heightSize = resolveSizeAndStateRespectingMinSize ( mMinHeight , getMeasuredHeight (),
761+ heightMeasureSpec );
753762 setMeasuredDimension (widthSize , heightSize );
754763 }
755764
@@ -1243,6 +1252,7 @@ public void setDisplayedValues(String[] displayedValues) {
12431252 }
12441253 updateInputTextView ();
12451254 initializeSelectorWheelIndices ();
1255+ tryComputeMaxWidth ();
12461256 }
12471257
12481258 @ Override
@@ -1368,6 +1378,9 @@ public void sendAccessibilityEvent(int eventType) {
13681378 * @return A measure spec greedily imposing the max size.
13691379 */
13701380 private int makeMeasureSpec (int measureSpec , int maxSize ) {
1381+ if (maxSize == SIZE_UNSPECIFIED ) {
1382+ return measureSpec ;
1383+ }
13711384 final int size = MeasureSpec .getSize (measureSpec );
13721385 final int mode = MeasureSpec .getMode (measureSpec );
13731386 switch (mode ) {
@@ -1382,6 +1395,26 @@ private int makeMeasureSpec(int measureSpec, int maxSize) {
13821395 }
13831396 }
13841397
1398+ /**
1399+ * Utility to reconcile a desired size and state, with constraints imposed by
1400+ * a MeasureSpec. Tries to respect the min size, unless a different size is
1401+ * imposed by the constraints.
1402+ *
1403+ * @param minSize The minimal desired size.
1404+ * @param measuredSize The currently measured size.
1405+ * @param measureSpec The current measure spec.
1406+ * @return The resolved size and state.
1407+ */
1408+ private int resolveSizeAndStateRespectingMinSize (int minSize , int measuredSize ,
1409+ int measureSpec ) {
1410+ if (minSize != SIZE_UNSPECIFIED ) {
1411+ final int desiredWidth = Math .max (minSize , measuredSize );
1412+ return resolveSizeAndState (desiredWidth , measureSpec , 0 );
1413+ } else {
1414+ return measuredSize ;
1415+ }
1416+ }
1417+
13851418 /**
13861419 * Resets the selector indices and clear the cached
13871420 * string representation of these indices.
0 commit comments