How do I add an icon showing that item has already been read, saving preferences using SharedPreferences? [closed]

0

This is a part of the application's home menu:

Home Screen

The image below represents the application's home screen, which has the "how to use" and "dynamic input" buttons, so the user can choose which task to do.

Howtousescreen

ByclickingonImagemButton(abovethetext"How to use") the user goes to a new Layout where he has a button to return to the main screen move on to the next steps of the tutorial, as below:

Representationofthe"how to use" screen

Doubt

Once the user has entered the layout of the "how to use", have read and completed clicking on the button back to the main screen wanted to see an icon on ImagemButton to notify that it has completed that task. So when the user logs into the application again, he will be able to know which ones he has already done and continue where he left off. As shown below:

screenasitshouldbeaftercompletionofthe"how to use"

What Android resources / methods should I use so that I can add an icon showing that that item has already been read?

    
asked by anonymous 20.01.2017 / 04:59

1 answer

1

As you mentioned, for relatively small collection of key-values to save, use the SharedPreferences . A SharedPreferences object indicates a file that contains key-value pairs and provides simple methods for reading and writing.

  

Each SharedPreferences file is managed by the framework   and can be private or shared.

You can first define a static string to give the name of your configuration and declare the image that will appear with the click of the button:

public static final String PREFS_NAME = "Preferences";
private ImageView img;

Then we create a method to be added to the image when the button is clicked and a preference is saved, like this:

private void onOff() {
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    settings.edit().putBoolean("online", true).apply();

    boolean online = settings.getBoolean("online", false);
    if (online) {
        img.setImageResource(R.drawable.confirmacao);
    } else {
        img.setImageResource(R.drawable.fundo);
    }
}

In order to redeem the recorded value, you can simply check the screen where the image is located:

img = (ImageView) findViewById(R.id.img);

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
Boolean online = settings.getBoolean("online", false);

if (online)
    img.setImageResource(R.drawable.confirmacao);
else
    img.setImageResource(R.drawable.fundo);

To end, you should put this onOff(); method on the button you want.

So, you'll do this checkup every time you log in to your app. Remember that it is a quick way to do and can be optimized. This is just a basic example so that you have more notion.

Comments:

1 - "img" is the id of the image that has no background defined in the layout and is where the confirmation image is inserted.

2 - "confrimation" is the name of the image file that represents the view notification.

3 - "background" is the name of a file of an image with nothing (transparent) saved in .png

4 - Perhaps the onOff(); method can be triggered on any button in my app because I use only one Activity in the whole app.

5 - There is a video on the article page "Beyond SQLite" (proposed below) that can help a lot.

There are other persistent, simple and agile alternative techniques that allow the persistence of small amounts of data. These are:

PreferenceActivity ;

Internal Storage ;

Armazenamento em Cache ;

External Storage .

See some articles:

20.01.2017 / 19:21