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

Gap size changing #197

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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ numberPicker.setMaxValue(59);
numberPicker.setMinValue(0);
numberPicker.setValue(3);

//Set gap size
numberPicker.setGapSize(50);

// Using string values
// IMPORTANT! setMinValue to 1 and call setDisplayedValues after setMinValue and setMaxValue
String[] data = {"A", "B", "C", "D", "E", "F", "G", "H", "I"};
Expand Down Expand Up @@ -127,6 +130,7 @@ add `xmlns:app="http://schemas.android.com/apk/res-auto"`
app:np_height="180dp"
app:np_dividerColor="@color/colorPrimary"
app:np_formatter="@string/number_picker_formatter"
app:np_gapSize="50"
app:np_max="59"
app:np_min="0"
app:np_selectedTextColor="@color/colorPrimary"
Expand Down Expand Up @@ -155,6 +159,7 @@ add `xmlns:app="http://schemas.android.com/apk/res-auto"`
|np_hideWheelUntilFocused|Flag whether the selector wheel should hidden until the picker has focus.|
|np_itemSpacing|Amount of space between items.|
|np_lineSpacingMultiplier|The line spacing multiplier for the multiple lines.|
|np_gapSize|The gap size of this widget.|
|np_max|The max value of this widget.|
|np_maxFlingVelocityCoefficient|The coefficient to adjust (divide) the max fling velocity.|
|np_min|The min value of this widget.|
Expand Down
38 changes: 34 additions & 4 deletions library/src/main/java/com/shawnlin/numberpicker/NumberPicker.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.shawnlin.numberpicker;

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

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.TypedArray;
Expand Down Expand Up @@ -44,8 +46,6 @@
import java.text.NumberFormat;
import java.util.Locale;

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

/**
* A widget that enables the user to select a number from a predefined range.
*/
Expand Down Expand Up @@ -174,6 +174,11 @@ public class NumberPicker extends LinearLayout {
*/
private static final float DEFAULT_LINE_SPACING_MULTIPLIER = 1f;

/**
* The default gap size.
*/
private static final int DEFAULT_GAP_SIZE = 0;

/**
* Use a custom NumberPicker formatting callback to use two-digit minutes
* strings like "01". Keeping a static formatter etc. is the most efficient
Expand Down Expand Up @@ -575,6 +580,11 @@ public static Formatter getTwoDigitFormatter() {
*/
private int mOrientation;

/**
* The gap size between items
*/
private int mGapSize;

/**
* The order of this widget.
*/
Expand Down Expand Up @@ -760,6 +770,8 @@ public NumberPicker(Context context, AttributeSet attrs, int defStyle) {
mOrder = attributes.getInt(R.styleable.NumberPicker_np_order, ASCENDING);
mOrientation = attributes.getInt(R.styleable.NumberPicker_np_orientation, VERTICAL);

mGapSize = attributes.getInt(R.styleable.NumberPicker_np_gapSize, DEFAULT_GAP_SIZE);

final float width = attributes.getDimensionPixelSize(R.styleable.NumberPicker_np_width,
SIZE_UNSPECIFIED);
final float height = attributes.getDimensionPixelSize(R.styleable.NumberPicker_np_height,
Expand Down Expand Up @@ -1670,6 +1682,24 @@ public void setMaxValue(int maxValue) {
invalidate();
}

/**
* Sets the gap size of the picker.
*
* @param size The gap size.
*/
public void setGapSize(int size) {
mGapSize = size;
}

/**
* Gets the gap size of the picker.
*
* @return The gap size.
*/
public int getGapSize() {
return mGapSize;
}

/**
* Gets the values to be displayed instead of string values.
*
Expand Down Expand Up @@ -2165,12 +2195,12 @@ private void initializeSelectorWheel() {
float textGapCount = selectorIndices.length;
if (isHorizontalMode()) {
float totalTextGapWidth = (getRight() - getLeft()) - totalTextSize;
mSelectorTextGapWidth = (int) (totalTextGapWidth / textGapCount);
mSelectorTextGapWidth = (int) (totalTextGapWidth / textGapCount) + mGapSize;
mSelectorElementSize = (int) getMaxTextSize() + mSelectorTextGapWidth;
mInitialScrollOffset = (int) (mSelectedTextCenterX - mSelectorElementSize * mWheelMiddleItemIndex);
} else {
float totalTextGapHeight = (getBottom() - getTop()) - totalTextSize;
mSelectorTextGapHeight = (int) (totalTextGapHeight / textGapCount);
mSelectorTextGapHeight = (int) (totalTextGapHeight / textGapCount) + mGapSize;
mSelectorElementSize = (int) getMaxTextSize() + mSelectorTextGapHeight;
mInitialScrollOffset = (int) (mSelectedTextCenterY - mSelectorElementSize * mWheelMiddleItemIndex);
}
Expand Down
1 change: 1 addition & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,6 @@
<attr name="np_value" format="integer" />
<attr name="np_wheelItemCount" format="integer" />
<attr name="np_wrapSelectorWheel" format="boolean" />
<attr name="np_gapSize" format="integer"/>
</declare-styleable>
</resources>
14 changes: 7 additions & 7 deletions sample/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_vertical_margin"
android:paddingBottom="@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 @@ -24,10 +24,12 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
app:np_width="180dp"
app:np_height="64dp"
app:np_accessibilityDescriptionEnabled="true"
app:np_dividerColor="@color/colorAccent"
app:np_dividerType="underline"
app:np_fadingEdgeEnabled="false"
app:np_gapSize="50"
app:np_height="64dp"
app:np_max="10"
app:np_min="0"
app:np_order="descending"
Expand All @@ -38,8 +40,6 @@
app:np_textColor="@color/colorAccent"
app:np_textSize="@dimen/text_size"
app:np_typeface="@string/roboto_light"
app:np_fadingEdgeEnabled="false"
app:np_wrapSelectorWheel="true"
app:np_dividerType="underline" />

app:np_width="180dp"
app:np_wrapSelectorWheel="true" />
</LinearLayout>