Skip to content

Commit

Permalink
Added the ability to adjust the SeekBarPreference options in the Pref…
Browse files Browse the repository at this point in the history
…erenceActivity by pressing left and right on the directional pad.
  • Loading branch information
forteri76 committed May 1, 2014
1 parent 02a29d3 commit 1a37427
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 77 deletions.
15 changes: 14 additions & 1 deletion src/com/efortin/frozenbubble/PreferencesActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,13 @@
public class PreferencesActivity extends PreferenceActivity{

private Preferences mPrefs;
private SeekBarPreference collisionSlider;
private SeekBarPreference difficultySlider;

private void cleanUp() {
mPrefs = null;
collisionSlider = null;
difficultySlider = null;
}

public static void getFrozenBubblePrefs(Preferences prefs, SharedPreferences sp) {
Expand Down Expand Up @@ -97,6 +101,8 @@ protected void onCreate(Bundle savedInstanceState) {
mPrefs = new Preferences();
setDefaultPreferences();
addPreferencesFromResource(R.layout.activity_preferences_screen);
collisionSlider = (SeekBarPreference) findPreference("collision_option");
difficultySlider = (SeekBarPreference) findPreference("difficulty_option");
}

@Override
Expand All @@ -108,11 +114,18 @@ protected void onDestroy() {

@Override
public boolean onKeyDown(int keyCode, KeyEvent msg) {
boolean result = false;

if (keyCode == KeyEvent.KEYCODE_BACK) {
savePreferences();
finish();
}
return super.onKeyDown(keyCode, msg);
else {
result = collisionSlider.onKey(keyCode, msg);
result |= difficultySlider.onKey(keyCode, msg);
}

return result || super.onKeyDown(keyCode, msg);
}

private void savePreferences() {
Expand Down
176 changes: 100 additions & 76 deletions src/com/efortin/frozenbubble/SeekBarPreference.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import android.preference.Preference;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -98,33 +99,6 @@ public SeekBarPreference(Context context, AttributeSet attrs, int defStyle) {
initPreference(context, attrs);
}

private void initPreference(Context context, AttributeSet attrs) {
setValuesFromXml(attrs);
mSeekBar = new SeekBar(context, attrs);
mSeekBar.setMax(mMaxValue - mMinValue);
mSeekBar.setOnSeekBarChangeListener(this);
}

private void setValuesFromXml(AttributeSet attrs) {
mMaxValue = attrs.getAttributeIntValue(ANDROIDNS, "max", 100);
mMinValue = attrs.getAttributeIntValue(FROZENBUBBLENS, "min", 0);

mUnitsLeft = getAttributeStringValue(attrs, FROZENBUBBLENS,
"unitsLeft", "");
String units = getAttributeStringValue(attrs, FROZENBUBBLENS, "units", "");
mUnitsRight = getAttributeStringValue(attrs, FROZENBUBBLENS,
"unitsRight", units);

try {
String newInterval = attrs.getAttributeValue(FROZENBUBBLENS, "interval");
if(newInterval != null)
mInterval = Integer.parseInt(newInterval);
}
catch(Exception e) {
Log.e(TAG, "Invalid interval value", e);
}
}

private String getAttributeStringValue(AttributeSet attrs, String namespace,
String name, String defaultValue) {
String value = attrs.getAttributeValue(namespace, name);
Expand All @@ -134,22 +108,14 @@ private String getAttributeStringValue(AttributeSet attrs, String namespace,
return value;
}

@Override
protected View onCreateView(ViewGroup parent){
RelativeLayout layout = null;

try {
LayoutInflater mInflater = (LayoutInflater)getContext().
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = (RelativeLayout)mInflater.
inflate(R.layout.seek_bar_preference, parent, false);
}
catch(Exception e)
{
Log.e(TAG, "Error creating seek bar preference", e);
}

return layout;
private void initPreference(Context context, AttributeSet attrs) {
setValuesFromXml(attrs);
mSeekBar = new SeekBar(context, attrs);
mSeekBar.setMax(mMaxValue - mMinValue);
mSeekBar.setOnSeekBarChangeListener(this);
mSeekBar.setFocusable(true);
mSeekBar.setFocusableInTouchMode(true);
this.setSelectable(true);
}

@Override
Expand Down Expand Up @@ -179,30 +145,48 @@ public void onBindView(View view) {
updateView(view);
}

/**
* Update a SeekBarPreference view with our current state.
* @param view
*/
protected void updateView(View view) {
try {
RelativeLayout layout = (RelativeLayout)view;
@Override
protected View onCreateView(ViewGroup parent){
RelativeLayout layout = null;

mStatusText = (TextView)layout.findViewById(R.id.seekBarPrefValue);
mStatusText.setText(String.valueOf(mCurrentValue));
mStatusText.setMinimumWidth(30);
try {
LayoutInflater mInflater = (LayoutInflater)getContext().
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = (RelativeLayout)mInflater.
inflate(R.layout.seek_bar_preference, parent, false);
}
catch(Exception e)
{
Log.e(TAG, "Error creating seek bar preference", e);
}

mSeekBar.setProgress(mCurrentValue - mMinValue);
return layout;
}

TextView unitsRight =
(TextView)layout.findViewById(R.id.seekBarPrefUnitsRight);
unitsRight.setText(mUnitsRight);
@Override
protected Object onGetDefaultValue(TypedArray ta, int index) {
int defaultValue = ta.getInt(index, DEFAULT_VALUE);
return defaultValue;
}

TextView unitsLeft =
(TextView)layout.findViewById(R.id.seekBarPrefUnitsLeft);
unitsLeft.setText(mUnitsLeft);
} catch(Exception e) {
Log.e(TAG, "Error updating seek bar preference", e);
public boolean onKey(int keyCode, KeyEvent event) {
if (mSeekBar.isSelected() && (event.getAction() == KeyEvent.ACTION_DOWN)) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_LEFT:
if (mCurrentValue > mMinValue) {
mSeekBar.setProgress((mCurrentValue - mMinValue) - mInterval);
return true;
}
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
if (mCurrentValue < mMaxValue) {
mSeekBar.setProgress((mCurrentValue - mMinValue) + mInterval);
return true;
}
break;
}
}
return false;
}

@Override
Expand All @@ -229,20 +213,6 @@ else if(mInterval != 1 && newValue % mInterval != 0)
persistInt(newValue);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
notifyChanged();
}

@Override
protected Object onGetDefaultValue(TypedArray ta, int index) {
int defaultValue = ta.getInt(index, DEFAULT_VALUE);
return defaultValue;
}

@Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
if(restoreValue) {
Expand All @@ -262,7 +232,61 @@ protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
}
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
notifyChanged();
}

public void setDefaultValue(int newValue) {
mCurrentValue = newValue;
}

private void setValuesFromXml(AttributeSet attrs) {
mMaxValue = attrs.getAttributeIntValue(ANDROIDNS, "max", 100);
mMinValue = attrs.getAttributeIntValue(FROZENBUBBLENS, "min", 0);

mUnitsLeft = getAttributeStringValue(attrs, FROZENBUBBLENS,
"unitsLeft", "");
String units = getAttributeStringValue(attrs, FROZENBUBBLENS, "units", "");
mUnitsRight = getAttributeStringValue(attrs, FROZENBUBBLENS,
"unitsRight", units);

try {
String newInterval = attrs.getAttributeValue(FROZENBUBBLENS, "interval");
if(newInterval != null)
mInterval = Integer.parseInt(newInterval);
}
catch(Exception e) {
Log.e(TAG, "Invalid interval value", e);
}
}

/**
* Update a SeekBarPreference view with our current state.
* @param view
*/
protected void updateView(View view) {
try {
RelativeLayout layout = (RelativeLayout)view;

mStatusText = (TextView)layout.findViewById(R.id.seekBarPrefValue);
mStatusText.setText(String.valueOf(mCurrentValue));
mStatusText.setMinimumWidth(30);

mSeekBar.setProgress(mCurrentValue - mMinValue);

TextView unitsRight =
(TextView)layout.findViewById(R.id.seekBarPrefUnitsRight);
unitsRight.setText(mUnitsRight);

TextView unitsLeft =
(TextView)layout.findViewById(R.id.seekBarPrefUnitsLeft);
unitsLeft.setText(mUnitsLeft);
} catch(Exception e) {
Log.e(TAG, "Error updating seek bar preference", e);
}
}
}

0 comments on commit 1a37427

Please sign in to comment.