0

I'm working on an application and I have to save changes made by the user ... the user going to click on an image, it's going to change the color. And I want to save that change.

I'm not sure how to do that actually. Here is where I want to make the change.

All I know is I need to use SharedPreferences .

private ImageView bookmark;
bookmark = (ImageView) findViewById(R.id.imageView_bookmark_readIt);

bookmark.setOnClickListener(new View.OnClickListener(){

     private boolean bookmarking = true;

     public void onClick(View v){

      if(bookmarking){
         bookmark.setImageResource(R.drawable.ic_bookmarked_blue);
         bookmarking=false;
      }
      else{
         bookmarking=true;
         bookmark.setImageResource(R.drawable.ic_bookmark);
         //Toast.makeText(getApplicationContext(), "Changed", Toast.LENGTH_LONG).show();
       }
});

So does anybody have an idea of how to save changes made to the above code?

Note : I'm not using database

Ahmad Dalao
  • 1,968
  • 1
  • 8
  • 14
  • 2
    look for any blog. – Nouman Ch Jan 17 '19 at 05:45
  • https://developer.android.com/training/data-storage/shared-preferences – Nouman Ch Jan 17 '19 at 05:46
  • 2
    Possible duplicate of [Android Shared preferences example](https://stackoverflow.com/questions/23024831/android-shared-preferences-example) – Rumit Patel Jan 17 '19 at 05:52
  • There are a lot of answers which are correct, but you can use a simple library that do most of the job for you and you do not need to implement these functions at all. Take a look [EasyPrefs](https://github.com/amin-amini/EasyPrefs) – Amin Jan 19 '19 at 23:50

4 Answers4

0
 /**
 * Get a shared preferences file named Const.SHARED_PREFERENCES_FILE_NAME, keys added to it must be unique
 *
 * @param ctx
 * @return the shared preferences
 */
public static SharedPreferences getSharedPreferences(Context ctx) {
    return ctx.getSharedPreferences(Const.SHARED_PREFERENCES_FILE_NAME, Context.MODE_PRIVATE);
}

public static void cacheBoolean(Context ctx, String k, Boolean v) {
    SharedPreferences prefs = getSharedPreferences(ctx);
    prefs.edit().putBoolean(k, v).apply();
}

public static Boolean getCachedBoolean(Context ctx, String k, Boolean defaultValue) {
    SharedPreferences prefs = getSharedPreferences(ctx);
    return prefs.getBoolean(k, defaultValue);
}

public static void cacheString(Context ctx, String k, String v) {
    SharedPreferences prefs = getSharedPreferences(ctx);
    prefs.edit().putString(k, v).apply();
}

public static String getCachedString(Context ctx, String k, String defaultValue) {
    SharedPreferences prefs = getSharedPreferences(ctx);
    return prefs.getString(k, defaultValue);
}

public static void cacheInt(Context ctx, String k, int v) {
    SharedPreferences prefs = getSharedPreferences(ctx);
    prefs.edit().putInt(k, v).apply();
}

public static int getCachedInt(Context ctx, String k, int defaultValue) {
    SharedPreferences prefs = getSharedPreferences(ctx);
    return prefs.getInt(k, defaultValue);
}

public static void clearCachedKey(Context context, String key) {
    getSharedPreferences(context).edit().remove(key).apply();
}
Ahmed Karam
  • 386
  • 1
  • 7
0

Using SharedPreferences is very easy. You need to define a key which you will use to retrieve the data later. You can store Strings, ints, floats, booleans... You need to provide the context.

Context context = getApplicationContext();

To write data, use this code.

SharedPreferences mPrefs = context.getSharedPreferences("YourApp", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("color", "blue");
editor.apply();

To retrieve data, use this

SharedPreferences mPrefs = context.getSharedPreferences("YourApp", Context.MODE_PRIVATE);
String color = mPrefs.getString("color", "defaultValue");

You can easily change the type from String to other types that might best suit your needs.

Hope it helps.

0

Hope its help you

 SharedPreferences sharedPrefs = getSharedPreferences("SharedPreferences_Name", Context.MODE_PRIVATE);
    private ImageView bookmark;
    bookmark = (ImageView) findViewById(R.id.imageView_bookmark_readIt);
    private boolean bookmarking = sharedPrefs.getBoolean("bookmarking",true);//To get value that saved in SharedPreferences 
    if(bookmarking){
        bookmark.setImageResource(R.drawable.ic_bookmarked_blue);
    }
    else{
        bookmark.setImageResource(R.drawable.ic_bookmark);
        //Toast.makeText(getApplicationContext(), "Changed", Toast.LENGTH_LONG).show();
    }
    bookmark.setOnClickListener(new View.OnClickListener(){

       // private boolean bookmarking = true;

        public void onClick(View v){

            if(bookmarking){
                bookmark.setImageResource(R.drawable.ic_bookmarked_blue);
                bookmarking=false;
                SharedPreferences.Editor editor = getSharedPreferences("SharedPreferences_Name", 0).edit();
                editor.putBoolean("bookmarking", bookmarking);
                editor.apply();
            }
            else{
                bookmarking=true;
                bookmark.setImageResource(R.drawable.ic_bookmark);
                //Toast.makeText(getApplicationContext(), "Changed", Toast.LENGTH_LONG).show();
                SharedPreferences.Editor editor = getSharedPreferences("SharedPreferences_Name", 0).edit();
                editor.putBoolean("bookmarking", bookmarking);
                editor.apply();
            }
        });
Praveen
  • 946
  • 6
  • 14
0

In shared preferences data is stored in the “key-value” format. As far as I understand, you need to save two fields and it will be something like this:

“booking: true”
“bookmarkImageResource: 15670341274”

Here is a convenient way to do it:

Step one – create a new class SharedPrefs:

public class SharedPrefs {

private static final String SHARED_PREFERENCES_NAME = "user_prefs";
private static final String BOOKING_INFO = "booking";
private static final String BOOKMARK_IMAGE_RESOURCE_INFO = "bookmarkImageResource";
private final SharedPreferences prefs;

public SharedPrefs(Context context) {
    prefs = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
}

public boolean saveBookingInfo(String bookingInfo, String bookmarkImageResource) {
    return prefs.edit()
            .putString(BOOKING_INFO, bookingInfo)
            .putString(BOOKMARK_IMAGE_RESOURCE_INFO, bookmarkImageResource)
            .commit();
}

public Pair<String, String> getBookingInfo() {
    return new Pair<String, String>(
            prefs.getString(BOOKING_INFO, ""),
            prefs.getString(BOOKMARK_IMAGE_RESOURCE_INFO, ""));
}

public void clearAll() {
    prefs.edit().clear().apply();
}

}

Step two - use your class wherever you need to save, get or clear data! In you case:

    SharedPrefs prefs = new SharedPrefs(this);  // or getActivity() instead of this if we are in a fragment      
    if(bookmarking){
        bookmark.setImageResource(R.drawable.ic_bookmarked_blue);
        bookmarking=false;
    }
    else{
        bookmarking=true;
        bookmark.setImageResource(R.drawable.ic_bookmark);
    }
    prefs.saveBookingInfo(String.valueOf(bookmarking), String.valueOf(bookmark));

Hope this will help you =)

Have a good day & happy coding!

Svetlana Rozhkova
  • 181
  • 1
  • 3
  • 10
  • I tried your code, it didn't show any error and its functioning as expected .. but when I leave the page and return the data is not saved :( , sorry for asking again but did I miss something? – Ahmad Dalao Jan 18 '19 at 00:13
  • do you call getBookingInfo() method to get your saved data? The example above shows how to save data to sharedPreferences. But you also have to get data from sharedPreferences when you need it – Svetlana Rozhkova Jan 18 '19 at 10:11