leithdm
4/20/2020 - 7:03 AM

SeekBar: [SeekBar] with a [ListView] to display times table

public class MainActivity extends AppCompatActivity {

    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = findViewById(R.id.listView);
        SeekBar seekBar = findViewById(R.id.seekBar);
        int startingPosition = 10;
        int max = 20;

        seekBar.setMax(max);
        setValues(startingPosition);
        seekBar.setProgress(startingPosition);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                int min = 1;
                int timesTableNumber;

                if (i < min) {
                    timesTableNumber = min;
                    seekBar.setProgress(min); //prevents it from hitting the edge of seekbar
                } else {
                    timesTableNumber = i;
                }
                Log.i("Seekbar value", Integer.toString(timesTableNumber));
                setValues(timesTableNumber);
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) { }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) { }
        });
    }

    public void setValues(int factor) {
        ArrayList<String> valuesArray = new ArrayList<>();
        for (int i = 1; i <21 ; i++) {
            valuesArray.add(Integer.toString(i*factor));
        }
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, valuesArray);
        listView.setAdapter(arrayAdapter);
    }
}