Dude
Dude

Reputation: 173

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 know i can set it View.gone but i want it to be visible again next day.what should come in the on click of button to get one day time

here is on click of my button what should i do?

@Override
public void onClick(View v) {
    super.onClick(v);
    switch (v.getId()) {
    case R.id.creditClaimBT: {

        break;
    }
    }
}

help would be greatly appreciated.

Upvotes: 1

Views: 3905

Answers (2)

Opiatefuchs
Opiatefuchs

Reputation: 9870

I think the best way is with sharedPreferences like ZerO said. For example, every time the user opens the activity where the button is, check the sharedPrefs, where You ,for example, save the date. After button click, save the current date. And every time You open the activity and the saved date is equal to the current date, don´t allow the button click (You can do it with View.GONE or setEnabled(false)):

  private boolean firstTimeUsed = false;
  private String firstTimeUsedKey="FIRST_TIME";

  private String sharedPreferencesKey = "MY_PREF";
  private String buttonClickedKey = "BUTTON_CLICKED;
  private SharedPreferences mPrefs;
  private long savedDate=0;

initialize these to set global variable in onCreate():

  mPrefs = getSharedPreferences(sharedPreferencesKey, Context.MODE_PRIVATE);
  savedDate = mPrefs.getLong(buttonClickedKey,0);
  firstTimeUsed = mPrefs.getBoolean(firstTimeUsedKey,true);//default is true if no value is saved
  checkPrefs();

  mButton.setOnClickListener(new OnClickListener(){

     @Override
     public void OnClick(View v){

           //things You want to do, after that:

          saveClickedTime();
     }
  });

do Your method-stuff to check the time:

  private void checkPrefs(){


      if(firstTimeUsed==false){
      if(savedDate>0){

      //create two instances of Calendar and set minute,hour,second and millis
      //to 1, to be sure that the date differs only from day,month and year

      Calendar currentCal = Calendar.getInstance();
      currentCal.set(Calendar.MINUTE,1);
      currentCal.set(Calendar.HOUR,1);
      currentCal.set(Calendar.SECOND,1);
      currentCal.set(Calendar.MILLISECOND,1);

      Calendar savedCal = Calendar.getInstance();
      savedCal.setTimeInMillis(savedDate); //set the time in millis from saved in sharedPrefs
      savedCal.set(Calendar.MINUTE,1);
      savedCal.set(Calendar.HOUR,1);
      savedCal.set(Calendar.SECOND,1);
      savedCal.set(Calendar.MILLISECOND,1);

      if(currentCal.getTime().after(savedCal.getTime()){

        mButton.setVisibility(View.VISIBLE);
      }else if(currentCal.getTime().equals(savedCal.getTime()){

          mButton.setVisibility(View.GONE);
     }

   }
 }else{

     //just set the button visible if app is used the first time

      mButton.setVisibility(View.VISIBLE); 

  }

}

And everytime the button is available and get clicked, save the date back to sharedPrefs:

       private void saveClickedTime(){

       Editor mEditor = mPrefs.edit();
       Calendar cal = Calendar.getInstance(); 
       long millis = cal.getTimeInMillis();
       mEditor.putLong(buttonClickedKey,millis);
       mEditor.putBoolean(firstTimeUsedKey,false); //the button is clicked first time, so set the boolean to false.
       mEditor.commit();

       //hide the button after clicked
       mButton.setVisibility(View.GONE);

      }

This is just from scratch and not tested for now, but should give You an idea how You can do it.

Upvotes: 2

PKlumpp
PKlumpp

Reputation: 5233

When your button was clicked, write the date of the event into SharedPreferences. If somebody clicks the button, check if the current date is already saved in SharedPreferences. Done.

Upvotes: 6

Related Questions