i have almost completed my game and now i wish to do something with the scores and name so i want to have a high score list...
The problem is that i cannot retrieve the name when i m trying to draw the name using alphabet bitmaps what i have preprared!!!
now the problem occurs when i m tryin to use the getSharedpreferences and m not able to copy the name anywhere in the whole project so that when in the activity of showing the high score i can draw it!!!
is there a simple way to do it please suggest!!!!
thank you!!!
The best way to store a value which will be available all around your application is, as you said, the SharedPreferences.
To store your name user you can do this:
SharedPreferences mPrefs = getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("username", "your name");
editor.commit();
To retrieve your username you can do this:
SharedPreferences mPrefs = getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
String username = mPrefs.getString("username", "");
Related
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.
I have a school planer app and I want to save the classes that are written in an EditText object. The problem is, the EditTexts are empty when I close the app and then open it again, and even when I press back. This may be a noob question, but I'm a beginner and I need help. Please answer quickly...
for saving edittext text you need to store the text into storage.
android support storage like
Database
Shared Preferences
For your problem you need to store your Edittext text into the storage when you change value.
And when you open it again you need to retrieve from the storage.
For you best option will be shared preferences. For more Go with This Example . This will make you understand save and retrieve shared preferences.
Short Example
Store Data
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
editor = pref.edit();
editor.putString("first_edittext", "value");
editor.commit();
Retrieve Data
SharedPreferences pref = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
pref.getString("first_edittext", "");
i want to make a game that
when some one write his/her name in a text input it shows in next frame:"Welcome(player name)to the game
What should i do now?
Help me please as much as u can.
Thank U.
Store it in SharedPreference and use it wherever you want to use it.
take a look at this :
http://developer.android.com/reference/android/content/SharedPreferences.html
Simple example code :
//save player name
SharedPreferences sharedpreferences = getSharedPreferences("MyPREFERENCES", Context.MODE_PRIVATE);
Editor editor = sharedpreferences.edit();
editor.putString("name", "player name");
editor.commit();
//fetch name
sharedpreferences.getString("name","defaultName");
I saw this on stack Need to save a high score for an Android game
This is what it told me
//setting preferences
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putInt("key", score);
editor.commit();
//getting preferences
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
int score = prefs.getInt("key", 0); //0 is the default value
I was wondering, should "key" be the string where the highscore is located? and does it matter what i name my prefs key.
Thanks for your time.
The Android Developers documentation is an excellent place to start with any question like this. SharedPreferences can be found here. SharedPreferences is a simple Key-Value pair storage, so if you put an int into your shared preferences as with the key "high score", then in the future, if you want to get that int, you need to use prefs.getInt("high score"). It doesn't matter what you name your keys, but it is generally good practice to use private static final string variables to store keys you will use on a regular basis.
You can name the string whatever you like. Click here for documentation. You can name it anything from "foo" to "bar".
I am using shared preference to store password but when ever my application starts it again ask preference value .i want once i have entered the preference password it should be fixed it should ask me again and again.
Where you want save your password use below code -
Editor editor = getSharedPreferences("password", 0).edit();
editor.putString("password", "your password");
editor.commit();
And where you like to get it back put below code there -
SharedPreferences pref = getSharedPreferences("password", 1);
String password = pref.getString("password", "");
If still its not working, Put your code out here.