I am working on a very simple Android app to convert temperature using SeekBar.
There are 2 seek bars, one to select the temperature in Celsius, and the another one to select temperature in Fahrenheit. They are both initialized in the code as follow:
private SeekBar celsiusSeekbar;
private TextView celsiusValueText;
private SeekBar fahrenheitSeekBar;
private TextView fahrenheitValueText;
private ArrayList<Integer> fahrenheitProgress;
private ArrayList<Integer> celsiusProgress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.celsiusSeekbar = (SeekBar) findViewById(R.id.celsius_seekbar);
this.fahrenheitSeekBar = (SeekBar) findViewById(R.id.fahrenheit_seekbar);
this.celsiusValueText = (TextView) findViewById(R.id.celsius_value);
this.fahrenheitValueText = (TextView) findViewById(R.id.fahrenheit_value);
this.initCelsiusProgress();
this.celsiusSeekbar.setMax(200);
this.celsiusSeekbar.setProgress(100);
this.celsiusValueText.setText("0°C");
this.celsiusSeekbar.incrementProgressBy(1);
this.celsiusSeekbar.setOnSeekBarChangeListener(new CelsiusSeekBarChangedListener());
this.initFahrenheitProgress();
this.fahrenheitSeekBar.setMax(360);
this.fahrenheitSeekBar.setProgress(180);
this.fahrenheitSeekBar.incrementProgressBy(1);
this.fahrenheitValueText.setText("32°F");
this.fahrenheitSeekBar.setOnSeekBarChangeListener(new FahrenheitSeekBarChangedListener());
}
private void initFahrenheitProgress() {
fahrenheitProgress = new ArrayList<>();
for(int i = 0; i <= 360; i++)
fahrenheitProgress.add(i, i - 148);
}
private void initCelsiusProgress(){
celsiusProgress = new ArrayList<>();
for(int i = 0; i <= 200; i++)
celsiusProgress.add(i, i - 100);
}
private int convertCelsiusProgressToValue(int progress){ return celsiusProgress.get(progress); }
private int convertFahrenheitProgressToValue(int progress){ return fahrenheitProgress.get(progress); }
private class CelsiusSeekBarChangedListener implements SeekBar.OnSeekBarChangeListener {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
System.out.println("progress : " + progress + " | seekbar progresss : " + seekBar.getProgress());
//Update celsius temperature
celsiusValueText.setText(convertCelsiusProgressToValue(progress) + "°C");
//Update fahrenheit temperature
fahrenheitValueText = (TextView) findViewById(R.id.fahrenheit_value);
int value = (int) (convertCelsiusProgressToValue(progress) * 1.8 + 32);
fahrenheitValueText.setText(value + "°F");
fahrenheitSeekBar.setProgress(fahrenheitProgress.indexOf(new Integer(value)));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
}
private class FahrenheitSeekBarChangedListener implements SeekBar.OnSeekBarChangeListener {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//Update fahrenheit temperature
fahrenheitValueText.setText(convertFahrenheitProgressToValue(progress) + "°F");
//Update celsius temperature
celsiusValueText = (TextView) findViewById(R.id.celsius_value);
int value = (int) ((convertFahrenheitProgressToValue(progress)-32) / 1.8);
celsiusValueText.setText(value + "°C");
celsiusSeekbar.setProgress(celsiusProgress.indexOf(new Integer(value)));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
}
The first seek bar (for Celsius temperature) is incremented by one between 0°C and -17°C, but then it is incremented by 5 and I don't understand why. Here is a little gif to show you the problem: GIF
I would like the seek bar to be incremented by one every time.
If anyone has an idea to explain why it's not working properly, I'd be very grateful !
I have an idea what might be wrong here. When you are moving your Celsius bar, fahrenheitSeekBar.setProgress(fahrenheitProgress.indexOf(new Integer(value)));
this is called at the end, and immediately the onProgressChanged
in FahrenheitListener
is called (which is then calling celsiusBar.setProgress()
and the circle starts).
I suggest wrapping both listeners (onProgressChanged
methods) in
if(fromUser){
//your whole listener
}
fromUser
is true if the user makes the change, in the app, thus changes made programatically are gonna be ignored.