Skip to content

Commit

Permalink
Feature/step value (#6)
Browse files Browse the repository at this point in the history
* Added a stepvalue to increment the values not by one but also with higher increments

* Update readme

* Add github actions and update readme
  • Loading branch information
invissvenska authored Jan 30, 2021
1 parent 621cd56 commit b514eb0
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 19 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/android-library.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Android-Library CI

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Make Gradle executable
run: chmod +x ./gradlew
- name: Build with Gradle
run: ./gradlew build
- name: Build Debug APK
run: ./gradlew assembleDebug
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# NumberPickerPreference
[![API](https://img.shields.io/badge/API-19%2B-brightgreen.svg?style=flat)](https://android-arsenal.com/api?level=19) [![](https://jitpack.io/v/invissvenska/NumberPickerPreference.svg)](https://jitpack.io/#invissvenska/NumberPickerPreference) <span class="badge-buymeacoffee"><a href="https://www.paypal.com/paypalme/svenvandentweel/3" title="Donate to this project using Buy Me A Coffee"><img src="https://img.shields.io/badge/buy%20me%20a%20coffee-donate-yellow.svg" alt="Buy Me A Coffee donate button" /></a></span>
[![API](https://img.shields.io/badge/API-19%2B-brightgreen.svg?style=flat)](https://android-arsenal.com/api?level=19) [![](https://jitpack.io/v/invissvenska/NumberPickerPreference.svg)](https://jitpack.io/#invissvenska/NumberPickerPreference) <a href="https://github.com/invissvenska/NumberPickerPreference/actions"><img alt="Build Status" src="https://github.com/invissvenska/NumberPickerPreference/workflows/Android-Library%20CI/badge.svg"/></a> <span class="badge-buymeacoffee"><a href="https://www.paypal.com/paypalme/svenvandentweel/3" title="Donate to this project using Buy Me A Coffee"><img src="https://img.shields.io/badge/buy%20me%20a%20coffee-donate-yellow.svg" alt="Buy Me A Coffee donate button" /></a></span>

## Prerequisites

Expand Down Expand Up @@ -36,6 +36,7 @@ Add the NumberDialogPreference to the preferences.xml:
app:defaultValue="20" // optional, default is 0
app:numberPickerPreference_minValue="10" // optional, default is 0
app:numberPickerPreference_maxValue="60" // optional, default is 100
app:numberPickerPreference_stepValue="1" // optional, default is 1
app:numberPickerPreference_unitText=" another quantity" /> // optional, default is ""
```

Expand All @@ -56,6 +57,7 @@ public class SettingsFragment extends PreferenceFragmentCompat {
dialogPreference.getKey(),
dialogPreference.getMinValue(),
dialogPreference.getMaxValue(),
dialogPreference.getStepValue(),
dialogPreference.getUnitText()
);
dialogFragment.setTargetFragment(this, 0);
Expand All @@ -69,14 +71,15 @@ public class SettingsFragment extends PreferenceFragmentCompat {

## Usage

To create a NumberPickerPreference with default value of 20, min value of 10, max value of 60 and custom unit text:
To create a NumberPickerPreference with default value of 20, min value of 10, max value of 60, increments of 2 and custom unit text:
```xml
<nl.invissvenska.numberpickerpreference.NumberDialogPreference
android:key="preference_key"
android:title="Preference title"
app:defaultValue="20"
app:numberPickerPreference_minValue="10"
app:numberPickerPreference_maxValue="60"
app:numberPickerPreference_stepValue="2"
app:numberPickerPreference_unitText=" another quantity" />
```

Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ android {

dependencies {
implementation "androidx.appcompat:appcompat:$androidXVersion"
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
testImplementation 'junit:junit:4.13'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.13.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class MainFragment extends Fragment {
private TextView demo4;
private TextView demo5;
private TextView demo6;
private TextView demo7;

public MainFragment() {
//keep default constructor
Expand Down Expand Up @@ -55,6 +56,7 @@ public void onClick(View v) {
demo4 = view.findViewById(R.id.demo4);
demo5 = view.findViewById(R.id.demo5);
demo6 = view.findViewById(R.id.demo6);
demo7 = view.findViewById(R.id.demo7);

preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
return view;
Expand All @@ -69,5 +71,6 @@ public void onResume() {
demo4.setText("Demo_4 value is: " + preferences.getInt("demo_4", 20));
demo5.setText("Demo_5 value is: " + preferences.getInt("demo_5", 0));
demo6.setText("Demo_6 value is: " + preferences.getInt("demo_6", 0));
demo6.setText("Demo_7 value is: " + preferences.getInt("demo_7", 10));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public void onDisplayPreferenceDialog(Preference preference) {
dialogPreference.getKey(),
dialogPreference.getMinValue(),
dialogPreference.getMaxValue(),
dialogPreference.getStepValue(),
dialogPreference.getUnitText()
);
dialogFragment.setTargetFragment(this, 0);
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,20 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/demo5" />

<TextView
android:id="@+id/demo7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:textAppearance="?android:attr/textAppearanceMedium"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/demo6" />


<Button
android:id="@+id/fab"
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<nl.invissvenska.numberpickerpreference.NumberDialogPreference
android:key="demo_3"
android:title="Different values"
app:defaultValue="10"
app:numberPickerPreference_maxValue="60"
app:numberPickerPreference_minValue="10"
app:numberPickerPreference_unitText=" another quantity" />
Expand Down Expand Up @@ -50,11 +51,22 @@
<nl.invissvenska.numberpickerpreference.NumberDialogPreference
android:key="demo_6"
android:title="Different values"
app:defaultValue="10"
app:iconSpaceReserved="false"
app:numberPickerPreference_maxValue="60"
app:numberPickerPreference_minValue="10"
app:numberPickerPreference_unitText=" another quantity" />

<nl.invissvenska.numberpickerpreference.NumberDialogPreference
android:key="demo_7"
android:title="Step value"
app:defaultValue="10"
app:iconSpaceReserved="false"
app:numberPickerPreference_maxValue="160"
app:numberPickerPreference_minValue="10"
app:numberPickerPreference_stepValue="5"
app:numberPickerPreference_unitText=" another quantity" />

</PreferenceCategory>

</PreferenceScreen>
6 changes: 3 additions & 3 deletions 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:4.0.1'
classpath 'com.android.tools.build:gradle:4.1.2'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'

// NOTE: Do not place your application dependencies here; they belong
Expand All @@ -29,8 +29,8 @@ ext {
androidXVersion = '1.1.1'
androidXLifeCycleVersion = '2.2.0'

versionCode = 2
versionName = '1.0.1'
versionCode = 3
versionName = '1.0.2'
}

task clean(type: Delete) {
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Sun Jun 07 15:22:48 CEST 2020
#Sat Jan 30 14:44:45 CET 2021
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class NumberDialogPreference extends DialogPreference {
private static final Integer DEFAULT_VALUE = 0;
private Integer minValue = 0;
private Integer maxValue = 100;
private Integer stepValue = 1;
private String unitText = "";
private Integer value;

Expand All @@ -23,6 +24,7 @@ public NumberDialogPreference(Context context, AttributeSet attrs, int defStyleA
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.numberPickerPreference, 0, 0);
minValue = ta.getInt(R.styleable.numberPickerPreference_numberPickerPreference_minValue, minValue);
maxValue = ta.getInt(R.styleable.numberPickerPreference_numberPickerPreference_maxValue, maxValue);
stepValue = ta.getInt(R.styleable.numberPickerPreference_numberPickerPreference_stepValue, stepValue);
if (ta.getString(R.styleable.numberPickerPreference_numberPickerPreference_unitText) != null) {
unitText = ta.getString(R.styleable.numberPickerPreference_numberPickerPreference_unitText);
}
Expand Down Expand Up @@ -60,6 +62,10 @@ public Integer getMaxValue() {
return maxValue;
}

public Integer getStepValue() {
return stepValue;
}

public String getUnitText() {
return unitText;
}
Expand Down Expand Up @@ -94,7 +100,7 @@ protected void onSetInitialValue(Object defaultValue) {
if (defaultValue == null) {
setSerializedValue(getPersistedInt(DEFAULT_VALUE));
} else if (defaultValue instanceof String) {
setSerializedValue(Integer.valueOf((String)defaultValue));
setSerializedValue(Integer.valueOf((String) defaultValue));
} else {
setSerializedValue((Integer) defaultValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,28 @@
public class NumberPickerPreferenceDialogFragment extends CustomPreferenceDialogFragmentCompat {
private static final String ARG_MIN_VALUE = "min_value";
private static final String ARG_MAX_VALUE = "max_value";
private static final String ARG_STEP_VALUE = "step_value";
private static final String ARG_UNIT_TEXT = "unit_text";
private static final String SAVE_STATE_TIME = "save_state_time";

protected NumberPicker np;

private int minValue;
private int stepValue;

public static NumberPickerPreferenceDialogFragment newInstance(
@NonNull final String key,
final Integer minValue,
final Integer maxValue,
final Integer stepValue,
@Nullable final String unitText
) {
final NumberPickerPreferenceDialogFragment fragment = new NumberPickerPreferenceDialogFragment();
final Bundle b = new Bundle(4);
b.putString(ARG_KEY, key);
b.putInt(ARG_MIN_VALUE, minValue);
b.putInt(ARG_MAX_VALUE, maxValue);
b.putInt(ARG_STEP_VALUE, stepValue);
b.putString(ARG_UNIT_TEXT, unitText);
fragment.setArguments(b);
return fragment;
Expand All @@ -42,21 +48,43 @@ public void onCreate(Bundle savedInstanceState) {
text = savedInstanceState.getInt(SAVE_STATE_TIME);
}

final int minValue = getArguments().getInt(ARG_MIN_VALUE, 0);
minValue = getArguments().getInt(ARG_MIN_VALUE, 0);
final int maxValue = getArguments().getInt(ARG_MAX_VALUE, 100);
stepValue = getArguments().getInt(ARG_STEP_VALUE, 1);

final String[] values = getDisplayValues(minValue, maxValue, stepValue);

np = new NumberPicker(getActivity());
np.setMinValue(minValue);
np.setMaxValue(maxValue);
np.setValue(text);
np.setMinValue(0);
np.setMaxValue(values.length - 1);
np.setDisplayedValues(values);
np.setValue(getSelectedValue(values, text));
np.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
}

private Integer getSelectedValue(String[] values, Integer serializedValue) {
for (int i = 0; i < values.length; i++) {
if (serializedValue.equals(Integer.valueOf(values[i]))) {
return i;
}
}
return 0;
}

private String[] getDisplayValues(int min, int max, int step) {
int length = (max - min) / step + 1;
String[] arrayValues = new String[length];
for (int i = 0; i < length; i++) {
arrayValues[i] = String.valueOf(min + (i * step));
}
return arrayValues;
}

@Override
public void onDialogClosed(boolean positiveResult) {
if (positiveResult) {
Integer value;

value = np.getValue();
value = (minValue + (np.getValue() * stepValue));
if (getNumberPickerDialogPreference().callChangeListener(value)) {
getNumberPickerDialogPreference().setSerializedValue(value);
}
Expand Down
7 changes: 4 additions & 3 deletions numberpickerpreference/src/main/res/values-night/attrs.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="numberPickerPreference">
<attr name="numberPickerPreference_minValue" format="integer"/>
<attr name="numberPickerPreference_maxValue" format="integer"/>
<attr name="numberPickerPreference_unitText" format="string|reference"/>
<attr name="numberPickerPreference_minValue" format="integer" />
<attr name="numberPickerPreference_maxValue" format="integer" />
<attr name="numberPickerPreference_stepValue" format="integer" />
<attr name="numberPickerPreference_unitText" format="string|reference" />
</declare-styleable>
</resources>

0 comments on commit b514eb0

Please sign in to comment.