Well, I looked at that example again, and it doesn't use a separate
thread, just a Runnable (which they unhelpfully call a "task").

I hacked on the code a bit and got it down to a pretty tight set of
logic.  This is it, including the lash-up to a SeekBar:

--- Logic in onCreate:

        SeekBar weightSeekbar =
(SeekBar)findViewById(R.id.WeightSeekbar);
        View decrementWeightOne = (View)
findViewById(R.id.DecrementWeightOne);
        View incrementWeightOne = (View)
findViewById(R.id.IncrementWeightOne);
        decrementWeightOne.setOnTouchListener(new
RepeatListener(weightSeekbar, 50, -1));
        incrementWeightOne.setOnTouchListener(new
RepeatListener(weightSeekbar, 50, 1));

--- The real code:

        private class RepeatListener implements OnTouchListener, Runnable {

                private SeekBar seekBar;
                private int period;
                private int incDec;
                private int downDuration;

                public RepeatListener(SeekBar seekBar, int period, int incDec) {
                        this.seekBar = seekBar;
                        this.period = period;
                        this.incDec = incDec;
                }

                public boolean onTouch(View view, MotionEvent motionEvent) {
                        int action = motionEvent.getAction();
                        if (action == MotionEvent.ACTION_DOWN) {
                                Log.i("Repeat", "ACTION_DOWN");
                                downDuration = 0;
                                seekBar.incrementProgressBy(incDec);
                                handler.removeCallbacks(this);
                                handler.postDelayed(this, 200);
                        }
                        else if (action == MotionEvent.ACTION_UP) {
                                Log.i("Repeat", "ACTION_UP");
                                handler.removeCallbacks(this);
                        }
                        return false;
                }

                public void run() {
                        Log.i("Repeat", "run");
                        downDuration += period;
                        int increment = incDec;

                        // Increase increment size the longer the button's held 
down
                        int halfSeconds = downDuration / 500;
                        int multiplier = halfSeconds + 1;

                        // Keep multiplier small relative to SeekBar range
                        int range = seekBar.getMax();
                        if (multiplier > range / 20) multiplier = range / 20;

                        increment *= multiplier;

                        seekBar.incrementProgressBy(increment);
                        handler.postDelayed(this, period);
                }
        }

--- That's it

You can play with the time delays in various ways, of course.

On Oct 5, 3:42 pm, DanH <[email protected]> wrote:
> I tried it using Messages rather than a separate thread, and it seems
> to work.  Essentially the same code, only the "run" routine in the
> Runnable becomes a handleMessage routine in a subclass of Handler, and
> you substitute the "send" methods in place of the "post" ones.  Seems
> wiser to me, since it keeps all the GUI actions in the main thread.
>
> On Oct 5, 1:56 pm, DanH <[email protected]> wrote:
>
> > One problem we're seeing with the scheme is that the count that's
> > being incremented "jumps" on press and release for some reason.  [Late
> > breaking news:  That was a finger malfunction.]
>
> > Also, I wonder if the separate thread is necessary -- couldn't one
> > just send a Message to one's own thread with sendMessageAtTime or
> > sendMessageDelayed?
>
> > On Oct 5, 1:01 pm, Kumar Bibek <[email protected]> wrote:
>
> > > This is ok I guess. You can optimize it or may be change a few things
> > > according to your needs.
>
> > > -Kumar Bibekhttp://techdroid.kbeanie.com
>
> > > On Tue, Oct 5, 2010 at 11:28 PM, DanH <[email protected]> wrote:
> > > > How does one create an auto-repeat Button on Android.  I found this:
>
> > > >http://groups.google.com/group/android-developers/browse_thread/threa...
>
> > > > But it seems kind of heavyweight and clumsy.  Is there a better way?
> > > > (One would think that this would be a built-in capability like it is
> > > > on most other UIs.)
>
> > > > --
> > > > You received this message because you are subscribed to the Google
> > > > Groups "Android Developers" group.
> > > > To post to this group, send email to [email protected]
> > > > To unsubscribe from this group, send email to
> > > > [email protected]<android-developers%[email protected]>
> > > > For more options, visit this group at
> > > >http://groups.google.com/group/android-developers?hl=en
>
> > > --
> > > Kumar Bibek
> > > Mobile  : +91-9663304186
> > > Mail : [email protected]

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to