1

I am creating an application, but I am not able to do the following: I have a button that increments a variable and a button that decrements and saves the value in the database through php and shows in a textview. What I am trying to do is that when the increment (or decrement) button receives multiple clicks, it will save only the last incremental value. Example:

    btn_UP.setOnClickListener (new View.OnClickListener () {
            @Override
            public void onClick (View v) {
                if (temperature <32)
                    temperature ++;
                    txtTemp.setText ("" + temperature);
                }
            }

        });

btn_DOWN.setOnClickListener (new View.OnClickListener () {
            @Override
            public void onClick (View v) {
                if (temperature> 17)
                    temperature--;
                    txtTemp.setText ("" + temperature);
                }
            }
        });

This way I'm doing it, it increases and decreases with each click made in the button, but when receiving multiple clicks, it also does the same, however I would like it to continue incrementing and only when the multicliques finishes that the value was displayed and saved in the database.

I tried to do the following and it did not work as expected:

link: Android Preventing Double Click On A Button

public class Activity ... {
 
    private long mLastClickTime = 0;

 
    protected void OnCreate ... {

    btn_tempUP.setOnClickListener (new View.OnClickListener () {
            @Override
            public void onClick (View v) {
                if (SystemClock.elapsedRealtime () - mLastClickTime <500) {
                    
        return;
                }
                mLastClickTime = SystemClock.elapsedRealtime ();

                if (temperature <32)
                    temperature ++;
                    txtTemp.setText ("" + temperature);
                }
            }

        });


    }

}

Can someone help me with how to do this? If I could be clear.

2 Answers2

1

I can offer a theoretical answer:

1) Have a separate class, preferably AsyncTask, or Service for uploading the temperature to PHP.

(You're going to need async, anyway, because you're doing networking).

2) Make this new task work like this:

START --> DELAY --> UPLOAD

3) When the user presses increment, or decrement, it cancels the current task and creates a new one... so it will never upload unless they stop pressing for [DELAY] amount of time.

You could even have a completion handler for the upload task that calls back and indicates that the upload has succeeded... for example, by setting the text bold... but not bold when they press +/-.

Nerdy Bunz
  • 3,581
  • 2
  • 28
  • 60
0

You can not know whenever user stop clicking, but you can know when he start clicking.So, I have an idea, not sure it is the best solution but hope that help. When user starts to click a button, wait at least 1s (maybe lower, larger as you want), if he's clicking multi time, we continue waiting after he stop clicking 1s, it's time to show the last value of temperature. See the code below, hope you got it.

private Handler handler = new Handler(Looper.getMainLooper());
    private int temperature = 0;
    private TextView textView;
    public Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (textView != null) {
                textView.setText(String.valueOf(temperature));
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.text);
        findViewById(R.id.btn_add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handler.removeCallbacksAndMessages(runnable);
                handler.postDelayed(runnable, 1000);
                if (temperature < 32) {
                    temperature++;
                }
            }
        });
        findViewById(R.id.btn_minus).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handler.removeCallbacksAndMessages(runnable);
                handler.postDelayed(runnable, 1000);
                if (temperature > 17) {
                    temperature--;
                }
            }
        });
    }

    @Override
    protected void onStop() {
        handler.removeCallbacksAndMessages(runnable);
        super.onStop();
    }
NamNH
  • 1,580
  • 12
  • 32
  • I tried to do it this way, but it did not work very well, because the user also needs to make several simultaneous clicks and send the signal only when they stop the clicks. This way you suggested I tried to do it, but it can only click on the button every second, and this gives an impression that the application is locking. In practice, it was not very good, thanks for the answer – Jhonathan Candeu May 05 '18 at 21:32
  • It's just locking the updated text, but you still count enough value as your clicked time. Temperature value is always the latest. – NamNH May 07 '18 at 02:35