diff options
| author | juloo | 2015-08-08 22:24:19 +0200 |
|---|---|---|
| committer | juloo | 2015-08-08 22:33:06 +0200 |
| commit | 6f09c971c0fe9eb0f03690bb3305164aa77c5a45 (patch) | |
| tree | 89ccb320731fd9ff1352397113f867e9275747e9 /srcs/juloo.common | |
| parent | fe432fd3ba4175bb0ae9ed2148ff5a739c5cd51a (diff) | |
| download | unexpected-keyboard-6f09c971c0fe9eb0f03690bb3305164aa77c5a45.tar.gz unexpected-keyboard-6f09c971c0fe9eb0f03690bb3305164aa77c5a45.zip | |
SideBarPreference + Continuing settings
Diffstat (limited to 'srcs/juloo.common')
| -rw-r--r-- | srcs/juloo.common/SlideBarPreference.java | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/srcs/juloo.common/SlideBarPreference.java b/srcs/juloo.common/SlideBarPreference.java new file mode 100644 index 0000000..6bfb33e --- /dev/null +++ b/srcs/juloo.common/SlideBarPreference.java @@ -0,0 +1,127 @@ +package juloo.common; + +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; + +/* +** SideBarPreference +** - +** Open a dialog showing a seekbar +** - +** xml attrs: +** android:defaultValue Default value (float) +** min min value (float) +** max max value (float) +** - +** Summary field allow to show the current value using %f or %s flag +*/ +public class SlideBarPreference extends DialogPreference + implements SeekBar.OnSeekBarChangeListener +{ + private static final int SEEKBAR_MAX = 1000; + + private LinearLayout _layout; + private TextView _textView; + private SeekBar _seekBar; + + private float _min; + private float _max; + private float _value; + + private String _initialSummary; + + public SlideBarPreference(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); + _seekBar.setMax(SEEKBAR_MAX); + _min = float_of_string(attrs.getAttributeValue(null, "min")); + _value = _min; + _max = Math.max(1f, float_of_string(attrs.getAttributeValue(null, "max"))); + _layout = new LinearLayout(getContext()); + _layout.setOrientation(LinearLayout.VERTICAL); + _layout.addView(_textView); + _layout.addView(_seekBar); + } + + @Override + public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) + { + _value = (progress * _max) / SEEKBAR_MAX + _min; + updateText(); + } + + @Override + public void onStartTrackingTouch(SeekBar seekBar) + { + } + + @Override + public void onStopTrackingTouch(SeekBar seekBar) + { + } + + @Override + protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) + { + if (restorePersistedValue) + { + _value = getPersistedFloat(_min); + } + else + { + _value = (Float)defaultValue; + persistFloat(_value); + } + _seekBar.setProgress((int)((_value - _min) * SEEKBAR_MAX / _max)); + updateText(); + } + + @Override + protected Object onGetDefaultValue(TypedArray a, int index) + { + return (a.getFloat(index, _min)); + } + + @Override + protected void onDialogClosed(boolean positiveResult) + { + if (positiveResult) + persistFloat(_value); + } + + protected View onCreateDialogView() + { + ViewGroup parent = (ViewGroup)_layout.getParent(); + + if (parent != null) + parent.removeView(_layout); + return (_layout); + } + + private void updateText() + { + String f = String.format(_initialSummary, _value); + + _textView.setText(f); + setSummary(f); + } + + private static float float_of_string(String str) + { + if (str == null) + return (0f); + return (Float.parseFloat(str)); + } +} |
