public class MainActivity extends AppCompatActivity {
Button button;
// Initializing SharedPreferences
SharedPreferences sharedPreferences = getSharedPreferences("MySharedPref",MODE_PRIVATE);
SharedPreferences.Editor myEdit = sharedPreferences.edit();
static String IS_DEACTIVE_KEY = "isActive";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
// Getting the isDeActive boolean from the sharedpreferences
Boolean isButtonDeActive = sharedPreferences.getBoolean(IS_DEACTIVE_KEY, false);
// Checking if we already set button deActive, if so, make it deActive
if (isButtonDeActive){
button.setClickable(false);
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setClickable(false);
// Saving DeActive state true in Sharedpreference
myEdit.putBoolean(IS_DEACTIVE_KEY,true);
myEdit.commit();
}
});
}