Skip to content

Commit

Permalink
Allow to modify value text field requested by #2
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelmuenzer committed May 3, 2017
1 parent e534dca commit 70676df
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 20 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ There exist further attributes which let you customize the general appearance of
app:snp_orientation="horizontal"
app:snp_valueMarginEnd="5dp"
app:snp_valueMarginStart="5dp"
app:snp_value_text_color="@color/colorPrimary"
app:snp_value_text_size="16sp"
app:snp_value_text_appearance="?android:attr/textAppearanceMedium"
app:snp_buttonPaddingBottom="8dp"
app:snp_buttonPaddingLeft="8dp"
app:snp_buttonPaddingRight="8dp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v4.widget.TextViewCompat;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
Expand All @@ -28,6 +29,7 @@
import static android.view.KeyEvent.KEYCODE_DPAD_UP;

public class ScrollableNumberPicker extends LinearLayout {
private final static int INVALID_RES = -1;
private final static float SLOWING_FACTOR = 1.25f;
private final static int MIN_UPDATE_INTERVAL_MS = 50;

Expand All @@ -47,6 +49,9 @@ public class ScrollableNumberPicker extends LinearLayout {
private int mMaxValue;
private int mMinValue;
private int mStepSize;
private float mValueTextSize;
private int mValueTextColor;
private int mValueTextAppearanceResId;

private boolean mScrollEnabled;
private int mUpdateIntervalMillis;
Expand Down Expand Up @@ -158,12 +163,38 @@ public void setValue(int value) {

private void setValue() {
mValueTextView.setText(String.valueOf(mValue));

if (mListener != null) {
mListener.onNumberPicked(mValue);
}
}

private void initValueView() {
mValueTextView = (TextView) findViewById(R.id.text_value);

if (mValueTextAppearanceResId != INVALID_RES) {
TextViewCompat.setTextAppearance(mValueTextView, mValueTextAppearanceResId);
}

if (mValueTextColor != INVALID_RES) {
mValueTextView.setTextColor(mValueTextColor);
}

if (mValueTextSize != INVALID_RES) {
mValueTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mValueTextSize);
}

LinearLayout.LayoutParams layoutParams = (LayoutParams) mValueTextView.getLayoutParams();
if (mOrientation == HORIZONTAL) {
layoutParams.setMargins(mValueMarginStart, 0, mValueMarginEnd, 0);
} else {
layoutParams.setMargins(0, mValueMarginStart, 0, mValueMarginEnd);
}

mValueTextView.setLayoutParams(layoutParams);

setValue();
}

@SuppressWarnings("unused")
public int getMaxValue() {
return mMaxValue;
Expand Down Expand Up @@ -311,6 +342,12 @@ private void init(Context context, AttributeSet attrs) {
mValue = typedArray.getInt(R.styleable.ScrollableNumberPicker_snp_value,
res.getInteger(R.integer.default_value));

mValueTextSize = typedArray.getDimension(R.styleable.ScrollableNumberPicker_snp_value_text_size,
INVALID_RES);
mValueTextColor = typedArray.getColor(R.styleable.ScrollableNumberPicker_snp_value_text_color,
INVALID_RES);
mValueTextAppearanceResId = typedArray.getResourceId(R.styleable.ScrollableNumberPicker_snp_value_text_appearance, INVALID_RES);

mScrollEnabled = typedArray.getBoolean(R.styleable.ScrollableNumberPicker_snp_scrollEnabled,
res.getBoolean(R.bool.default_scrollEnabled));

Expand All @@ -332,7 +369,6 @@ private void init(Context context, AttributeSet attrs) {
typedArray.recycle();

initViews();
setValue();

mAutoIncrement = false;
mAutoDecrement = false;
Expand All @@ -341,20 +377,12 @@ private void init(Context context, AttributeSet attrs) {
}

private void initViews() {
mValueTextView = (TextView) findViewById(R.id.text_value);
LinearLayout.LayoutParams layoutParams = (LayoutParams) mValueTextView.getLayoutParams();
if (mOrientation == HORIZONTAL) {
layoutParams.setMargins(mValueMarginStart, 0, mValueMarginEnd, 0);
} else {
layoutParams.setMargins(0, mValueMarginStart, 0, mValueMarginEnd);
}
mValueTextView.setLayoutParams(layoutParams);

setOrientation(mOrientation);
setGravity(Gravity.CENTER);

initButtonPlus();
initButtonMinus();
initValueView();
initButtonPlusView();
initButtonMinusView();

if (mScrollEnabled) {
setOnTouchListener(new OnTouchListener() {
Expand Down Expand Up @@ -431,7 +459,7 @@ public boolean onTouch(View view, MotionEvent motionEvent) {
}
}

private void initButtonPlus() {
private void initButtonPlusView() {
setButtonPlusImage();

mPlusButton.setOnClickListener(new OnClickListener() {
Expand Down Expand Up @@ -465,7 +493,7 @@ public boolean onTouch(View v, MotionEvent event) {
});
}

private void initButtonMinus() {
private void initButtonMinusView() {
setButtonMinusImage();

mMinusButton.setOnClickListener(new View.OnClickListener() {
Expand Down
4 changes: 4 additions & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
<attr name="snp_maxValue" format="integer"/>
<attr name="snp_minValue" format="integer"/>
<attr name="snp_stepSize" format="integer"/>
<attr name="snp_value_text_size" format="dimension"/>
<attr name="snp_value_text_color" format="color"/>
<attr name="snp_value_text_appearance" format="reference"/>

<attr name="snp_updateInterval" format="integer"/>
<attr name="snp_scrollEnabled" format="boolean"/>

Expand Down
File renamed without changes.
13 changes: 8 additions & 5 deletions sample-mobile/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

<com.michaelmuenzer.android.scrollablennumberpicker.ScrollableNumberPicker
android:id="@+id/number_picker_vertical_icons"
android:layout_width="40dp"
android:layout_width="50dp"
android:layout_height="95dp"
android:layout_margin="10dp"
android:background="@drawable/number_picker_bg_color"
Expand All @@ -64,7 +64,8 @@
app:snp_updateInterval="10"
app:snp_value="10"
app:snp_valueMarginEnd="8dp"
app:snp_valueMarginStart="8dp"/>
app:snp_valueMarginStart="8dp"
app:snp_value_text_appearance="?android:attr/textAppearanceMedium"/>

<com.michaelmuenzer.android.scrollablennumberpicker.ScrollableNumberPicker
android:id="@+id/number_picker_horizontal_icons"
Expand All @@ -75,14 +76,16 @@
app:snp_buttonIconLeft="@drawable/btn_left_selector_main"
app:snp_buttonIconRight="@drawable/btn_right_selector_main"
app:snp_buttonTouchScaleFactor="0.7"
app:snp_maxValue="1000"
app:snp_maxValue="90"
app:snp_minValue="10"
app:snp_orientation="horizontal"
app:snp_stepSize="10"
app:snp_updateInterval="50"
app:snp_updateInterval="5"
app:snp_value="10"
app:snp_valueMarginEnd="5dp"
app:snp_valueMarginStart="5dp"/>
app:snp_valueMarginStart="5dp"
app:snp_value_text_color="@color/colorPrimary"
app:snp_value_text_size="16sp"/>
</LinearLayout>

<LinearLayout android:layout_width="wrap_content"
Expand Down

0 comments on commit 70676df

Please sign in to comment.