Skip to content

Commit

Permalink
Merge pull request #6 from ShawnLin013/feature/Set-size
Browse files Browse the repository at this point in the history
Add the feature of width and height setting
  • Loading branch information
ShawnLin013 authored Jul 28, 2016
2 parents b1fcfb0 + 013f8f7 commit cccc263
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 18 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ add `xmlns:app="http://schemas.android.com/apk/res-auto"`
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
app:np_width="64"
app:np_height="180"
app:np_dividerColor="@color/colorPrimary"
app:np_formatter="@string/number_picker_formatter"
app:np_max="59"
Expand All @@ -67,6 +69,8 @@ add `xmlns:app="http://schemas.android.com/apk/res-auto"`
|np_textColor|The text color of the numbers|
|np_textSize|The text size of the numbers|
|np_typeface|The typeface of the numbers|
|np_width|The width of the number picker|
|np_height|The height of the number picker|

## Gradle

Expand All @@ -80,7 +84,7 @@ buildscript {
}
dependencies {
compile 'com.shawnlin:number-picker:1.0.0'
compile 'com.shawnlin:number-picker:1.0.1'
}
```

Expand Down
78 changes: 64 additions & 14 deletions library/src/main/java/com/shawnlin/numberpicker/NumberPicker.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@

public class NumberPicker extends android.widget.NumberPicker {

private static final float DEFAULT_WIDTH = 64;

private static final float DEFAULT_HEIGHT = 180;

private int mDividerColor;

private String mFormatter;

private boolean mFocusable = false;

private boolean mIsHorizontal = false;

private int mMax = 100;

private int mMin = 1;
Expand All @@ -36,28 +42,72 @@ public class NumberPicker extends android.widget.NumberPicker {

private Typeface mTypeface;

public NumberPicker(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.NumberPicker);
initAttributes(typedArray);
typedArray.recycle();
private float mWidth;

private float mHeight;

/**
* Create a new number picker.
*
* @param context The application environment.
*/
public NumberPicker(Context context) {
this(context, null);
}

private void initAttributes(TypedArray typedArray) {
mDividerColor = typedArray.getColor(R.styleable.NumberPicker_np_dividerColor, mDividerColor);
mFormatter = typedArray.getString(R.styleable.NumberPicker_np_formatter);
mFocusable = typedArray.getBoolean(R.styleable.NumberPicker_np_focusable, mFocusable);
mMax = typedArray.getInt(R.styleable.NumberPicker_np_max, mMax);
mMin = typedArray.getInt(R.styleable.NumberPicker_np_min, mMin);
mTextColor = typedArray.getColor(R.styleable.NumberPicker_np_textColor, mTextColor);
mTextSize = typedArray.getDimension(R.styleable.NumberPicker_np_textSize, mTextSize);
mTypeface = Typeface.create(typedArray.getString(R.styleable.NumberPicker_np_typeface), Typeface.NORMAL);
/**
* Create a new number picker.
*
* @param context The application environment.
* @param attrs A collection of attributes.
*/
public NumberPicker(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

/**
* Create a new number picker
*
* @param context the application environment.
* @param attrs a collection of attributes.
* @param defStyle The default style to apply to this view.
*/
public NumberPicker(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs);
TypedArray attributesArray = context.obtainStyledAttributes(attrs, R.styleable.NumberPicker, defStyle, 0);
mDividerColor = attributesArray.getColor(R.styleable.NumberPicker_np_dividerColor, mDividerColor);
mFormatter = attributesArray.getString(R.styleable.NumberPicker_np_formatter);
mFocusable = attributesArray.getBoolean(R.styleable.NumberPicker_np_focusable, mFocusable);
mIsHorizontal = attributesArray.getBoolean(R.styleable.NumberPicker_np_horizontal, mIsHorizontal);
mMax = attributesArray.getInt(R.styleable.NumberPicker_np_max, mMax);
mMin = attributesArray.getInt(R.styleable.NumberPicker_np_min, mMin);
mTextColor = attributesArray.getColor(R.styleable.NumberPicker_np_textColor, mTextColor);
mTextSize = attributesArray.getDimension(R.styleable.NumberPicker_np_textSize, mTextSize);
mTypeface = Typeface.create(attributesArray.getString(R.styleable.NumberPicker_np_typeface), Typeface.NORMAL);
mWidth = attributesArray.getFloat(R.styleable.NumberPicker_np_width, 0);
mHeight = attributesArray.getFloat(R.styleable.NumberPicker_np_height, 0);
attributesArray.recycle();

if (mIsHorizontal) {
setOrientation(HORIZONTAL);
}
setDividerColor(mDividerColor);
setFormatter(mFormatter);
setRotation(mIsHorizontal ? 270 : 0);
setMaxValue(mMax);
setMinValue(mMin);
setTextAttributes();

if (mWidth != 0 && mHeight != 0) {
setScaleX(mWidth / DEFAULT_WIDTH);
setScaleY(mHeight / DEFAULT_HEIGHT);
} else if (mWidth != 0) {
setScaleX(mWidth / DEFAULT_WIDTH);
setScaleY(mWidth / DEFAULT_WIDTH);
} else if (mHeight != 0) {
setScaleX(mHeight / DEFAULT_HEIGHT);
setScaleY(mHeight / DEFAULT_HEIGHT);
}
}

public void setDividerColor(@ColorInt int color) {
Expand Down
3 changes: 3 additions & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
<attr name="np_dividerColor" format="color" />
<attr name="np_formatter" format="string" />
<attr name="np_focusable" format="boolean" />
<attr name="np_horizontal" format="boolean" />
<attr name="np_max" format="integer" />
<attr name="np_min" format="integer" />
<attr name="np_textColor" format="color" />
<attr name="np_textSize" format="dimension" />
<attr name="np_typeface" format="string" />
<attr name="np_width" format="float" />
<attr name="np_height" format="float" />
</declare-styleable>
</resources>
24 changes: 21 additions & 3 deletions sample/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.shawnlin.numberpicker.sample.MainActivity"
tools:showIn="@layout/activity_main">
Expand All @@ -16,7 +16,9 @@
android:id="@+id/number_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="45dp"
app:np_width="50"
app:np_dividerColor="@color/colorPrimary"
app:np_formatter="@string/number_picker_formatter"
app:np_max="59"
Expand All @@ -25,4 +27,20 @@
app:np_textSize="@dimen/text_size"
app:np_typeface="@string/roboto_light" />

<com.shawnlin.numberpicker.NumberPicker
android:id="@+id/horizontal_number_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/number_picker"
android:layout_centerHorizontal="true"
android:layout_marginTop="45dp"
app:np_dividerColor="@color/colorPrimary"
app:np_formatter="@string/number_picker_formatter"
app:np_horizontal="true"
app:np_max="100"
app:np_min="1"
app:np_textColor="@color/colorPrimary"
app:np_textSize="@dimen/text_size"
app:np_typeface="@string/roboto_light" />

</RelativeLayout>

0 comments on commit cccc263

Please sign in to comment.