The comment button that has already been pressed will not appear the next time the user opens the application.

I want the button with the comment already pressed to not appear the next time the user opens the application. I searched on google and realized that I should use the general settings, but I do not know how to use the general settings in the application.

I am wondering how to use sharedpreferences for a visibility button?

+4
source share
1 answer

Here's how you use shared privileges:

public class AppPrefrances { protected static AppPrefrances INSTANCE; private static SharedPreferences prefs; public static AppPrefrances getInstance(Context context) { if (INSTANCE == null) { INSTANCE = new AppPrefrances(); prefs = PreferenceManager.getDefaultSharedPreferences(context); } return INSTANCE; } public void setClicked(String c) { //click should be unique prefs.edit().putString("click", c).apply(); } public String getClicked() { // 0 is the default value return prefs.getString("click", "0"); } } 

Then from inside the action:

 Button comment = (Button) findViewById(R.id.button); if(AppPrefrances.getInstance(getApplicationContext()).getClicked().equals("1")) { comment.setVisibility(View.INVISIBLE); } comment.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AppPrefrances.getInstance(getApplicationContext()).setClicked("1"); } }); 

General privileges are erased if clear data is selected from the application information

+1
source

All Articles