0

Is there a way for a button to be executed in seconds without touching it. The following code execute the button in one second but after i touch the button. My question is can this be achieved without touching the button?

View.OnClickListener mButtonStartListener = new View.OnClickListener() {
    public void onClick(View v) {
        try {
            mHandler.removeCallbacks(hMyTimeTask);
            //        Parameters
            //        r  The Runnable that will be executed.
            //        delayMillis  The delay (in milliseconds) until the Runnable will be executed.
            mHandler.postDelayed(hMyTimeTask, 1000); // delay 1 second
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
};


private Runnable hMyTimeTask = new Runnable() {
    public void run() {
        nCounter++;
        hTextView.setText("Hallo from thread counter: " + nCounter);
    }
};
/**
 *
 */
View.OnClickListener mButtonStopListener = new View.OnClickListener() {
    public void onClick(View v) {
        mHandler.removeCallbacks(hMyTimeTask);

    }
};
Meli
  • 382
  • 3
  • 6
  • 19

2 Answers2

0

Try .onLoadCompleteListener instead of .onClickListener

Elvopresla
  • 147
  • 2
  • 14
0

You may simulate click event by following code

View.performClick();

originally posted at https://stackoverflow.com/a/4877526/5329101.

Community
  • 1
  • 1