Skip to content

Commit

Permalink
Add features:
Browse files Browse the repository at this point in the history
> setDividerDistance
> setDividerThickness
> setOrientation
  • Loading branch information
ShawnLin013 committed Nov 10, 2016
1 parent 1263db7 commit 4e1c91b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ buildscript {
}
dependencies {
compile 'com.shawnlin:number-picker:2.1.0'
compile 'com.shawnlin:number-picker:2.2.0'
}
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.3'
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'com.novoda:bintray-release:0.3.4'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
VERSION_CODE=5
VERSION_NAME=2.1.0
VERSION_CODE=6
VERSION_NAME=2.2.0

GROUP=com.shawnlin
ARTIFACT_ID=number-picker
Expand Down
81 changes: 55 additions & 26 deletions library/src/main/java/com/shawnlin/numberpicker/NumberPicker.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.DimenRes;
import android.support.annotation.IntDef;
import android.support.annotation.StringRes;
import android.text.InputFilter;
import android.text.InputType;
Expand All @@ -42,17 +43,28 @@
import android.widget.EditText;
import android.widget.LinearLayout;

import java.lang.annotation.Retention;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

import static java.lang.annotation.RetentionPolicy.SOURCE;

/**
* A widget that enables the user to select a number form a predefined range.
*/
public class NumberPicker extends LinearLayout {

@Retention(SOURCE)
@IntDef({VERTICAL, HORIZONTAL})
public @interface Orientation{}

public static final int VERTICAL = LinearLayout.VERTICAL;

public static final int HORIZONTAL = LinearLayout.HORIZONTAL;

/**
* The default update interval during long press.
*/
Expand Down Expand Up @@ -169,25 +181,20 @@ public static final Formatter getTwoDigitFormatter() {
*/
private final EditText mInputText;

/**
* The distance between the two selection dividers.
*/
private final int mSelectionDividersDistance;

/**
* The min height of this widget.
*/
private final int mMinHeight;
private int mMinHeight;

/**
* The max height of this widget.
*/
private final int mMaxHeight;
private int mMaxHeight;

/**
* The max width of this widget.
*/
private final int mMinWidth;
private int mMinWidth;

/**
* The max width of this widget.
Expand Down Expand Up @@ -389,10 +396,15 @@ public static final Formatter getTwoDigitFormatter() {
*/
private int mSelectionDividerColor;

/**
* The distance between the two selection dividers.
*/
private int mSelectionDividersDistance;

/**
* The thickness of the selection divider.
*/
private final int mSelectionDividerThickness;
private int mSelectionDividerThickness;

/**
* The current scroll state of the number picker.
Expand Down Expand Up @@ -544,34 +556,24 @@ public NumberPicker(Context context, AttributeSet attrs, int defStyle) {

mSelectionDividerColor = attributesArray.getColor(R.styleable.NumberPicker_np_dividerColor, mSelectionDividerColor);

final int defSelectionDividerThickness = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, UNSCALED_DEFAULT_SELECTION_DIVIDER_THICKNESS,
getResources().getDisplayMetrics());
mSelectionDividerThickness = attributesArray.getDimensionPixelSize(
R.styleable.NumberPicker_np_dividerThickness, defSelectionDividerThickness);

final int defSelectionDividerDistance = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, UNSCALED_DEFAULT_SELECTION_DIVIDERS_DISTANCE,
getResources().getDisplayMetrics());
mSelectionDividersDistance = attributesArray.getDimensionPixelSize(
R.styleable.NumberPicker_np_dividerDistance, defSelectionDividerDistance);

final int defSelectionDividerThickness = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, UNSCALED_DEFAULT_SELECTION_DIVIDER_THICKNESS,
getResources().getDisplayMetrics());
mSelectionDividerThickness = attributesArray.getDimensionPixelSize(
R.styleable.NumberPicker_np_dividerThickness, defSelectionDividerThickness);

mOrientation = attributesArray.getInt(R.styleable.NumberPicker_np_orientation, VERTICAL);

mWidth = attributesArray.getDimensionPixelSize(R.styleable.NumberPicker_np_width, SIZE_UNSPECIFIED);
mHeight = attributesArray.getDimensionPixelSize(R.styleable.NumberPicker_np_height, SIZE_UNSPECIFIED);

if (isHorizontalMode()) {
mMinHeight = SIZE_UNSPECIFIED;
mMaxHeight = dpToPx(DEFAULT_MIN_WIDTH);
mMinWidth = dpToPx(DEFAULT_MAX_HEIGHT);
mMaxWidth = SIZE_UNSPECIFIED;
} else {
mMinHeight = SIZE_UNSPECIFIED;
mMaxHeight = dpToPx(DEFAULT_MAX_HEIGHT);
mMinWidth = dpToPx(DEFAULT_MIN_WIDTH);
mMaxWidth = SIZE_UNSPECIFIED;
}
setWidthAndHeight();

mComputeMaxWidth = true;

Expand Down Expand Up @@ -2501,6 +2503,20 @@ private String formatNumberWithLocale(int value) {
return String.format(Locale.getDefault(), "%d", value);
}

private void setWidthAndHeight() {
if (isHorizontalMode()) {
mMinHeight = SIZE_UNSPECIFIED;
mMaxHeight = dpToPx(DEFAULT_MIN_WIDTH);
mMinWidth = dpToPx(DEFAULT_MAX_HEIGHT);
mMaxWidth = SIZE_UNSPECIFIED;
} else {
mMinHeight = SIZE_UNSPECIFIED;
mMaxHeight = dpToPx(DEFAULT_MAX_HEIGHT);
mMinWidth = dpToPx(DEFAULT_MIN_WIDTH);
mMaxWidth = SIZE_UNSPECIFIED;
}
}

public void setDividerColor(@ColorInt int color) {
mSelectionDividerColor = color;
mSelectionDivider = new ColorDrawable(color);
Expand All @@ -2510,6 +2526,19 @@ public void setDividerColorResource(@ColorRes int colorId) {
setDividerColor(getResources().getColor(colorId));
}

public void setDividerDistance(int distance) {
mSelectionDividersDistance = dpToPx(distance);
}

public void setDividerThickness(int thickness) {
mSelectionDividerThickness = dpToPx(thickness);
}

public void setOrientation(@Orientation int orientation) {
mOrientation = orientation;
setWidthAndHeight();
}

public void setWheelItemCount(final int count) {
mWheelItemCount = count;
mWheelMiddleItemIndex = mWheelItemCount / 2;
Expand Down

0 comments on commit 4e1c91b

Please sign in to comment.