2

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

Owen Nel
  • 367
  • 3
  • 9
  • 21
  • See setOnSeekBarChangeListener here: http://stackoverflow.com/questions/8956218/android-seekbar-setonseekbarchangelistener – prompteus Oct 26 '15 at 15:34
  • 1
    I am sorry but that did not answer the entire question. I thank you for sending me in that direction and will definitely look at it. I also need to know how to change the position of the thumb by entering a value in an `EditText`. Thanks again – Owen Nel Oct 26 '15 at 15:38
  • About the edittext, see this: http://stackoverflow.com/questions/20802397/change-seekbar-progress-based-on-edittext-value – prompteus Oct 26 '15 at 15:43
  • @makadlcik Thank you so much... That really helps! I appreciate the help – Owen Nel Oct 26 '15 at 15:49

2 Answers2

10

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) {}
});
Roman Zhukov
  • 506
  • 5
  • 8
  • thank you for the answer. I will definitely use the code a try. Please can you be a little more specific with regards to the `TextWatcher`. I already implement a `TextWatcher` on another `EditText` but I am uncertain of the implementation. Which method do I use on the `SeekBar`? – Owen Nel Oct 26 '15 at 15:42
0

You will need to use the SeekBar object and an EditText instead of TextView as eidttext is used for taking user inputs. Then you have to add seekbarChangeListener on seekBar and TextWatcher on your EditText. I am giving below example.

    yourEditText.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) {
            //You can set value to seekbar here. obviously after checking if its valid and converting it into int/float.

              yourseekBar.setvalue(Integer.valueOf(s));
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

And add seekbarchanged listener like.

yourseekbar.setOnSeekBarChangeListener(new  SeekBar.OnSeekBarChangeListener() {

 @Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
      //convert progress into float if you want but it should be multiple of 10        

    yourEdittext.setText(progress+"");
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

}

});

As seekbar only accepts int value so you have to convert your float value to integer first.

Arslan Ashraf
  • 867
  • 7
  • 12