How to add an icon showing that that item has already been read, being saved the preferences using Sharedpreferences?

Asked

Viewed 126 times

0

This is a part of the app’s home menu:

Home Screen

The image below represents the initial screen of the application, which has the buttons "how to use" and "Introduction to dynamics", for the user to choose which task will do.

inserir a descrição da imagem aqui

Screen "How to use"

Clicking on the ImagemButton (above the text "How to Use") the user goes to a new Layout in which there is a button back to main screen so that you can move forward to the next steps of the tutorial, as below:

Representação da tela do "como usar"

Doubt

After the user has entered layout of the "how to use", have read and completed by clicking the button back to main screen wanted an icon to appear on ImagemButton to notify that he has already completed that task. So, when the user enters the application again, he can know which one he has already done and continue where he left off. As represented below:

tela como deveria ficar após a conclusão do "como usar"

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

  • Dude, you’re a little confused more I understand a little of what you want. In the media that goes answering the questions, the button changes right?

  • Hey, buddy, I rephrased the question... I hope you’re feeling better

  • I don’t understand, what answers?

  • Ah ta! Inside "As a user" has some interaction with the user or is it simply a "layout"?

  • It is simply a Layout with a button to return to the menu... The other tasks have a greater interaction than this "how to use", but I believe that knowing to make this simpler, that only has this button "Go to main screen", the others will be right too...

  • Are there many canvases or few? You can do it in several ways.

  • This how to use only has one... only one detail that can matter since you believe it has several ways to solve... I made the entire application with the same Activity, I open only different layouts...

  • If you are referring to user interaction due to the name Dynamics, it is the subject name dealt with in the application... it is an application to assist Physics teachers in High School...

  • Then you would have to put the logic in the first click of the button to enter its layout. Type, clicked and opened the layout, would already make as read. It would be the most viable way at the moment for you are using the same Activity. If it were something more rigorous, you could do type beyond the user click and enter the layout, could require you to scroll the app until the end etc.

  • Great, clicking the button and already adding is enough... would be added as soon as it entered the layout of how to use, right?

  • There are other ways to do it besides the answer I just published. If you’re totally clear of your doubt, do a brief research on data persistence, because as far as we can tell, that’s what you’re in need of. In case the data does not persist, every time you open your application, will stick to values standards of development.

  • Friend, thank you so much for helping me, but is that I am new in programming... I’m still trying to put what you proposed in the app... appeared some errors and I’m trying to see if it right, I will see some videos and read about data persistence and Sharedpreferences... As soon as it works out or if I don’t make it back... The first error that came up was "Boolean tela1..." maybe I didn’t set the button as correctly viewed...

  • Quiet! I’ve also been there in your place and asking for help from other people. In my spare time I’m always trying to help. Anything, go here! abs

Show 8 more comments

1 answer

1


As you mentioned, for relatively small collection of key values to save cases, use the Apis Sharedpreferences. An object SharedPreferences indicates a file that contains key value pairs and provides simple methods to read and write.

Each Sharedpreferences file is managed by the work structure and can be private or shared.

First can define a string static 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 add the image when the button is clicked and a preference is saved, this way:

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);
    }
}

To rescue the recorded value just you check this way, on the screen the image is on:

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 the end, you must put this method onOff(); on the button you wish.

Therefore, you will do that above check every time you enter your application. Remembering that it is a quick way of doing and can be optimized. This is just a basic example for you to have more sense.

Remarks:

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

2 - "confrimacao" is the name of the image file that represents the visualized notification.

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

4 - Perhaps the method onOff(); can be triggered on any button of my app due I use only one Activity throughout the app.

5 - There is a video on the page of the article "Far beyond Sqlite" (proposed below) that can help a lot.

There are other persistence techniques as an alternative, simple and agile, which allow the persistence of small amounts of data. They are:

PreferenceActivity;

Internal Storage;

Armazenamento em Cache;

External Storage.

See some articles:

  • This article that you proposed "Far beyond the Sqlite" looks like it would show me exactly what’s left for me to get, but it gets paid...

  • @Tonyálaffe true, I forgot that detail. Actually, the Sqlite is for very large amount of data, in your case would not need. This article shows other alternatives for small amounts of storage. But there are other good articles on the internet that you might be accessing. There are two reference gringos that I love to follow to learn new things, the Vogella and Ravi Tamada. If you want to learn, see their tutorials. I recommend!

  • Ack Lay, it worked! It was really good! In the article "Much Beyond the Sqlite" has a video that explains something very similar to what I wanted, then I learned to do the video and then I fit this suggestion and it turned out to work! Thank you very much Again, helped me d+!

  • The question is, should I edit your answers by increasing what’s missing to make it work or should I create an answer showing how it turned out? I do not understand very well how it works here... I lost 4 reputation points by which I put this question as from Android Studio (I still do not know why).

  • @Tonyálaffe is very complicated this issue of negative here in the OS. There are many people who do not know yet how to use this down button. Maybe you were negatived by not having a little research effort on your question. Usually there has to be code, etc. and more clarity, plus formatting. Read here how to ask a good question; and types of questions I should avoid asking.

  • @Tonyálaffe if my answer helped you and you have an add-on for it, I suggest making the edition of my answer incrementing what you have. Now if it’s totally different, you can add a new answer to your question. Feel free! As you do not have enough reputation, your edition will pass an Ouvidoria that will subsequently be approved if it is suitable. I also suggest marking the answer as valid if it is actually valid.

  • @Tonyálaffe I will edit your question a little bit. See.

  • @Tonyálaffe edited trying not to leave the goal, but it was costly. hehe

  • But it has really improved!!! Once I have enough reputation, I will just put the image of the before, so that I have the ante and the after...

  • I edited the answer, you can see how it turned out?

  • @Perfect Tonyálaffe. I approved it. Now you just need to mark the answer as correct, and so the question is finished. If you need me, give me a call here. Abs.

  • I’ve got enough reputation, I’ve edited it and I’ve marked it right. Abs... I know it’s hard, but if you need it, I’m around too.

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.