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:
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?
– viana
Hey, buddy, I rephrased the question... I hope you’re feeling better
– Tony Álaffe
I don’t understand, what answers?
– Tony Álaffe
Ah ta! Inside "As a user" has some interaction with the user or is it simply a "layout"?
– viana
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...
– Tony Álaffe
Are there many canvases or few? You can do it in several ways.
– viana
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...
– Tony Álaffe
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...
– Tony Álaffe
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.
– viana
Great, clicking the button and already adding is enough... would be added as soon as it entered the layout of how to use, right?
– Tony Álaffe
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.
– viana
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...
– Tony Álaffe
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
– viana