From 53356a27b22e2384c3032001199c82a03a9cbd8d Mon Sep 17 00:00:00 2001 From: Tony Al Haddad Date: Sat, 10 Jun 2017 12:35:23 +0800 Subject: [PATCH 1/2] Adding ability to change selected text size --- README.md | 6 ++++++ build.gradle | 2 +- gradle/wrapper/gradle-wrapper.properties | 4 ++-- .../shawnlin/numberpicker/NumberPicker.java | 21 ++++++++++++++++++- library/src/main/res/values/attrs.xml | 1 + .../numberpicker/sample/MainActivity.java | 4 ++++ sample/src/main/res/layout/content_main.xml | 1 + sample/src/main/res/values/dimens.xml | 2 ++ 8 files changed, 37 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a6888ce..5bf175b 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,10 @@ numberPicker.setTextColorResource(R.color.dark_grey); numberPicker.setTextSize(getResources().getDimension(R.dimen.text_size)); numberPicker.setTextSize(R.dimen.text_size); +// set selected text size +numberPicker.setSelectedTextSize(getResources().getDimension(R.dimen.selected_text_size)); +numberPicker.setSelectedTextSize(R.dimen.selected_text_size); + // set typeface numberPicker.setTypeface(Typeface.create(getString(R.string.roboto_light), Typeface.NORMAL)); numberPicker.setTypeface(getString(R.string.roboto_light), Typeface.NORMAL); @@ -68,6 +72,7 @@ add `xmlns:app="http://schemas.android.com/apk/res-auto"` app:np_selectedTextColor="@color/colorPrimary" app:np_textColor="@color/colorPrimary" app:np_textSize="@dimen/text_size" + app:np_selectedTextSize="@dimen/selected_text_size" app:np_typeface="@string/roboto_light" app:np_value="3" /> ``` @@ -88,6 +93,7 @@ add `xmlns:app="http://schemas.android.com/apk/res-auto"` |np_selectedTextColor|The text color of the selected number.| |np_textColor|The text color of the numbers.| |np_textSize|The text size of the numbers.| +|np_selectedTextSize|The text size of the selected number.| |np_typeface|The typeface of the numbers.| |np_value|The current value of this widget.| |np_wheelItemCount|The number of items show in the selector wheel.| diff --git a/build.gradle b/build.gradle index b6e6252..d350f33 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.2.3' + classpath 'com.android.tools.build:gradle:2.3.3' 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 diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index ce21133..753d1c2 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Wed Aug 17 00:26:16 CST 2016 +#Sat Jun 10 11:18:32 SGT 2017 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip diff --git a/library/src/main/java/com/shawnlin/numberpicker/NumberPicker.java b/library/src/main/java/com/shawnlin/numberpicker/NumberPicker.java index d71c611..a23958f 100644 --- a/library/src/main/java/com/shawnlin/numberpicker/NumberPicker.java +++ b/library/src/main/java/com/shawnlin/numberpicker/NumberPicker.java @@ -220,6 +220,11 @@ public static final Formatter getTwoDigitFormatter() { */ private float mTextSize = DEFAULT_TEXT_SIZE; + /** + * The size of the selected text. + */ + private float mSelectedTextSize = DEFAULT_TEXT_SIZE; + /** * The typeface of the text. */ @@ -585,6 +590,8 @@ public NumberPicker(Context context, AttributeSet attrs, int defStyle) { mSelectedTextColor = attributesArray.getColor(R.styleable.NumberPicker_np_selectedTextColor, mSelectedTextColor); mTextColor = attributesArray.getColor(R.styleable.NumberPicker_np_textColor, mTextColor); mTextSize = attributesArray.getDimension(R.styleable.NumberPicker_np_textSize, spToPx(mTextSize)); + mSelectedTextSize = attributesArray.getDimension(R.styleable.NumberPicker_np_selectedTextSize, spToPx(mSelectedTextSize)); + mTypeface = Typeface.create(attributesArray.getString(R.styleable.NumberPicker_np_typeface), Typeface.NORMAL); mFormatter = stringToFormatter(attributesArray.getString(R.styleable.NumberPicker_np_formatter)); mWheelItemCount = attributesArray.getInt(R.styleable.NumberPicker_np_wheelItemCount, mWheelItemCount); @@ -614,6 +621,7 @@ public NumberPicker(Context context, AttributeSet attrs, int defStyle) { setSelectedTextColor(mSelectedTextColor); setTextColor(mTextColor); setTextSize(mTextSize); + setSelectedTextSize(mSelectedTextSize); setTypeface(mTypeface); setFormatter(mFormatter); updateInputTextView(); @@ -1365,8 +1373,10 @@ protected void onDraw(Canvas canvas) { int[] selectorIndices = mSelectorIndices; for (int i = 0; i < selectorIndices.length; i++) { if (i == mWheelMiddleItemIndex) { + mSelectorWheelPaint.setTextSize(mSelectedTextSize); mSelectorWheelPaint.setColor(mSelectedTextColor); } else { + mSelectorWheelPaint.setTextSize(mTextSize); mSelectorWheelPaint.setColor(mTextColor); } @@ -2072,14 +2082,20 @@ public void setTextColorResource(@ColorRes int colorId) { public void setTextSize(float textSize) { mTextSize = textSize; - mSelectedText.setTextSize(pxToSp(mTextSize)); mSelectorWheelPaint.setTextSize(mTextSize); } public void setTextSize(@DimenRes int dimenId) { setTextSize(getResources().getDimension(dimenId)); } + public void setSelectedTextSize(float textSize) { + mSelectedTextSize = textSize; + mSelectedText.setTextSize(pxToSp(mSelectedTextSize)); + } + public void setSelectedTextSize(@DimenRes int dimenId) { + setSelectedTextSize(getResources().getDimension(dimenId)); + } public void setTypeface(Typeface typeface) { mTypeface = typeface; if (mTypeface != null) { @@ -2183,4 +2199,7 @@ public Typeface getTypeface() { return mTypeface; } + public float getmSelectedTextSize() { + return mSelectedTextSize; + } } diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml index 77038f8..e7ab119 100644 --- a/library/src/main/res/values/attrs.xml +++ b/library/src/main/res/values/attrs.xml @@ -19,6 +19,7 @@ + diff --git a/sample/src/main/java/com/shawnlin/numberpicker/sample/MainActivity.java b/sample/src/main/java/com/shawnlin/numberpicker/sample/MainActivity.java index 3f9dca4..1beac55 100644 --- a/sample/src/main/java/com/shawnlin/numberpicker/sample/MainActivity.java +++ b/sample/src/main/java/com/shawnlin/numberpicker/sample/MainActivity.java @@ -39,6 +39,10 @@ protected void onCreate(Bundle savedInstanceState) { numberPicker.setTextSize(getResources().getDimension(R.dimen.text_size)); numberPicker.setTextSize(R.dimen.text_size); + // set selected text size + numberPicker.setSelectedTextSize(getResources().getDimension(R.dimen.selected_text_size)); + numberPicker.setSelectedTextSize(R.dimen.selected_text_size); + // set typeface numberPicker.setTypeface(Typeface.create(getString(R.string.roboto_light), Typeface.NORMAL)); numberPicker.setTypeface(getString(R.string.roboto_light), Typeface.NORMAL); diff --git a/sample/src/main/res/layout/content_main.xml b/sample/src/main/res/layout/content_main.xml index d01b003..3cdf0b6 100644 --- a/sample/src/main/res/layout/content_main.xml +++ b/sample/src/main/res/layout/content_main.xml @@ -35,6 +35,7 @@ app:np_selectedTextColor="@color/colorAccent" app:np_textColor="@color/colorAccent" app:np_textSize="@dimen/text_size" + app:np_selectedTextSize="@dimen/selected_text_size" app:np_typeface="@string/roboto_light" /> diff --git a/sample/src/main/res/values/dimens.xml b/sample/src/main/res/values/dimens.xml index 6b76556..950b211 100644 --- a/sample/src/main/res/values/dimens.xml +++ b/sample/src/main/res/values/dimens.xml @@ -5,4 +5,6 @@ 16dp 30dp + 35dp + From 0410a0ccddd257dcf92bd6ff936e4dbc5130c8af Mon Sep 17 00:00:00 2001 From: Tony Al Haddad Date: Sat, 10 Jun 2017 12:38:28 +0800 Subject: [PATCH 2/2] remove empty lines --- .../src/main/java/com/shawnlin/numberpicker/NumberPicker.java | 1 - sample/src/main/res/values/dimens.xml | 1 - 2 files changed, 2 deletions(-) diff --git a/library/src/main/java/com/shawnlin/numberpicker/NumberPicker.java b/library/src/main/java/com/shawnlin/numberpicker/NumberPicker.java index a23958f..74663d0 100644 --- a/library/src/main/java/com/shawnlin/numberpicker/NumberPicker.java +++ b/library/src/main/java/com/shawnlin/numberpicker/NumberPicker.java @@ -591,7 +591,6 @@ public NumberPicker(Context context, AttributeSet attrs, int defStyle) { mTextColor = attributesArray.getColor(R.styleable.NumberPicker_np_textColor, mTextColor); mTextSize = attributesArray.getDimension(R.styleable.NumberPicker_np_textSize, spToPx(mTextSize)); mSelectedTextSize = attributesArray.getDimension(R.styleable.NumberPicker_np_selectedTextSize, spToPx(mSelectedTextSize)); - mTypeface = Typeface.create(attributesArray.getString(R.styleable.NumberPicker_np_typeface), Typeface.NORMAL); mFormatter = stringToFormatter(attributesArray.getString(R.styleable.NumberPicker_np_formatter)); mWheelItemCount = attributesArray.getInt(R.styleable.NumberPicker_np_wheelItemCount, mWheelItemCount); diff --git a/sample/src/main/res/values/dimens.xml b/sample/src/main/res/values/dimens.xml index 950b211..e26ebc1 100644 --- a/sample/src/main/res/values/dimens.xml +++ b/sample/src/main/res/values/dimens.xml @@ -6,5 +6,4 @@ 30dp 35dp -