From 24bda3d2ae0b7c4551c999b2ea113b874a80c94d Mon Sep 17 00:00:00 2001 From: Mateus Pires Date: Fri, 3 Jul 2020 10:45:32 -0300 Subject: [PATCH] Add align and sidePadding attributes --- .../shawnlin/numberpicker/NumberPicker.java | 27 ++++++++++++++++++- library/src/main/res/values/attrs.xml | 6 +++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/library/src/main/java/com/shawnlin/numberpicker/NumberPicker.java b/library/src/main/java/com/shawnlin/numberpicker/NumberPicker.java index 343cece..279a533 100644 --- a/library/src/main/java/com/shawnlin/numberpicker/NumberPicker.java +++ b/library/src/main/java/com/shawnlin/numberpicker/NumberPicker.java @@ -148,6 +148,11 @@ public class NumberPicker extends LinearLayout { */ private static final int DEFAULT_TEXT_ALIGN = CENTER; + /** + * The default content alignment. + */ + private static final int DEFAULT_ALIGN = CENTER; + /** * The default color of text. */ @@ -614,6 +619,16 @@ public static final Formatter getTwoDigitFormatter() { */ private ViewConfiguration mViewConfiguration; + /** + * The content alignment. + */ + private int mAlign = DEFAULT_ALIGN; + + /** + * The padding applied to the content when it's not centered (align is "left" or "right"). + */ + private int mSidePadding = 0; + /** * Interface to listen for changes of the current value. */ @@ -793,6 +808,9 @@ public NumberPicker(Context context, AttributeSet attrs, int defStyle) { R.styleable.NumberPicker_np_hideWheelUntilFocused, false); mAccessibilityDescriptionEnabled = attributes.getBoolean( R.styleable.NumberPicker_np_accessibilityDescriptionEnabled, true); + mAlign = attributes.getInt(R.styleable.NumberPicker_np_align, mAlign); + mSidePadding = attributes.getDimensionPixelSize( + R.styleable.NumberPicker_np_sidePadding, mSidePadding); // By default Linearlayout that we extend is not drawn. This is // its draw() method is not called but dispatchDraw() is called @@ -1751,7 +1769,14 @@ protected void onDraw(Canvas canvas) { canvas.clipRect(mLeftDividerLeft, 0, mRightDividerRight, getBottom()); } } else { - x = (getRight() - getLeft()) / 2; + if (mAlign == LEFT) { + x = getLeft() + mSidePadding; + } else if (mAlign == RIGHT) { + x = getRight() - getMaxTextSize() - mSidePadding; + } else { + x = (getRight() - getLeft()) / 2f; + } + y = mCurrentScrollOffset; if (mRealWheelItemCount < DEFAULT_WHEEL_ITEM_COUNT) { canvas.clipRect(0, mTopDividerTop, getRight(), mBottomDividerBottom); diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml index db23c62..e4206fe 100644 --- a/library/src/main/res/values/attrs.xml +++ b/library/src/main/res/values/attrs.xml @@ -51,5 +51,11 @@ + + + + + + \ No newline at end of file