-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
159 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
app/src/main/java/org/sportiduino/app/NumberPickerPreference.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package org.sportiduino.app; | ||
|
||
import android.content.Context; | ||
import android.content.DialogInterface; | ||
import android.content.res.TypedArray; | ||
import android.util.AttributeSet; | ||
import android.util.Log; | ||
import android.view.View; | ||
|
||
import android.widget.NumberPicker; | ||
|
||
import androidx.preference.DialogPreference; | ||
|
||
public class NumberPickerPreference extends DialogPreference { | ||
|
||
private NumberPicker picker; | ||
private Integer initialValue; | ||
//private int minValue = 0; // Default minimum | ||
//private int maxValue = 10; // Default maximum | ||
|
||
public NumberPickerPreference(Context context, AttributeSet attrs) { | ||
super(context, attrs); | ||
setDialogLayoutResource(R.layout.preference_number_picker); | ||
|
||
// Read custom min and max attributes from XML if provided | ||
//TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.NumberPickerPreference); | ||
//minValue = a.getInt(R.styleable.NumberPickerPreference_minValue, minValue); | ||
//maxValue = a.getInt(R.styleable.NumberPickerPreference_maxValue, maxValue); | ||
//a.recycle(); | ||
} | ||
|
||
@Override | ||
protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) { | ||
int def = defaultValue instanceof Integer ? (Integer)defaultValue : 0; | ||
initialValue = restorePersistedValue ? getPersistedInt(def) : def; | ||
setSummary(initialValue.toString()); | ||
} | ||
|
||
@Override | ||
protected Object onGetDefaultValue(TypedArray a, int index) { | ||
return a.getInt(index, 0); | ||
} | ||
|
||
public int getValue() { | ||
return initialValue != null ? initialValue : 0; | ||
} | ||
|
||
public void setValue(int value) { | ||
initialValue = value; | ||
persistInt(value); | ||
setSummary(initialValue.toString()); | ||
} | ||
|
||
//public int getMinValue() { | ||
// return minValue; | ||
//} | ||
|
||
//public int getMaxValue() { | ||
// return maxValue; | ||
//} | ||
} | ||
|
53 changes: 53 additions & 0 deletions
53
app/src/main/java/org/sportiduino/app/NumberPickerPreferenceDialogFragmentCompat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.sportiduino.app; | ||
|
||
import android.content.Context; | ||
import android.os.Bundle; | ||
import android.util.Log; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.NumberPicker; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
import androidx.preference.PreferenceDialogFragmentCompat; | ||
|
||
public class NumberPickerPreferenceDialogFragmentCompat extends PreferenceDialogFragmentCompat { | ||
|
||
private NumberPicker picker; | ||
|
||
public static NumberPickerPreferenceDialogFragmentCompat newInstance(String key) { | ||
final NumberPickerPreferenceDialogFragmentCompat fragment = new NumberPickerPreferenceDialogFragmentCompat(); | ||
final Bundle b = new Bundle(1); | ||
b.putString(ARG_KEY, key); | ||
fragment.setArguments(b); | ||
return fragment; | ||
} | ||
|
||
@Override | ||
protected View onCreateDialogView(@NonNull Context context) { | ||
LayoutInflater inflater = LayoutInflater.from(context); | ||
View view = inflater.inflate(R.layout.preference_number_picker, null); | ||
|
||
picker = view.findViewById(R.id.pref_num_picker); | ||
NumberPickerPreference preference = (NumberPickerPreference) getPreference(); | ||
picker.setMinValue(getResources().getInteger(R.integer.countdown_timer_min)); | ||
picker.setMaxValue(getResources().getInteger(R.integer.countdown_timer_max)); | ||
picker.setValue(preference.getValue()); | ||
Log.d("onCreateDialogView", String.valueOf(preference.getValue())); | ||
|
||
return view; | ||
} | ||
|
||
@Override | ||
public void onDialogClosed(boolean positiveResult) { | ||
if (positiveResult && picker != null) { | ||
int value = picker.getValue(); | ||
Log.d("onDialogClosed", String.valueOf(value)); | ||
NumberPickerPreference preference = (NumberPickerPreference) getPreference(); | ||
if (preference.callChangeListener(value)) { | ||
preference.setValue(value); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:padding="16dp" | ||
android:orientation="vertical"> | ||
|
||
<android.widget.NumberPicker | ||
android:id="@+id/pref_num_picker" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" /> | ||
</LinearLayout> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<integer name="countdown_timer_default">2</integer> | ||
<integer name="countdown_timer_max">10</integer> | ||
<integer name="countdown_timer_min">0</integer> | ||
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters