D
D
Dmitry Bobreshov2018-05-13 19:33:49
Android
Dmitry Bobreshov, 2018-05-13 19:33:49

How to put a timer on a button in java?

There is a code that displays a message after pressing the button, after this pressing it is necessary to hide the button and in its place start a timer for 24 hours, after which the button appears again.
Can anyone provide some guidance on how to implement this? Google didn't help me!
It is not clear to me - work with this timer. How to put it and then return the button? How to hide the button to display a timer in its place and then hide the timer?
Now there is code that displays a message when clicked and then hides the button.

@Override
            public void onClick(View v) {
                String product = "";

                Cursor cursor = mDb.rawQuery("SELECT * FROM Guru ORDER BY RANDOM() LIMIT 1;", null);
                cursor.moveToFirst();
                while (!cursor.isAfterLast()) {
                    product = cursor.getString(1);
                    cursor.moveToNext();
                }
                cursor.close();

                textView.setText(product);
                v.setVisibility(View.GONE);


            }
        });

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2018-05-13
@is_there_something_wrong

in onClick you do
time = Date().getTime() + 24*60*60*1000
write time to sherepreferences
make the button invisible and the TextView visible and start sending messages about which below.
when creating (or in onStart), you read the data from sharepreferences or take time (whichever is more relevant) and start sending messages through the handler (handler.postDelay for example every 1000 milliseconds )
in each message handler, set the remaining time in the TextView
Date().getTime - time (translating everything nicely into hours and minutes)
If Date().getTime()> time , then you make the button visible, the TextView hidden. You stop sending messages.

E
Eugene, 2018-05-13
@klim76

make a sharedpreference into it, write boolean - the rendering flag, after pressing the flag, make it false and start the timer or task in the scheduler when it works - rewrite the sharedpreference to true.
well, in rendering, insert a check of this flag somewhere in onResme

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question