I have an app with 2 activities, the preference and the main activity, I need the preference screen to show first time the app is run so the user can do some configuration. I have checked through the answers on this topic and they don't seem very clear but I gather it has to do with checking that there are sharedpreference file is empty.
Can someone please give me a code to sort this out and on which activity would I put the code?
Also I am still in the developing stage so I already have my preferences setup how do I undo this?
Thanks in Advance
1) When your main activity starts check a boolean preference with the default set to false. If it is false, launch your preference activity, if it is true then you know you have saved it to be true!
SharedPreferences prefs = getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
boolean haveWeShownPreferences = prefs.getBoolean("HaveShownPrefs", false);
if (!haveWeShownPreferences) {
// launch the preferences activity
} else {
// we have already shown the preferences activity before
}
2) In your preferences activity save the same boolean preference with a value of true in onCreate
SharedPreferences prefs = getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
SharedPreferences.Editor ed = prefs.edit();
ed.putBoolean("HaveShownPrefs", true);
ed.commit();
I assume that you're running an emulator, when you start the emulator you have the choice to "wipe saved data" when you start it so it will be like you started it as if you just started the application. Alternatively, you can go into settings -> Applications -> You app -> Wipe data.
In regards to your coding solution, I don't have anything handy at the moment, but what you should do is start your main activity, run a procedure/function to check if the sharedpreference file is empty, and if it is start the preference activity, otherwise run your main activity. Alternatively, instead of checking for the file to be empty, you could see if a value that you are looking for user input (for example UserID) is null or not. If that value isn't null that means that the application can continue.
I've sorted this out with this bit of code in my main activity
if (prefs.getString("edittextpref", null) == null)
{
startActivity(new Intent(this, Preferences.class));
return;
}
}
It just checks if one of your values is empty but you need to put this at the bottom of onCreate or else when you go back to the main page it will be blank.
I am doing something like this. And its works for me.
String path = "//data//data//"+this.getPackageName()+"//shared_prefs//feedbackpref.xml";
boolean exists = (new File(path)).exists();
if (exists) {
introWindowNavigate=false;
}
Related
How do i update a Prefence UI instance that i created in a Setting Activity from another activity (Main Activity)?
I tried using these lines in Main Activity to update the Preference within Settings Activity, but I get ClassCastException.
Preference IsFeature =(Preference)((PreferenceActivity)context).findPreference((getString(R.string.key_enable_feature)));
IsFeature.setEnabled(True);
Just wondering whether theres another way to do this?
Any help, feedback or answers would be great!
You can try this:
In xml of settings get the "key" attribute from element you want to change (in bottom example it's "example_switch"). Than put this code in onClick method of button or wherever else you want to. This below takes the preference of switch in general settings and sets its value to false.
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("example_switch", false); // "example_switch" - "key" attribute of your element | false - value
editor.commit();
i an developping an app for android and i want to "disable" or "hide" some activitys after my start.
i tried it with the shared preferences... but it somehow didnt work...
// First Start
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
if(mPrefs.getBoolean("firstLaunch", true)) {
mPrefs.edit().putBoolean("firstLaunch", false);
}
in my head my idea would look like that:
on first start:
enter the name (activity 1)
enter two friends (activity 2)
menu (activity 3)
when its not the first start is should start like that
menu
i hope you can help me
It works with SharedPreferences! But adding an entry is only half the way. You will also have to check this entry at the start of the application. Then it will work.
And you have to call mPrefs.commit() after adding the entry...
You're missing a commit() call in your preferences Editor.
This:
mPrefs.edit().putBoolean("firstLaunch", false);
should look more like this:
mPrefs.edit().putBoolean("firstLaunch", false);
mPrefs.commit();
try this to load a function or activity just one time
public void onResume()
{
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
if(mPrefs.getBoolean("firstLaunch", true))
//here you add whatever you want to do one time
mPrefs.edit().putBoolean("firstrunas12", false).commit();
}
I have this settings section where I allow users to change the languages displayed within the app. When the user chooses a different language, the activity is reloaded so that the change of language can be applied. But the problem is, when the user clicks back right after changing the language, the language shown in the background activity is still the same.
So my question is, what should I do to apply the change of language when I get back to some activity on the background? I suppose I should do something to detect the change in onResume method, but I'm not sure what it is. If you have any suggestions, please let me know.
Thank you.
After several attempts, I have found the solution to my problem. On my onCreate method, I get the SharedPreferences that contains the value of current language, and get the current language:
SharedPrefrences languagepref = getSharedPreferences("language",MODE_PRIVATE);
String language = languagepref.getString("languageToLoad", Locale.getDefault().getDisplayLanguage());
Then, in my onResume method, I assign the value of the above mentioned variable language to a local variable, and update the value of language. Then I compare these two variables - if they are different, I will destroy the current activity and start another:
#Override
public void onResume(){
super.onResume();
String oldLanguage = language;
language = languagepref.getString("languageToLoad", Locale.getDefault().getDisplayLanguage());
if (!oldLanguage.equals(language)){
finish();
startActivity(getIntent());
}
}
And voila, that did the trick!
I would suggest using SharedPreferences. You can store a lang key with the associated value in there and update it when necessary. In your onResume() methods you can get the lang value and then populate views according the value stored.
SharedPreferences sharedPreferences;
sharedPreferences = this.getSharedPreferences("MyActivity", Activity.MODE_PRIVATE);
String lang = sharedPreferences.getString("lang", "en-GB");
SharedPreferences.Editor editor;
editor = sharedPreferences.edit();
editor.putString("lang", "en-US").commit();
That's the basics you need to get going.
Have you tried restarting the Activity after the change is done ?
You can Simply use
finish();
startActivity(getIntent());
to refresh an activity whenever it detects a preference change.
The built in back pressed function does not refresh the code,so do this after u you change the language.
#Override
public void onBackPressed()
{
//new MainActivity();
Intent intent=new Intent(this,MainActivity.class);
startActivity(intent);
}
and in Main Activity class do this
public MainActivity() {
//1- This code is responsible for updating the change of all Strings from a language to another
//it will be called every time this activity is instantiated (da,since it is a constructor) , or when this activity gets
// called by an intent.
//2- Every String inside this Activity(ever fragment inside it ) will also be under the effect of change of language
LocalUtils.updateConfig(this);
}
Please review the following answer and add this to it. To get a full answer written by Roberto.B and LarsH:
Changing Locale within the app itself
I would like to create a popup window which will appear the first time the user opens the application and ask the user to select the setting within a spinner (example something close like the picture below)
(source: mikesandroidworkshop.com)
Also, I would like it to popup automatically rather then having the need to press on a button. Is it possible to do so?
Please help. thanks a lot. =)
That is called a Dialog. See this page for more info http://developer.android.com/guide/topics/ui/dialogs.html.
To create the one like you showed, look under the Custom Dialog section. Basically create the layout you want to see inside of the dialog in an XML file and use setContentView as you would with an activity.
If you want it to pop up when an activity starts just put the code in the onStart method in your activity.
Just call it onCreate, for example. And use shared preferences to check for first time launch.
private void showSettingsPopUpOnFirstTimeLaunch(){
SharedPreferences settings = this.getSharedPreferences("default", 0);
boolean firstStart = settings.getBoolean("firstStart", true);
if(firstStart){
showPopUp(); //
}
}
And when closing popup just change flag in SharedPreferences (you would probably will want to make popup not-cancelable).
SharedPreferences settings = this.getSharedPreferences("default",
0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("firstStart", false);
editor.commit();
I'm trying to get and set a listPreference value from different activities and it's not working.
When I read and write it from my main activity, it only keeps whatever I write, so I'm assuming that I'm not targeting the listPreference correctly when I'm out of the activity because it's working inside my preference activity no problem.
I've seen some references on the developer website to CharSequence with getValue and getEntryValues but I haven't had luck getting them to work correctly either.
Here is my code for clicking a button and setting the listpreference value then it launches an intent to switch activities:
Main Activity, attempting to set the value of the listpreference to the first index value;
SharedPreferences settings = getSharedPreferences("PreferenceXML",
MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString("ListPreferenceInXML", "1");
editor.commit();
String levelCheck = settings.getString("ListPreferenceInXML","1");
In my next activity I call read the value on launch to see which listPreference is active and it is always the number I write from the mains activity listed above. The problem is when I goto the actual Preference activity and it doesn't match or update when I change it on the ListPreference and launch the same activity from there (it still reads the value I set from the Main activity button)
code as follows for activity trying to read ListPreference:
SharedPreferences settings = getSharedPreferences("PreferenceXML",
MODE_PRIVATE);
Toast.makeText(this, settings.getString("ListPreferenceInXML","1"), 1000).show();
So I finally figured it out, the problem was with the way I was calling the preferences. Instead of calling the preferences like this, in both cases;
SharedPreferences settings = getSharedPreferences("PreferenceXML",
MODE_PRIVATE);
Call them like this:
SharedPreferences settings =
PreferenceManager.getDefaultSharedPreferences(getBaseContext());
I'm not sure if there is a step missing out of the first way of calling the preferences but this 2nd way worked like a champ.