Android SeekBar: Changing the value

12,781

First of all, you should set max progress:

strokeWidthBar.setMax(<your max progress>);
strokeWidthBar.setProgress(0); // Set initial progress value

Tracking changes of SeekBar

seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
        textView.setText(String.valueOf(progress));
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
});

And for changing SeekBar progress from EditText, you can add a TextWatcher to your EditText

Update For the TextWatcher you can use something like this:

editText.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // Convert text to integer. Do you already use editText.setInputType(InputType.TYPE_CLASS_NUMBER), don't you?
        Integer enteredProgress = Integer.valueOf(s.toString());
        seekBar.setProgress(enteredProgress);
    }

    @Override
    public void afterTextChanged(Editable s) {}
});
Share:
12,781
Owen Nel
Author by

Owen Nel

Updated on June 14, 2022

Comments

  • Owen Nel
    Owen Nel almost 2 years

    I am writing a block of code where I am programmatically creating a SeekBar to capture a percentage. The percentage is then displayed in a TextView. I have written the code to display both on screen:

    public static void pebbleTypeHandler(LinearLayout layout, final TaskSwitcherTask task, PData value)
    {
        // Create an android seek bar to allow thumb drag
        // Have a text view to display the value
        Log.v(PebbleTypeHandlerPercentage.class.toString(), "-----> Percentage");
    
        SeekBar seekbar = new SeekBar(task.getParent());
        TextView textView = new TextView(task.getParent());
        textView.setTextColor(ColorStateList.valueOf(0xFFFFFFFF));
        textView.setText(((PPercentage) value).getValue() + "%");
    
        layout.addView(textView);
        layout.addView(seekbar);
    
        int id = UtilityMethods.getNextId();
        value.getMeta().getDataPebble().setId(id);
        textView.setId(id);
        id = UtilityMethods.getNextId();
        seekbar.setId(id);
    
        Log.v(PebbleTypeHandlerLevel.class.toString(), "-----> TextView ID: " + textView.getId());
        Log.v(PebbleTypeHandlerLevel.class.toString(), "-----> SeekBar ID: " + seekbar.getId());
    }
    

    How can I handle the SeekBar change. I want the TextView value to change depending on the thumb position. The maximum percentage is 100% and the minimum is 0%.

    I would also like to set an onClickListener on the TextView and update the position of the SeekBar based on the entered value to allow for more accurate values to be entered. The value in my data carrier, called Pebble, is a float as I would like to allow Float numbers.

    Any help would be appreciated.

    Regards,

    Owen