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();
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 am developing a simple apps that allow users to post articles on a website.
It's just a simple form with inputs for title et text and a send button.
To do this, I used a FragmentActivity with a Fragment called ArticleFragment inside.
I've read everywhere that the best way to save user's inputs was to override onPause() in the fragment, and to restore them in the onCreateView().
#Override
public void onPause() {
Log.i(TAG, "Pausing fragment");
Log.i(TAG, "Saving edits");
// Sauvegarde des saisies
EditText titreEdit = (EditText) getActivity().findViewById(R.id.titre_saisie);
EditText texteEdit = (EditText) getActivity().findViewById(R.id.texte_saisie);
String titre = titreEdit.getText().toString();
String texte = texteEdit.getText().toString();
SharedPreferences settings = getActivity().getPreferences(0);
Editor editor = settings.edit();
editor.putString("titre", titre);
editor.putString("texte", texte);
editor.commit();
Toast toast = Toast.makeText(getActivity(), R.string.article_enregistre, Toast.LENGTH_SHORT);
toast.show();
super.onPause();
}
As you can see, each time the activity is paused, a Toast is shown to inform the user that inputs have been saved.
That's what I wanted. But whenever the user changes the orientation of the screen, the popup is shown again (because Activity is paused, then destroyed) and this may annoye the user.
I tried to add android:configChanges="orientation" in the Manifest, but I think this is not the best way to deal with this issue.
I also tried to make a solution with onSaveInstance() but I do not understand how it works : sometimes onSavedInstance is null, sometimes not.
With all this, the main question is what is the best way to save user inputs, with orientation changes and back button, and where to restore what was saved.
Thank you!
Check if input values are empty in onBackPressed() and onClick of whatever button that takes the user to Home and display appropriate Toast message
Add android:configChanges="orientation"to manifest file.
Override onConfigurationChanged. Before you call super.onConfigurationChanged save the data.
If you have different layout files for portrait and landscape you have to remove all the views and setcontentview and initialize them(just like what you do on create ) after all these you can restore the data
I don't quite follow your logic. Lets see ... You have two edittext fields and save button, when user changes screen orientation on destroy you can save input in shared preferences or in Bundle and when onCreate() starts you can check if values are not empty and then show toast that there is a draft. If user hits back button you can also save input and display toast, if he/she hits send you just send input, right ?
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.
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;
}