Search code examples
javaandroidseekbar

Android seekbar textView


I have 3 seekbar to change the background color. Each seekbar har a textView from 0-255 to show the colorcode in RGB. When I slide one of the seekbar, the RGB textView change on all. How can I specify, so only the textView that belong to the seekbar, that I slide will change?

  private SeekBar.OnSeekBarChangeListener seekBarChangeListener
        = new SeekBar.OnSeekBarChangeListener()
{

    TextView seekBar1Progress;
    TextView seekBar2Progress;
    TextView seekBar3Progress;


    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
                                  boolean fromUser) {

        updateBackground();
        seekBar1Progress = (TextView) findViewById(R.id.viewSlider1);
        seekBar1Progress.setText(" " + progress);
        seekBar2Progress = (TextView) findViewById(R.id.viewSlider2);
        seekBar2Progress.setText(" " + progress);
        seekBar3Progress = (TextView) findViewById(R.id.viewSlider3);
        seekBar3Progress.setText(" " + progress);
    }
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }

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

Solution

  • You are getting SeekBar object in OnProgressChanged method, you can compare its id to your individual SeekBar. Try below code:

    @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                                          boolean fromUser) {
                if(seekBar.getId()==R.id.seekBar1){
                    seekBar1Progress = (TextView) findViewById(R.id.viewSlider1);
                    seekBar1Progress.setText(" " + progress);
                }else if(seekBar.getId()==R.id.seekBar2){
                    seekBar2Progress = (TextView) findViewById(R.id.viewSlider2);
                    seekBar2Progress.setText(" " + progress);
                }else{
                    seekBar3Progress = (TextView) findViewById(R.id.viewSlider3);
                    seekBar3Progress.setText(" " + progress);
                }
    
    
            }
    

    Hope this will help!!