diff options
| author | Jules Aguillon | 2024-01-13 20:59:05 +0100 |
|---|---|---|
| committer | Jules Aguillon | 2024-01-13 20:59:05 +0100 |
| commit | eddf9c6c117449012e2aece5776694467e3483f0 (patch) | |
| tree | 0f503419b7beffc15f6f0903d95e68d1c4bdddd1 /srcs/juloo.keyboard2/prefs/IntSlideBarPreference.java | |
| parent | 148f3dfc052bc6a4161073e59505bd547ab347c3 (diff) | |
| download | unexpected-keyboard-eddf9c6c117449012e2aece5776694467e3483f0.tar.gz unexpected-keyboard-eddf9c6c117449012e2aece5776694467e3483f0.zip | |
Refactor: New namespace for preference classes
Diffstat (limited to 'srcs/juloo.keyboard2/prefs/IntSlideBarPreference.java')
| -rw-r--r-- | srcs/juloo.keyboard2/prefs/IntSlideBarPreference.java | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/srcs/juloo.keyboard2/prefs/IntSlideBarPreference.java b/srcs/juloo.keyboard2/prefs/IntSlideBarPreference.java new file mode 100644 index 0000000..bb6bf7d --- /dev/null +++ b/srcs/juloo.keyboard2/prefs/IntSlideBarPreference.java @@ -0,0 +1,120 @@ +package juloo.keyboard2.prefs; + +import android.content.Context; +import android.content.res.TypedArray; +import android.preference.DialogPreference; +import android.util.AttributeSet; +import android.view.View; +import android.view.ViewGroup; +import android.widget.LinearLayout; +import android.widget.TextView; +import android.widget.SeekBar; + +/* + ** IntSlideBarPreference + ** - + ** Open a dialog showing a seekbar + ** - + ** xml attrs: + ** android:defaultValue Default value (int) + ** min min value (int) + ** max max value (int) + ** - + ** Summary field allow to show the current value using %s flag + */ +public class IntSlideBarPreference extends DialogPreference + implements SeekBar.OnSeekBarChangeListener +{ + private LinearLayout _layout; + private TextView _textView; + private SeekBar _seekBar; + + private int _min; + + private String _initialSummary; + + public IntSlideBarPreference(Context context, AttributeSet attrs) + { + super(context, attrs); + _initialSummary = getSummary().toString(); + _textView = new TextView(context); + _textView.setPadding(48, 40, 48, 40); + _seekBar = new SeekBar(context); + _seekBar.setOnSeekBarChangeListener(this); + _min = attrs.getAttributeIntValue(null, "min", 0); + int max = attrs.getAttributeIntValue(null, "max", 0); + _seekBar.setMax(max - _min); + _layout = new LinearLayout(getContext()); + _layout.setOrientation(LinearLayout.VERTICAL); + _layout.addView(_textView); + _layout.addView(_seekBar); + } + + @Override + public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) + { + updateText(); + } + + @Override + public void onStartTrackingTouch(SeekBar seekBar) + { + } + + @Override + public void onStopTrackingTouch(SeekBar seekBar) + { + } + + @Override + protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) + { + int value; + + if (restorePersistedValue) + { + value = getPersistedInt(_min); + } + else + { + value = (Integer)defaultValue; + persistInt(value); + } + _seekBar.setProgress(value - _min); + updateText(); + } + + @Override + protected Object onGetDefaultValue(TypedArray a, int index) + { + return (a.getInt(index, _min)); + } + + @Override + protected void onDialogClosed(boolean positiveResult) + { + if (positiveResult) + persistInt(_seekBar.getProgress() + _min); + else + _seekBar.setProgress(getPersistedInt(_min) - _min); + + updateText(); + } + + protected View onCreateDialogView() + { + ViewGroup parent = (ViewGroup)_layout.getParent(); + + if (parent != null) + parent.removeView(_layout); + return (_layout); + } + + private void updateText() + { + String f = String.format(_initialSummary, _seekBar.getProgress() + _min); + + _textView.setText(f); + setSummary(f); + } +} |
