Q:

Android cyclic listeners

I've got a small issue in my code:
I've a SeekBar and an EditText in a layout.
What I want to do is to set the EditText value when the SeekBar changes and set the SeekBar progress when the EditText changes.
So far, I've the following code:
    SeekBar seek_coagulation = (SeekBar) findViewById(R.id.seek_coagulation);
    EditText edit_coagulation = (EditText) findViewById(R.id.edit_coagulation);
    seek_coagulation.incrementProgressBy(1);
    seek_coagulation.setMax(1000);
    seek_coagulation.setOnSeekBarChangeListener( new OnSeekBarChangeListener() {
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
        {
            double value = progress / 100.0;
            edit_coagulation.setText(String.valueOf(value));
        }

        public void onStartTrackingTouch(SeekBar seekBar)
        {
        }

        public void onStopTrackingTouch(SeekBar seekBar)
        {
        }
    });
    edit_coagulation.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s)
        {
            double value = (s != null && s.toString().length() > 0) ? Double.parseDouble(s.toString().replace(',', '.')) : 0D;
            int progress = (int) (value * 100);
            seek_coagulation.setProgress(progress);
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after)
        {
        }

        public void onTextChanged(CharSequence s, int start, int before, int count)
        {
        }
    });

The first case (SeekBar to EditText) works well, but not the second.
The problem is that it is a loop (SeekBar to EditText to SeekBar to EditText etc.).
Is there a way to fix that ?

A:

I suggest to use two boolean parameters to lock changes, like this:
    boolean mIsTextLocked = false;    
    boolean mIsSeekBarLocked = false;

    seek_coagulation = (SeekBar) findViewById(R.id.seek_coagulation);
    EditText edit_coagulation = (EditText) findViewById(R.id.edit_coagulation);
    seek_coagulation.incrementProgressBy(1);
    seek_coagulation.setMax(1000);
    seek_coagulation.setOnSeekBarChangeListener( new OnSeekBarChangeListener() {
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
        {
            if(!mIsSeekBarLocked){
               mIsTextLocked = true;
               double value = progress / 100.0;
               edit_coagulation.setText(String.valueOf(value));
            }
        }

        public void onStartTrackingTouch(SeekBar seekBar)
        {
        }

        public void onStopTrackingTouch(SeekBar seekBar)
        {
           mIsTextLocked = false;
        }
    });
    edit_coagulation.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s)
        {
           if(!mIsTextLocked){
            mIsSeekBarLocked = true;
            double value = (s != null && s.toString().length() > 0) ?Double.parseDouble(s.toString().replace(',', '.')) : 0D;
            int progress = (int) (value * 100);
            seek_coagulation.setProgress(progress);
            mIsSeekBarLocked = false;
           }
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after)
        {
        }

        public void onTextChanged(CharSequence s, int start, int before, int count)
        {
        }
    });

I haven't tested it but it should work.

