7

I have Button and an TextView. Initially on click of button i an incrementing the count value and display in the TextView. The code i have used to do so is.

buttonDayPlus.setOnClickListener(new OnClickListener() 
{               
     @Override
     public void onClick(View v) 
     {
     count = count + 1;
         textView.setText(Integer.toString(count));
     }
}

But now what i want is if the user pressed the Button, then i want to increment the value one after another and display the increment value in the textView and as soon as user move his hand up...the final increment value is shown in textView. So if anyone knows help me to solve this out

Rahul
  • 1,667
  • 6
  • 21
  • 38
  • 1
    possible duplicate of http://stackoverflow.com/questions/3553163/android-long-touch-event – bhups Aug 22 '12 at 10:34

5 Answers5

8

You will need to use a Handler object because you have to implement a separate thread for incrementing/decrementing

public class MainActivity extends Activity {

    private boolean autoIncrement = false;
    private boolean autoDecrement = false;
    private final long REPEAT_DELAY = 50;
    private Handler repeatUpdateHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.input);

        class RepetitiveUpdater implements Runnable {

            @Override
            public void run() {
                if (autoIncrement) {
                    increment();
                    repeatUpdateHandler.postDelayed(new RepetitiveUpdater(), REPEAT_DELAY);
                } else if (autoDecrement) {
                    decrement();
                    repeatUpdateHandler.postDelayed(new RepetitiveUpdater(), REPEAT_DELAY);
                }
            }

        }

        up.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                increment();
            }
        });

        up.setOnLongClickListener(new OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                autoIncrement = true;
                repeatUpdateHandler.post(new RepetitiveUpdater());
                return false;
            }
        });

        up.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP && autoIncrement) {
                    autoIncrement = false;
                }
                return false;
            }
        });

    }

    public void increment() {
        if (i < 100) {
            i++;
            TextView no = (TextView) findViewById(R.id.no);
            no.setText(String.valueOf(i));
        }

    }

}

Do the same for decrement.

Courtesy: Github, author: Jeffrey F. Cole

Kirill Rakhman
  • 42,195
  • 18
  • 124
  • 148
Susheel
  • 784
  • 1
  • 12
  • 20
4

I just answered this question here https://stackoverflow.com/a/41466381/2991628

I created a CounterHandler class to do all the counting and event handling. This is how it works.

 new CounterHandler.Builder()
            .incrementalView(buttonPlus)
            .decrementalView(buttonMinus)
            .minRange(-50) // cant go any less than -50
            .maxRange(50) // cant go any further than 50
            .isCycle(true) // 49,50,-50,-49 and so on
            .counterDelay(200) // speed of counter
            .counterStep(2)  // steps e.g. 0,2,4,6...
            .listener(this) // to listen counter results and show them in app
            .build();

You can find this class from my gist https://gist.github.com/nomanr/d142f4ccaf55ceba22e7f7122b55b9b6

Community
  • 1
  • 1
Noman Rafique
  • 3,735
  • 26
  • 29
2

You should use OnTouchListener instead of OnClickListener.

However, your question is ambiguous. If you want to increment the value only once when the button is released you would do something like this:

button.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            count++;
        }
    }
};

If you want to increase count while the user is holding down the button you would do it like this:

button.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        count++;
        myTextView.setText("Click count: "+count);
    }
};

Where myTextView of course is the textview you want to show the clicks on.

Kirill Rakhman
  • 42,195
  • 18
  • 124
  • 148
Emir Kuljanin
  • 3,881
  • 1
  • 25
  • 30
  • But in my case i have to show the user the change in the count value in textview as long as user presses the button. I mean to say How would I have the count value increment/decrement and show in textview if the button is held? – Rahul Aug 22 '12 at 10:48
  • Why is your boolean not returning any value @EmirKuljanin? – enthusiasticgeek Apr 08 '19 at 23:53
0

Use custom touch listener like this,

public class CustomTouchListener implements View.OnTouchListener {     
    public boolean onTouch(View view, MotionEvent motionEvent) {
    switch(motionEvent.getAction()){            
            case MotionEvent.ACTION_DOWN:
           count = count + 1;
                break;          
            case MotionEvent.ACTION_CANCEL:             
            case MotionEvent.ACTION_UP:
          textView.setText(Integer.toString(count));
                break;
    } 
        return false;   
    } 
}

and set these listener to your button. May it helps you.

Shalini
  • 1,733
  • 4
  • 17
  • 31
0

As @shalini gave answer is right but there are little change as per your requirement is

public class CustomTouchListener implements View.OnTouchListener {     
    public boolean onTouch(View view, MotionEvent motionEvent) {
    switch(motionEvent.getAction()){            
            case MotionEvent.ACTION_DOWN:
               count = count + 1;
               textView.setText(Integer.toString(count));
            break;          

           case MotionEvent.ACTION_CANCEL:             

           case MotionEvent.ACTION_UP:

                break;
    } 
        return false;   
    } 
} 
Chirag Patel
  • 11,416
  • 3
  • 26
  • 38