1

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.

Dude
  • 173
  • 2
  • 4
  • 14

2 Answers2

6

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.

PKlumpp
  • 4,913
  • 8
  • 36
  • 64
2

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.

Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49
  • got the idea but where to set the initial date in my preference – Dude Jun 30 '14 at 11:42
  • I made a little workaround. Put some boolean to check if this is the first time the app is used. For the first time, there is no value saved inside sharedPrefs, so You will get the default what is true (see boolean firstTimeUsed). If it is true, set the button directly visible. On the button click, set the value always to false, so You never get the value true after the first time the button is clicked (expect the user deleted the app or the sharedPreferences if he has root) – Opiatefuchs Jun 30 '14 at 12:03
  • Why not just store `currentTimeMillis()` and check the difference for greater than `86400000`, if it is.. enable the button and reset. Initially when the activity is first launched give a default value of 0 as in `getLong(, 0)` – hypd09 Jun 30 '14 at 12:06
  • the reason is, because if I only use currentTimeInMillis, the buttonClick is time dependent if I check greater than 86400000. For example, the user click at 12:13 pm, the button will just clickable after 12:13 pm the next day. I don´t think that is the goal – Opiatefuchs Jun 30 '14 at 12:15