How to disable button after it is clicked once in a day and reenable the button on the next day?
NickName:Smithy Johnsony Ask DateTime:2013-08-06T23:46:56

How to disable button after it is clicked once in a day and reenable the button on the next day?

I already know how to disable the button by using this code:

b.setFocusable(false);
b.setEnable(false);

I want to disable the button once it is clicked for the day, then enable the button the next day in android. In other words, if the button is clicked once today, it will become disabled until tomorrow. Any ideas?

Copyright Notice:Content Author:「Smithy Johnsony」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/18084733/how-to-disable-button-after-it-is-clicked-once-in-a-day-and-reenable-the-button

Answers
MiStr 2013-08-06T16:13:05

Saving a timestamp in SharedPreferences should suffice. If you're worried about security, you could use a crypto library (see this SO link) and save the datetime in a file, but that is most likely overkill.\n\nTo use the SharedPreferences with Dates, it it easy enough to use a formatted string (with day-precision) of a java.util.Date object. \n\nFor example, to persist a java.util.Date class to SharedPreferences as a formatted string:\n\n//pre-condition: variable \"context\" is already defined as the Context object in this scope\nString dateString = DateFormat.format(\"MM/dd/yyyy\", new Date((new Date()).getTime())).toString();\nSharedPreferences sp = context.getSharedPreferences(\"<your-app-id>\", Context.MODE_PRIVATE);\nEditor editor = sp.edit();\neditor.putString(\"<your-datetime-label>\", dateString);\neditor.commit();\n\n\nTo retrieve the dateTime again from SharedPreferences, you could try:\n\n//pre-condition: variable \"context\" is already defined as the Context object in this scope\nSharedPreferences sp = context.getSharedPreferences(\"<your-app-id>\", Context.MODE_PRIVATE);\nString savedDateTime = sp.getString(\"<your-datetime-label>\", \"\");\nif (\"\".equals(savedDateTime)) {\n //no previous datetime was saved (allow button click)\n //(don't forget to persist datestring when button is clicked)\n} else {\n String dateStringNow = DateFormat.format(\"MM/dd/yyyy\", new Date((new Date()).getTime())).toString();\n //compare savedDateTime with today's datetime (dateStringNow), and act accordingly\n if(savedDateTime.equals(dateStringNow){\n //same date; disable button\n } else {\n //different date; allow button click\n }\n}\n\n\nThis makes it rather simple to persist dates and check them again.\nYou could also store the System's timeInMillis, and use a long value in shared preferences instead of a string representation of the date.",


jimmithy 2013-08-06T15:56:54

When the button is pressed, you can save the current time into SharedPreferences. One way of gathering the current time would be to use System.currentTimeMillis. \n\nThen during onResume of your activity or after an interval of a custom timer, you could get the stored time from the Shared preferences, subtract it from the current time and then see if that number if larger than a day.\n\nif (now - storedTime > DateUtils. DAY_IN_MILLIS) {\n b.setEnabled(true);\n}\n",


More about “How to disable button after it is clicked once in a day and reenable the button on the next day?” related questions

How to disable button after it is clicked once in a day and reenable the button on the next day?

I already know how to disable the button by using this code: b.setFocusable(false); b.setEnable(false); I want to disable the button once it is clicked for the day, then enable the button the nex...

Show Detail

How to disable button after it is clicked once in a day and reenable the button on 24 house later?

A.setEnable(false); I want to disable the button once it is clicked for the day, then enable the button the next day in android. In other words, if the button is clicked once today, it will become

Show Detail

How to disable a button, if it is clicked once in a day and enable on next day using JavaScript and AngularJS 1?

My button should be disabled once I clicked in a day (i.e) only once in a day a user enter the details using submit button. Next day automatically button should be enabled. Help me with JavaScript...

Show Detail

How to disable submit button after clicking one time in a day and re-enable this button on next day using php?

I want to disable submit button for a user after clicking one time in a day and re-enable this button on next day. &lt;input type="submit" class="myButton" id="option" value="submit"/&gt; &#x

Show Detail

How to disable the button after first click and i want the button click functionality after the day

I want to create a button which will disable after clicks and enable next day, that is button will enable for single click for single day. User will come to office and click button after clicking

Show Detail

How to disable button once clicked in iPhone programmatically

I have made a footer with 5 buttons, I want to disable the button once clicked till another button is clicked (means once another button is clicked then button should be re enable it). I am posting...

Show Detail

How to make a Button clickable only once in a day

i have a button through which a customer can claim his daily rewards. i want that button to be clicked only once a day. how could i do this? i have no idea how to achieve this functionality i kno...

Show Detail

How disable button until the next day after on click

I am completely lost, I have a website where the user can login, after login I would like to add a button which deactivates after a click and reactivates the next day (every midnight), I would like...

Show Detail

Flutter Button It should be pressed once in a day

Is there any where I can make a button to be pressed only once in a day. You cannot press that button again in that day. you need to wait till next day inorder to press it again

Show Detail

how to make countdown timer to disable the button for one day

I am developing a quiz app in it I retrieve one question per day date wise and the user has to answer only this retrieved question. I want to disable my button after user submitted his answer and it

Show Detail