title: Limit multiple seekbars to a maximum of 100% overall
body: <p>I have 5 seekbars which I want the user to be able to manipulate.  The seekbars are dependent on one another so if one is set to 100% all others would have to be 0%.  I have been able to implement this using the code found in <a href="https://stackoverflow.com/a/11559614/411224">this post</a> however when manipulating the seekbars it is glitchy and the seekbar jumps around. For example, by default I set all the seekbars to 20%.  In order to move seekbar_1 20% higher, you lower the value of another seekbar (say seekbar_2 by 20%).  This frees up 20% of which seekbar_1 can than use.  My issue is with the actual touch and move of the seekbar, it jumps around I think because of the various calculations I do within the onProgressChanged method.  I've attached my code below. Is there an easier way to implement this?  Any help would be much appreciated.</p>
  
  <pre><code>public class MainActivity extends Activity implements OnSeekBarChangeListener{
  public SeekBar sb1,sb2,sb3,sb4,sb5;
  public TextView tv1,tv2,tv3,tv4,tv5;
  public int mPercentRemaining, mPercentTotal, mPercentUsed;
  public int mCurrentPercent;
  
  @Override
  public void onCreate(Bundle savedInstanceState) 
  {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  sb1=(SeekBar) findViewById(R.id.seekBar1);
  sb2=(SeekBar) findViewById(R.id.seekBar2);
  sb3=(SeekBar) findViewById(R.id.seekBar3);
  sb4=(SeekBar) findViewById(R.id.seekBar4);
  sb5=(SeekBar) findViewById(R.id.seekBar5);
  
  tv1=(TextView) findViewById(R.id.textView1);
  tv2=(TextView) findViewById(R.id.textView2);
  tv3=(TextView) findViewById(R.id.textView3);
  tv4=(TextView) findViewById(R.id.textView4);
  tv5=(TextView) findViewById(R.id.textView5);
  
  sb1.setOnSeekBarChangeListener(this);
  sb2.setOnSeekBarChangeListener(this);
  sb3.setOnSeekBarChangeListener(this);
  sb4.setOnSeekBarChangeListener(this);
  sb5.setOnSeekBarChangeListener(this);
  
  mPercentTotal = 100;
  mPercentUsed = 100; //Seekbars are all set to 20% by default
  mPercentRemaining = mPercentTotal - mPercentUsed;
  
  }
  
  @Override
  public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) 
  {
  switch (seekBar.getId()) 
  {
  case R.id.seekBar1:
      if((progress - mCurrentPercent) &lt;= mPercentRemaining || mCurrentPercent &gt;= progress)
          tv1.setText(progress);
      else
      {
          seekBar.setProgress(mCurrentPercent);
          tv1.setText(mCurrentPercent);
      }
      break;
  case R.id.seekBar2:
      if((progress - mCurrentPercent) &lt;= mPercentRemaining || mCurrentPercent &gt;= progress)
          tv2.setText(progress);
      else
      {
          seekBar.setProgress(mCurrentPercent);
          tv2.setText(mCurrentPercent);
      }
      break;
  case R.id.seekBar3:
      if((progress - mCurrentPercent) &lt;= mPercentRemaining || mCurrentPercent &gt;= progress)
          tv3.setText(progress);
      else
      {
          seekBar.setProgress(mCurrentPercent);
          tv3.setText(mCurrentPercent);
      }
      break;
  case R.id.seekBar4:
      if((progress - mCurrentPercent) &lt;= mPercentRemaining || mCurrentPercent &gt;= progress)
          tv4.setText(progress);
      else
      {
          seekBar.setProgress(mCurrentPercent);
          tv4.setText(mCurrentPercent);
      }
      break;
  case R.id.seekBar5:
      if((progress - mCurrentPercent) &lt;= mPercentRemaining || mCurrentPercent &gt;= progress)
          tv5.setText(progress);
      else
      {
          seekBar.setProgress(mCurrentPercent);
          tv5.setText(mCurrentPercent);
      }
      break;
  
  }
  
  mPercentUsed = sb1.getProgress() + sb2.getProgress() + sb3.getProgress() + sb4.getProgress() + sb5.getProgress();
  mPercentRemaining = mPercentTotal - mPercentUsed;
  
  }
  
  @Override
  public void onStartTrackingTouch(SeekBar arg0) 
  {
  mCurrentProgress = seekBar.getProgress();
  }
  
  @Override
  public void onStopTrackingTouch(SeekBar arg0) 
  {
  
  // TODO Auto-generated method stub
  }}
  </code></pre>