Skip to content

Commit

Permalink
Add align and sidePadding attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuspiresl committed Jul 2, 2020
1 parent e693cad commit 0a30457
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
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>
8 changes: 6 additions & 2 deletions sample/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@

<com.shawnlin.numberpicker.NumberPicker
android:id="@+id/number_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_width="match_parent"
android:layout_height="match_parent"
app:np_align="left"
app:np_sidePadding="20dp"
app:np_wheelItemCount="7" />

<com.shawnlin.numberpicker.NumberPicker
android:id="@+id/horizontal_number_picker"
Expand All @@ -40,6 +43,7 @@
app:np_typeface="@string/roboto_light"
app:np_fadingEdgeEnabled="false"
app:np_wrapSelectorWheel="true"
android:visibility="gone"
app:np_dividerType="underline" />

</LinearLayout>

0 comments on commit 0a30457

Please sign in to comment.