Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add align and sidePadding attributes #172

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,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.
*/
Expand Down Expand Up @@ -630,6 +635,16 @@ private enum DividerType {
*/
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.
*/
Expand Down Expand Up @@ -814,6 +829,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
Expand Down Expand Up @@ -1767,7 +1785,14 @@ protected void onDraw(Canvas canvas) {
canvas.clipRect(mLeftDividerLeft, 0, mRightDividerRight, getBottom());
}
} else {
x = (getRight() - getLeft()) / 2f;
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);
Expand Down
6 changes: 6 additions & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,11 @@
<attr name="np_value" format="integer" />
<attr name="np_wheelItemCount" format="integer" />
<attr name="np_wrapSelectorWheel" format="boolean" />
<attr name="np_align" format="enum">
<enum name="right" value="0" />
<enum name="center" value="1" />
<enum name="left" value="2" />
</attr>
<attr name="np_sidePadding" format="dimension" />
</declare-styleable>
</resources>