Search code examples
javaandroidtextviewseekbar

Adding multiple seekbar values together in a textview


Having trouble adding values together from multiple Seekbars. Trying to send the total to a total_view but it keeps showing up as 0. Im using two seekbars and the total should change depending on the movement on the bar so Im using onSeekChangeListener

public class CalculateGradeActivity extends Activity {
    TextView text_view;  //shows percentage
    TextView text_view2; //shows percentage

    TextView total_view; //show total from text_view and text_view2

    SeekBar seek_bar1;
    SeekBar seek_bar2;

    int exam_grade;
    int homework;

    int total;

    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.calculate_grade_view);
        seek_bar1 = (SeekBar) findViewById(R.id.seekBarExam);
        seek_bar2 = (SeekBar) findViewById(R.id.seekBarHomework);


        text_view = (TextView) findViewById(R.id.percentageSeekbar1);
        text_view2 = (TextView) findViewById(R.id.percentageSeekbar2);

        total_view = (TextView) findViewById(R.id.numeric_grade_id_output);


        seekbar();


    }



    public void seekbar() {


        int progress = seek_bar1.getProgress();
        text_view.setText(String.valueOf(progress));

        int progress2 = seek_bar2.getProgress();
        text_view2.setText(String.valueOf(progress2));


        total = exam_grade + homework;

        seek_bar1.setOnSeekBarChangeListener(
            new SeekBar.OnSeekBarChangeListener() {

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

                    text_view.setText(String.valueOf(progress) + "%");

                    exam_grade = progress;
                    total_view.setText(String.valueOf(total));

                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {

                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {

                }
            }
        );

        seek_bar2.setOnSeekBarChangeListener(
            new SeekBar.OnSeekBarChangeListener(){

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

                    text_view2.setText(String.valueOf(progress)+"%");
                    homework = progress;
                    total_view.setText(String.valueOf(total));
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {

                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {


                }
            }
        );
    }
}

Solution

  • Every time you change a value you have to recalculate the total before you display it.

    seek_bar1.setOnSeekBarChangeListener(
    
            new SeekBar.OnSeekBarChangeListener() {
    
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    
                    text_view.setText(String.valueOf(progress) + "%");
    
                    exam_grade = progress;
                    total = exam_grade + homework;
                    total_view.setText(String.valueOf(total));
    
                }
    
                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {
    
                }
    
                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
    
                }
            }
    );
    
    seek_bar2.setOnSeekBarChangeListener(
            new SeekBar.OnSeekBarChangeListener(){
    
    
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    
                    text_view2.setText(String.valueOf(progress)+"%");
                    homework = progress;
                    total = exam_grade + homework;
                    total_view.setText(String.valueOf(total));
                }
    
                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {
    
                }
    
                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
    
    
                }
            }
    );
    

    ja