Multiple SeekBars listener

clement picture clement · Jan 3, 2012 · Viewed 12k times · Source

I have multiple seekbar on the same view :

barheures = (SeekBar)findViewById(R.id.barheures); // make seekbar object
        barheures.setOnSeekBarChangeListener(this); // set seekbar listener.
        // since we are using this class as the listener the class is "this"
        barminutes = (SeekBar)findViewById(R.id.barminutes);
        barminutes.setOnSeekBarChangeListener(this); 

And here is the listener:

@Override
public void onProgressChanged(SeekBar bar, int progress, boolean fromUser) {
    // TODO Auto-generated method stub
    Log.v("", "" + bar);
    textMinutes.setText("" + progress + "Minute(s)" );
    textHours.setText("" + progress + "Heure(s)" );
}

I wanna make something different if the first OR the second bar have moved into the same listener (is it the good practice?), but how to?? Here I have the app that don't do what I want

Answer

King RV picture King RV · Jan 4, 2012

Now then you have 2 seekbars with the following ids:

  1. barheures - its id: R.id.barheures
  2. barminutes - its id: R.id.barminutes

Now in the onProgressChanged(SeekBar bar, int progress, boolean fromUser) method, Do the following:

@Override
public void onProgressChanged(SeekBar bar, int progress, boolean fromUser) {
    // TODO Auto-generated method stub
    Log.v("", "" + bar);

    switch (bar.getId()) {

    case R.id.barheures:
        textHours.setText("" + progress + "Heure(s)");
        break;

    case R.id.barminutes:
        textMinutes.setText("" + progress + "Minute(s)");
        break;
    }
}