Code example for SeekBar

Methods: getMaxsetProgresssetOnSeekBarChangeListener, setLayoutParams, setMax, setPadding

0
        currentValue.setText(String.format("%d", getProgress(defaultValue)));
        LayoutParams params = new LayoutParams(25, LayoutParams.WRAP_CONTENT, 1);
        currentValue.setLayoutParams(params);
        currentValue.setGravity(Gravity.CENTER | Gravity.RIGHT);
 
        bar = new SeekBar(getContext());
        bar.setPadding(5, 5, 5, 5);
        bar.setMax(maxValue - minValue);
        bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override 
            public void onStopTrackingTouch(SeekBar seekBar) {
            } 
            @Override 
            public void onStartTrackingTouch(SeekBar seekBar) {
            } 
            @Override 
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                currentValue.setText(Integer.valueOf(getValue()).toString());
            } 
        }); 
        bar.setProgress(getProgress(defaultValue));
        bar.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT, 4));
 
        container.addView(bar);
        container.addView(currentValue);
        layout.addView(container);
        return layout;
    } 
     
    private int getValue() 
    { 
        if (bar == null) {
            return minValue;
        } 
        return bar.getProgress() + minValue;
    } 
    private int getProgress(int value)
    { 
        if (bar == null) {
            return 0; 
        } 
        if (value > maxValue) {
            return bar.getMax();
        } 
        if (value < minValue) {
            return 0; 
        } 
        return value - minValue;