1

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?

  • In the click listener, save the date it was clicked in SharedPreferences, then compare the current date to the value in shared preferences in onResume() to enable or disable the button. – Simon Aug 06 '13 at 15:48

3 Answers3

1

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.

Then 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.

if (now - storedTime > DateUtils. DAY_IN_MILLIS) {
    b.setEnabled(true);
}
jimmithy
  • 6,360
  • 2
  • 31
  • 29
1

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.

To use the SharedPreferences with Dates, it it easy enough to use a formatted string (with day-precision) of a java.util.Date object.

For example, to persist a java.util.Date class to SharedPreferences as a formatted string:

//pre-condition: variable "context" is already defined as the Context object in this scope
String dateString = DateFormat.format("MM/dd/yyyy", new Date((new Date()).getTime())).toString();
SharedPreferences sp = context.getSharedPreferences("<your-app-id>", Context.MODE_PRIVATE);
Editor editor = sp.edit();
editor.putString("<your-datetime-label>", dateString);
editor.commit();

To retrieve the dateTime again from SharedPreferences, you could try:

//pre-condition: variable "context" is already defined as the Context object in this scope
SharedPreferences sp = context.getSharedPreferences("<your-app-id>", Context.MODE_PRIVATE);
String savedDateTime = sp.getString("<your-datetime-label>", "");
if ("".equals(savedDateTime)) {
    //no previous datetime was saved (allow button click)
    //(don't forget to persist datestring when button is clicked)
} else {
    String dateStringNow = DateFormat.format("MM/dd/yyyy", new Date((new Date()).getTime())).toString();
    //compare savedDateTime with today's datetime (dateStringNow), and act accordingly
    if(savedDateTime.equals(dateStringNow){
        //same date; disable button
    } else {
        //different date; allow button click
    }
}

This makes it rather simple to persist dates and check them again. You could also store the System's timeInMillis, and use a long value in shared preferences instead of a string representation of the date.

Community
  • 1
  • 1
MiStr
  • 1,193
  • 10
  • 17
0

When the button is pressed for the first time, you need to persist the dateTime somewhere. When pressed again, you compared the stored dateTime with the actual one.

The way to persist that data is up to you.

Matt B-L
  • 253
  • 1
  • 9