Skip to content

Instantly share code, notes, and snippets.

@ukcoderj
Last active August 29, 2015 14:25
Show Gist options
  • Save ukcoderj/dbc24179da9e0015ce10 to your computer and use it in GitHub Desktop.
Save ukcoderj/dbc24179da9e0015ce10 to your computer and use it in GitHub Desktop.
Android SDK: Converting SeekBar to Value (and back) when the seekers start value is greater than zero
/**
* The seekbars min value cannot be changed in the sdk. It has to be zero
* Total = 240seconds represented
* Start Value = 30 seconds
* Each interval of the seekbar = 5 seconds.
* <SeekBar
android:id="@+id/settings_seekbar_time_interval"
android:layout_span="2"
android:minWidth="200dp"
android:max="42"
android:minHeight="6dip"
android:maxHeight="6dip"/>
*
* /
/*
* These methods can be made more terse. They are deliberately "long hand".
* /
/**
* We have a seeker that allows user to select a number of seconds
* Zero value is 30 (seconds) and then increments 5 per value change
* This method will convert seconds to the seeker value.
* @param interval is the value from the seeker.
* @return The time converted to an int of the seeker value
*/
private int convertRealSecondsToTimeSeekerValue(int interval) {
int valuePerIncrementOfSeeker = 5;
int zeroValueOfSeeker = 30;
if(interval > zeroValueOfSeeker) {
int adjustment = zeroValueOfSeeker - valuePerIncrementOfSeeker;
return (interval - adjustment) / valuePerIncrementOfSeeker;
}
else
return 0;
}
/**
* We have a seeker that allows user to select a number of seconds
* Zero value is 30 (seconds) and then increments 5 per value change
* This method will take the value of the seeker and convert it to our number of seconds.
* @param seekerValue is the value of the seeker (as just moved by the user)
* @return The time that the seeker represents
*/
private int convertTimeSeekerToRealSeconds(int seekerValue) {
int valuePerIncrementOfSeeker = 5;
int zeroValueOfSeeker = 30;
if(seekerValue > 0) {
int adjustment = zeroValueOfSeeker - valuePerIncrementOfSeeker;
return adjustment + (seekerValue * valuePerIncrementOfSeeker);
}
else
return zeroValueOfSeeker;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment