'How to create a timer on Button and save in SharedPreferences(android)
I want to create a button that after clicking this button will be not clickable for 1 min, but after restarting a program, this button is clickable cuz i didn't understand - how to save this in sharedpreferences
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btn.setEnabled(false);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
btn.setEnabled(true);
}
},120000);
}
});
But how I need to save that in sharedpreferences?
Solution 1:[1]
If you want to save your state in preference then you can do it like below
public void write(String key, boolean value) {
SharedPreferences.Editor editor =
getSharedPreferences("Some Name",MODE_PRIVATE).edit();
editor.putBoolean(key,value);
editor.commit();
}
public Boolean read(String key, int defValue) {
return getSharedPreferences("Some Name",MODE_PRIVATE).getBoolean(key, defValue);
}
On click of the button you can write your value to shared preference and when you are rendering your button you can get the value from preference and enable and disable it based on the value and also save the value as false when your time is finished.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Swayangjit |
