how to save multiple data in shared preferences in android - android

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)

Related

SharePrefs not working for Android Spinner - behaviour very erratic

I have a spinner inside of a dialog box, which you can use to select a currency for my app. When you select a currency, it connects to an api from a currency site and displays the parsed info using Json to display the chosen currency's info in my app's mainactivity.
I have used shared preferences to save the index no of the selected spinner choice and have some coding at the beginning of the oncreate method to retrieve the saved spinner index no when the activity is started back up, which will then also apply the currency associated with the index by using the site's api and json again.
The shared preferences file seems to update with the selected index no when I go into it to check, but upon starting the activity it will show the right currency, before very quickly changing to one of the others. The behaviour in retrieving and applying the right index no from shared prefs seems very erratic. Is anybody able to take a look and advise where I might be going wrong please?
An example of the code for when the spinner is used is:
if (newIndex == 1) {
// save inputted spinner position to sharedpreferences
int userChoice = powerSpinnerView.getSelectedIndex();
SharedPreferences sharedPref =
getSharedPreferences("MySharedPref", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.clear();
editor.putInt("UserChoice", userChoice);
editor.apply();
Toast.makeText(getApplicationContext(),"CURRENCY CHANGED TO GBP",Toast.LENGTH_SHORT).show();
The code for applying shared prefs upon start up:
// Retrieve spinner position from sharedpreferences
SharedPreferences sharedPref = getSharedPreferences("MySharedPref",
Context.MODE_PRIVATE);
int spinnerValue = sharedPref.getInt("UserChoice", -1);
if (spinnerValue != 1){
// set the value of the spinner
Toast.makeText(getApplicationContext(),"GBP
SELECTED",Toast.LENGTH_SHORT).show();

How can I save data from an EditText component so it shows up when the app is reopened?

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();

How do I save the progress already made in an app?

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.

First time open my app, request data and save them

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.

Saving any information in list view without using database in Android

I want to make a reminder application without using a database. How can I save reminders (like reminder 1, reminder 2 goes on up to 3) in list view items so that by clicking any list view item, I can open that reminder set by me earlier?
There is a number of storage options available in Android. May be simply storing in files will work for you.
Well just for three reminders I might go for SharedPreferences
Use Shared Preferences for that.
Use below code for save data into shared preferences
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putString(MY_NAME, "Sai");
prefsEditor.putString(MY_WALLPAPER, "f664.PNG");
prefsEditor.commit();
Use below code for get data from shared preferences
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
String prefName = myPrefs.getString(MY_NAME, "nothing");
String wallPaper = myPrefs.getString(MY_WALLPAPER, null);
Shared Preferences
Internal Sotrage
External Storage
SqLite Databases
Network Storage

Categories

Resources