I have an android application that displays, by default, a series of ImageButtons using xml. The user changes the image based on their input. I am trying to display the changed ImageButtons the next time the user loads the app.
Example:
ImageButton starts as Android.png (loaded from default xml page)
User enters text
ImageButton is changed to Correct.png
The next time that the app loads I want Correct.png to display instead of Android.png. Is there a way to iterate through the ImageButtons before the app starts (the buttons are NOT created programatically) to set the source value for each one before the application loads?
You can use findViewById(R.id.button_1) for each button and set it as needed. You'll then need to store which values it should have so that you can load these values and set the ImageButton source for each one. You can store it with either SharedPreferences or a SQLite database, depending on how much you have to change.
So, for example:
ImageButton b = (ImageButton)findViewById(R.id.button_1);
b.setImageDrawable(drawable);
You can load a drawable up for the checkbox so that you do it only once, which is more efficient, or you could setImageResource to R.drawable.correct, which would be a bit slower since I assume that the checkbox would probably be set for multiple images.
A better way to perform this by using SharedPreferences.
SharedPreferences sharedPref = getSharedPreferences("userpref", Activity.MODE_PRIVATE);
String image = pref.getString("userchoice","no-image");
if(image.equeals("no-image")
{
imageView.setBackgroundResource(R.drawable.android);
}
else
{
//compare here your image
imageView.setBackgroundResource(R.drawable.Correct);
}
And whenever user selects any other image, just edit your preference file by using following code.
SharedPreferences.Editor editor = pref.edit();
editor.putString("userchoice","Correct.png");
Related
I am trying to retrieve a default value (which is the last value saved)
I am making some changes in the background color of the layout
so before the setContentView
I put this code in.
SharedPreferences preferences = getSharedPreferences("UID",MODE_PRIVATE);
setTheme(preferences.getInt("color",R.style.AppTheme));
But it won't work, the R.style.Apptheme's background color (which I set in the style.xml) works but I have changed the bg's color with this code
SharedPreferences preferences = getSharedPreferences("UID",MODE_PRIVATE);
preferences.edit().putInt("color",R.style.red_theme).commit();
finish();
startActivity(getIntent());
what is the proper way of retrieving the last saved style?
I am trying to develop an app that requires certain values to be set by the users at the app's first startup only, because i don't wanna bother them frequently inputting the values everytime they launch the app. My app has a single activity main and uses certain values that are inputted by the users at first startup. How can I make this possible.
Please explain me elaborately . :-)
You should use SharedPreferences to keep a track of the first use.
In the onCreate Method of your Activity (Startup activity), you could do something like this,
SharedPreferences userPrefs = getSharedPreferences("UserPrefs", 0);
Boolean firstUse = userPrefs.getBoolean("firstUse", true);
if(firstUse){
//this implies it is the first use of the app
//also once you are done implementing the logic for first use you need to put firstUse as true
SharedPreferences.Editor editor = userPrefs.edit();
editor.putBoolean("firstUse", false);
editor.commit();
}
else{
//take the user directly inside the app
}
Also, if you plan to save user information in the first use, look at different ways of storing data here.
show the alert initially and after getting the input values keep it in preference and next time check whether the required values existing or not. If it is already there avoid popup
For getting more information about shared preference check this link http://www.vogella.com/tutorials/AndroidFileBasedPersistence/article.html
preferences_statusFirst.getString("boot", "");
if (status.length() <= 0)
{
showDialog(DIALOG_birth);
editor_boot.putString("boot", "1");
editor_boot.commit();
}
else
{
}
}
My app has this appearance
It seems to be a TableLayout with several TableRows. In my activity, each TableRow has 3 views: ImageView, TextView and a Button.
The user is the one who sets the content of the ImageView and the TestView by entering the text he wants and pressing a button.
I store this data in stringArray variables and works fine if the phone is not restarted or the app is not closed (forceClose)
If one of these two situations happen, i lose all my data.
I've been trying to store my StringArrays by SharedPreferences but I don't know when i should load the preferences, whether it's in OnCreate() or OnResume() or OnStart methods().
Another question is how to define the arrays. I use this:
String[] titulo = new String[500];
I don't know if this string is created each time i start the activity. Because what i want is to load the previous String (from SharedPreferences) and add some more entries not to create new ones every time the phone is rebooted, for example.
Do you think i need a SQL database or it's OK with this StringArrays.
Thank you.
You should probably switch to using a database, seeing as you have an array of size 500, which could possibly increase in the future.
However, if you want to continue using SharedPreferences, you should write the data in onPause() and onStop() methods, and use an if else statement to see if your data is null before running an operation on it. If the data is null, the read it from the SharedPreferences before continuing.
I’m a new developer, so my question maybe too much basic.
I look for example of defining preferences of sound. User can choose what kind of sound will start an application, for example. There can be such a RingtonPreference widjet, so user can choose a sound.
As I know, preferences support the primitive types: Boolean, string, float, long and integer. What way is the best to design preferences: store in entryValues the names if sounds (strings), the address of files from Resourse class (integer), or other way.
Please provide a short example of code.
Thanks in advance!
First of all thank you for the quick and detailed answer!
I want to arrange list of sounds: there must be one “None”, list of sounds that contains folder “raw”, option to add a new sound from different locations and two buttons: “set” and “cancel”. When user touches one item from the list – sound starts to play.
There is a little problem with standard widget that provide android library. “ListPreference” isn’t appropriate because on touch on one of the items – item is chosen and list closes, “there are not buttons set and cancel”.
“RingtonPreference” isn’t appropriate as well – I didn’t succeed to add something to list.
How is possible to build a custom preference layout and that is options that were chosen will be saved as well as on standard widgets. Please provide a short code example. Thanks in advance!
I think the best way to store the Resource are by integers. or you could do names.
I think integer is more reliable.
So to use SharedPreference with this you will need to get access to the apps SharedPreference
public class PreferencesDemo extends Activity {
SharedPreferences app_preferences;
private int resourceNumber;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Get the app's shared preferences
SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
resourceNumber = app_preferences.getInt("resourceNumber", 0);
if(resourceNumber == 0){
//This means the user hasnt selected a song and you must act accordingly. Or put a resource number where the 0 is do set it to a default song
}
You will probably want to create a method to put the songs in the SharedPreference such as;
private void createSongResouces(){
SharedPreferences.Editor editor = app_preferences.edit();
//Here you can put as many songs as you want just make sure you call editor.commit(); as i do.
editor.putInt("resourceNumber", resourceNumber);
editor.commit(); // Very important
}
OK, so I'm playing around with an android app.
The 90% use case is that users want to go straight to the primary list screen to find what they're looking for. That's what I want as my default screen.
The first time a user loads the app however, some configuration is required before their list screen is of any value to them.
So my question, is how I can go about displaying the configuration activity the first time the app is opened up, and then the list screen for future openings.
I also want to put a demo button on the configuration screen, so I suppose more than just detecting that it's the first time, I specifically want to detect whether the user has performed certain configurations within the first screen.
After the first time the user has loaded the app you could store the details of whether user has performed the configurations in SharedPreferences.
protected void storeSharedPrefs(String value) {
/*
* Storing in Shared Preferences
*/
editor.putString("first", value);
editor.commit(); //Commiting changes
}
Check each on time application is loaded, whether its the first time and configuration details has been entered correctly by checking SharedPreferences
private boolean first_time_check() {
/*
* Checking Shared Preferences if the user had pressed
* the remember me button last time he logged in
* */
String first = uPreferences.getString("first", null);
if((first == null)){
return false;
}
else
return true;
}
i like dweebsonduty's method. a similar way to do this is to save their configuration information in files on the SD card. your app could start out by checking for those files and loading the information. if the load is successful, it moves on to the next activity, if not it starts the appropriate activity to prompt the user for input.
I have done this same thing, but instead of swiching activities i just switch views until i have all the info i need, and then move on.
Many applications actually store the current version in SharedPreferences, and check against it for if an update has been installed. Its a clever way of achieving a "what's changed" popup, or making sure that some settings get set (I would be wary of just having a boolean flag because if you ever want to add an additional setting, you will need a second flag and it gets messy after the third, etc.).
String VersionValue = "v.1.0";
final String PREFS_NAME = "MyPrefsFile";
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
if (settings.getBoolean(VersionValue, true)) {
//the app is being launched for first time, do something
NewDialogFragment newFragment = new NewDialogFragment();
newFragment.show(getFragmentManager(), "New");
// record the fact that the app has been started at least once
settings.edit().putBoolean(VersionValue, false).commit();
}
You could do it this way and still get the same result I tried it its a small workaround if u do not fully understand how to check if the app is updated. Instead with this code you can just simply change the String VersoinValue to your current app version and android will think the app is a new first time app and will only display the code u wrote once until you change VersionValue on your next update. (:
How will you be storing the configuration?
If it is in SQLlite you could just create a table called firstuse and put a field in there called hasbeenused and make it null. Then when the app is used you can put a 1 in there. Then you can read it each time your app loads and if that field = 1 then go to your next activity.