diff options
| author | Svetoslav Ganov <svetoslavganov@google.com> | 2011-10-31 15:52:55 -0700 |
|---|---|---|
| committer | Svetoslav Ganov <svetoslavganov@google.com> | 2011-11-01 14:48:34 -0700 |
| commit | ec1e06a00d26a783fb948e739ad8157c03e55302 (patch) | |
| tree | 517c75471a4de5a41f5456437522577fd8d34777 /core/java/android/widget/NumberPicker.java | |
| parent | 2333a02e152616c4b982c149ea2a5c9e9615e274 (diff) | |
Updating NumberPicker, TimePicker, DatePicker to fit different screen and font sizes.
1. Now the NumberPicker has minWidth/minHeight that is the lower bound
of the correspodning size for which the widget looks well enough to
be usable. There is also maxWidth/masHeight that is the upper bound
of the corresponding size for which the widget looks best. The picker
tries to greedily reach the max dimesions for which it looks best.
2. The NumberPicker was not taking care of the max width of the items
is shows numbers/strings mapped to numbers. Now if not explicitly
specified the widget computes the maxWidth at which it looks best
based on the content it shows.
3. Removed an unnecessary layout for number picker on tablets.
4. Updated the TimePicker/DatePicker to not hard-code width for the
number pickers it uses, rahter wrap the content.
bug:5417100
Change-Id: I432aa96185961e59a058a2565b15265ba7394818
Diffstat (limited to 'core/java/android/widget/NumberPicker.java')
| -rw-r--r-- | core/java/android/widget/NumberPicker.java | 172 |
1 files changed, 116 insertions, 56 deletions
diff --git a/core/java/android/widget/NumberPicker.java b/core/java/android/widget/NumberPicker.java index 320c65045894..5ab99dcfa184 100644 --- a/core/java/android/widget/NumberPicker.java +++ b/core/java/android/widget/NumberPicker.java @@ -203,6 +203,11 @@ public class NumberPicker extends LinearLayout { private final EditText mInputText; /** + * The min height of this widget. + */ + private final int mMinHeight; + + /** * The max height of this widget. */ private final int mMaxHeight; @@ -210,7 +215,17 @@ public class NumberPicker extends LinearLayout { /** * The max width of this widget. */ - private final int mMaxWidth; + private final int mMinWidth; + + /** + * The max width of this widget. + */ + private int mMaxWidth; + + /** + * Flag whether to compute the max width. + */ + private final boolean mComputeMaxWidth; /** * The height of the text. @@ -527,8 +542,19 @@ public class NumberPicker extends LinearLayout { getResources().getDisplayMetrics()); mSelectionDividerHeight = attributesArray.getDimensionPixelSize( R.styleable.NumberPicker_selectionDividerHeight, defSelectionDividerHeight); - mMaxHeight = attributesArray.getDimensionPixelSize(R.styleable.NumberPicker_maxHeight, 0); - mMaxWidth = attributesArray.getDimensionPixelSize(R.styleable.NumberPicker_maxWidth, 0); + mMinHeight = attributesArray.getDimensionPixelSize(R.styleable.NumberPicker_minHeight, 0); + mMaxHeight = attributesArray.getDimensionPixelSize(R.styleable.NumberPicker_maxHeight, + Integer.MAX_VALUE); + if (mMinHeight > mMaxHeight) { + throw new IllegalArgumentException("minHeight > maxHeight"); + } + mMinWidth = attributesArray.getDimensionPixelSize(R.styleable.NumberPicker_minWidth, 0); + mMaxWidth = attributesArray.getDimensionPixelSize(R.styleable.NumberPicker_maxWidth, + Integer.MAX_VALUE); + if (mMinWidth > mMaxWidth) { + throw new IllegalArgumentException("minWidth > maxWidth"); + } + mComputeMaxWidth = (mMaxWidth == Integer.MAX_VALUE); attributesArray.recycle(); mShowInputControlsAnimimationDuration = getResources().getInteger( @@ -677,37 +703,33 @@ public class NumberPicker extends LinearLayout { @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { - if (mMaxHeight <= 0 && mMaxWidth <= 0) { - super.onLayout(changed, left, top, right, bottom); - } else { - final int msrdWdth = getMeasuredWidth(); - final int msrdHght = getMeasuredHeight(); - - // Increment button at the top. - final int inctBtnMsrdWdth = mIncrementButton.getMeasuredWidth(); - final int incrBtnLeft = (msrdWdth - inctBtnMsrdWdth) / 2; - final int incrBtnTop = 0; - final int incrBtnRight = incrBtnLeft + inctBtnMsrdWdth; - final int incrBtnBottom = incrBtnTop + mIncrementButton.getMeasuredHeight(); - mIncrementButton.layout(incrBtnLeft, incrBtnTop, incrBtnRight, incrBtnBottom); - - // Input text centered horizontally. - final int inptTxtMsrdWdth = mInputText.getMeasuredWidth(); - final int inptTxtMsrdHght = mInputText.getMeasuredHeight(); - final int inptTxtLeft = (msrdWdth - inptTxtMsrdWdth) / 2; - final int inptTxtTop = (msrdHght - inptTxtMsrdHght) / 2; - final int inptTxtRight = inptTxtLeft + inptTxtMsrdWdth; - final int inptTxtBottom = inptTxtTop + inptTxtMsrdHght; - mInputText.layout(inptTxtLeft, inptTxtTop, inptTxtRight, inptTxtBottom); - - // Decrement button at the top. - final int decrBtnMsrdWdth = mIncrementButton.getMeasuredWidth(); - final int decrBtnLeft = (msrdWdth - decrBtnMsrdWdth) / 2; - final int decrBtnTop = msrdHght - mDecrementButton.getMeasuredHeight(); - final int decrBtnRight = decrBtnLeft + decrBtnMsrdWdth; - final int decrBtnBottom = msrdHght; - mDecrementButton.layout(decrBtnLeft, decrBtnTop, decrBtnRight, decrBtnBottom); - } + final int msrdWdth = getMeasuredWidth(); + final int msrdHght = getMeasuredHeight(); + + // Increment button at the top. + final int inctBtnMsrdWdth = mIncrementButton.getMeasuredWidth(); + final int incrBtnLeft = (msrdWdth - inctBtnMsrdWdth) / 2; + final int incrBtnTop = 0; + final int incrBtnRight = incrBtnLeft + inctBtnMsrdWdth; + final int incrBtnBottom = incrBtnTop + mIncrementButton.getMeasuredHeight(); + mIncrementButton.layout(incrBtnLeft, incrBtnTop, incrBtnRight, incrBtnBottom); + + // Input text centered horizontally. + final int inptTxtMsrdWdth = mInputText.getMeasuredWidth(); + final int inptTxtMsrdHght = mInputText.getMeasuredHeight(); + final int inptTxtLeft = (msrdWdth - inptTxtMsrdWdth) / 2; + final int inptTxtTop = (msrdHght - inptTxtMsrdHght) / 2; + final int inptTxtRight = inptTxtLeft + inptTxtMsrdWdth; + final int inptTxtBottom = inptTxtTop + inptTxtMsrdHght; + mInputText.layout(inptTxtLeft, inptTxtTop, inptTxtRight, inptTxtBottom); + + // Decrement button at the top. + final int decrBtnMsrdWdth = mIncrementButton.getMeasuredWidth(); + final int decrBtnLeft = (msrdWdth - decrBtnMsrdWdth) / 2; + final int decrBtnTop = msrdHght - mDecrementButton.getMeasuredHeight(); + final int decrBtnRight = decrBtnLeft + decrBtnMsrdWdth; + final int decrBtnBottom = msrdHght; + mDecrementButton.layout(decrBtnLeft, decrBtnTop, decrBtnRight, decrBtnBottom); if (!mScrollWheelAndFadingEdgesInitialized) { mScrollWheelAndFadingEdgesInitialized = true; @@ -719,20 +741,9 @@ public class NumberPicker extends LinearLayout { @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { - super.onMeasure(widthMeasureSpec, heightMeasureSpec); - final int measuredWidth; - if (mMaxWidth > 0) { - measuredWidth = getMaxSize(widthMeasureSpec, mMaxWidth); - } else { - measuredWidth = getMeasuredWidth(); - } - final int measuredHeight; - if (mMaxHeight > 0) { - measuredHeight = getMaxSize(heightMeasureSpec, mMaxHeight); - } else { - measuredHeight = getMeasuredHeight(); - } - setMeasuredDimension(measuredWidth, measuredHeight); + final int newWidthMeasureSpec = makeMeasureSpec(widthMeasureSpec, mMinWidth, mMaxWidth); + final int newHeightMeasureSpec = makeMeasureSpec(heightMeasureSpec, mMinHeight, mMaxHeight); + super.onMeasure(newWidthMeasureSpec, newHeightMeasureSpec); } @Override @@ -1034,6 +1045,49 @@ public class NumberPicker extends LinearLayout { } /** + * Computes the max width if no such specified as an attribute. + */ + private void tryComputeMaxWidth() { + if (!mComputeMaxWidth) { + return; + } + int maxTextWidth = 0; + if (mDisplayedValues == null) { + float maxDigitWidth = 0; + for (int i = 0; i <= 9; i++) { + final float digitWidth = mSelectorWheelPaint.measureText(String.valueOf(i)); + if (digitWidth > maxDigitWidth) { + maxDigitWidth = digitWidth; + } + } + int numberOfDigits = 0; + int current = mMaxValue; + while (current > 0) { + numberOfDigits++; + current = current / 10; + } + maxTextWidth = (int) (numberOfDigits * maxDigitWidth); + } else { + final int valueCount = mDisplayedValues.length; + for (int i = 0; i < valueCount; i++) { + final float textWidth = mSelectorWheelPaint.measureText(mDisplayedValues[i]); + if (textWidth > maxTextWidth) { + maxTextWidth = (int) textWidth; + } + } + } + maxTextWidth += mInputText.getPaddingLeft() + mInputText.getPaddingRight(); + if (mMaxWidth != maxTextWidth) { + if (maxTextWidth > mMinWidth) { + mMaxWidth = maxTextWidth; + } else { + mMaxWidth = mMinWidth; + } + invalidate(); + } + } + + /** * Gets whether the selector wheel wraps when reaching the min/max value. * * @return True if the selector wheel wraps. @@ -1119,6 +1173,7 @@ public class NumberPicker extends LinearLayout { setWrapSelectorWheel(wrapSelectorWheel); initializeSelectorWheelIndices(); updateInputTextView(); + tryComputeMaxWidth(); } /** @@ -1150,6 +1205,7 @@ public class NumberPicker extends LinearLayout { setWrapSelectorWheel(wrapSelectorWheel); initializeSelectorWheelIndices(); updateInputTextView(); + tryComputeMaxWidth(); } /** @@ -1298,24 +1354,28 @@ public class NumberPicker extends LinearLayout { } /** - * Gets the max value for a size based on the measure spec passed by - * the parent and the max value for that size. + * Makes a measure spec that tries greedily to use the max value. * * @param measureSpec The measure spec. * @param maxValue The max value for the size. - * @return The max size. + * @return A measure spec greedily imposing the max size. */ - private int getMaxSize(int measureSpec, int maxValue) { + private int makeMeasureSpec(int measureSpec, int minValue, int maxValue) { + final int size = MeasureSpec.getSize(measureSpec); + if (size < minValue) { + throw new IllegalArgumentException("Available space is less than min size: " + + size + " < " + minValue); + } final int mode = MeasureSpec.getMode(measureSpec); switch (mode) { case MeasureSpec.EXACTLY: - return MeasureSpec.getSize(measureSpec); + return measureSpec; case MeasureSpec.AT_MOST: - return Math.min(MeasureSpec.getSize(measureSpec), maxValue); + return MeasureSpec.makeMeasureSpec(Math.min(size, maxValue), MeasureSpec.EXACTLY); case MeasureSpec.UNSPECIFIED: - return maxValue; + return MeasureSpec.makeMeasureSpec(maxValue, MeasureSpec.EXACTLY); default: - throw new IllegalArgumentException(); + throw new IllegalArgumentException("Unknown measure mode: " + mode); } } |
