I am making an application.
I want the first time the user opens my application, display an initial screen for entering the Name, Email and Phone Number,
Then, store the input data in the memory of the mobile phone.
I want this screen appears only the first time the user opens the application, because in my application always the same Name, Email and Phone will be used.
How I can do it?
For only first time view either you can either use shared preference or database.
Once user enter the values store all the values in shared preference and set one flag true in shared preference, when again next time user open the application check the flag if it is true then move to next screen either open the screen for entering name, email and phone number
Setting value in shared preference
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("name", "test");
editor.putInt("id", 12);
editor.commit();
Retrieve from Shared Preference
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String name = prefs.getString("name", "No name");//"No name" is the default value.
int idName = prefs.getInt("idName", 0); //0 is the default value.
Related
I am using Android Studio and programming in Kotlin. I haven't been able to find a way to save the data entered by the user so it appears when they reopen the app. Thank you ahead of time!
You have to store your data in shared preference when any click event listen or on edittext textwatcher you can set your character in preference but best way is on any click event save data on preference and when activity open set data from preference on edittext.
Store it in Shared Preferences
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
use this save the value..
Editor editor = sharedpreferences.edit();
editor.putString("key_name", "value"); //here give name and value to save
editor.commit();
use this to get the value.
String edittextvalue = pref.getString("key_name", null); //put edittextvalue where ever you what to show the value.
use this step to remove or clear the value
editor.remove("name");
editor.commit();
This question already has answers here:
Shared preferences for creating one time activity
(14 answers)
Closed 4 years ago.
I read about SharedPreferences but did not understand where i need to put the saving data and where to put the get objects.
In my app i get the full name when i open it in the first time by dialog.
I need to save the full name for ever (until the user will delete the app or something).
Where and what should i write to save the data(in onDestroy)?
Like :
// Create object of SharedPreferences.
SharedPreferences sharedPref= getSharedPreferences("mypref", 0);
//now get Editor
SharedPreferences.Editor editor= sharedPref.edit();
//put your value
editor.putString("name", strName);
editor.commit();
SharedPreferences sharedPref= getSharedPreferences("mypref", 0);
String name = sharedPref.getString("name", "");
And where and what should i write to get the data(in onCreate)?
You don't have to to anything in onDestroy(). If your app gets uninstalled your data in shared preferences will be removed as well.
editor.putString("name", strName);
The first parameter is the key and the second one ist the value.
if you want to save the user's name, you pass the first parameter "name" and for the second parameter, the user's name.
when you want to read the user's name later you use
String name = sharedPref.getString("name", "");
Again, the first parameter is the key. You want to read the user's name so you use "name" and the second parameter is the default value, if there hasn't been saved a value, yet.
I'm doing a quiz where the user unlocked the levels, but when he closes the app all progress is lost, and he has to redo everything again. How can I fix this and make the user's progress automatically saved? Or with it by clicking a button, I do not know
You can save it in SharedPreferences and check the value of it every time application is opened.
To save value:
SharedPreferences sp = getSharedPreferences("YourFileName", Context.MODE_PRIVATE); //YourFileName= Any file name you give it
SharedPreferences.Editor editor = sp.edit();
editor.putString("levels", your_level_value); //levels=key at which data is stored, your_level_value=No. of levels completed
editor.apply();
To retrieve value:
SharedPreferences sp = getSharedPreferences("YourFileName", Context.MODE_PRIVATE); //YourFileName= Any file name you give it
String levelCompleted = sp.getString("levels", "0"); //levels=key at which no. of levels is saved.
my app contains a list of dates in which there is toggle button
1.>on check it sets alarm and on uncheck it deletes the alarm
these is working normally but when i close aplication then alarm will automaically gets deleted
so i want to save data in shared preferences when toggle button is clicked and delete particular data when btn is unchek from shared preferneces for this i am doing
SharedPreferences sharedPref = context.getSharedPreferences("com.example.dd.mydata", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("alarm"+cday+"_"+month,cday);
editor.commit();
where cday and month is alarm date and month
on recieve i have to fetch these data and match with date so that alarm can ring
so on recive i am doing this
SharedPreferences sharedPref = arg0.getSharedPreferences("com.example.dd.mydata", Context.MODE_PRIVATE);
String name = sharedPref.getString("alarm"+cday+"_"+cmonth, null);
in these way i am achiving the value of shared preferences in both class but it is working for single toggle button clicking more than 2 button **overrites the also tell me how can i check whether the current dates exists or not in shared prefernces
You can call SharedPreferences.contains(String key) method.
For example, contains("alarm"+cday+"_"+cmonth)
I have 3 webviews in my search engine app.When the user enters his query, by default he gets Google results, followed by yahoo and Ask buttons at the bottom, on clicking either of them, he gets results for that query from those sites. Now I want to give the user the privilege to change the default result site. I have created 3 radiobuttons. Upon confirmation, say he chooses Yahoo, how can i set it as Yahoo till the next time he changes it to some other site,
Accessing data from SharedPreferences:
SharedPreferences sharedPref = getSharedPreferences("FileName",MODE_PRIVATE);
String webViewChoice = sharedPref.getString("userChoice","null");
if no choice was saved (in the case when the application is running for the first time), you'll get "null" in webViewChoice.
use this condition as you wish
Saving data in SharedPreferences:
SharedPreferences sharedPref = getSharedPreferences("FileName",MODE_PRIVATE);
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putString("userChoice",usersChoice);
prefEditor.commit();
I hope it helps.
Save the user's preference as default-engine=google by default in a shared preferences file.
On app loading, read the file and set the default engine during the app runtime. When user chooses a different engine as default, then update the preferences file.
Hope this helps.