I am pretty new to Android / Java App development and wanted to ask,
the best way to save the app condition.
In my case I have two buttons, which have a default color of red.
When the user clicks on each of them it gets the color of blue.
What's the best way to save this condition?
So that the color will not reset to the color of red when I restart the app.
Try to look here.
Principal data storage options in Android:
Saving key-value pairs of simple data types in a shared preferences
file
Saving arbitrary files in Android's file system
Using databases managed by SQLite
Related
In my application after login I have to save state in shared preference to change the app view on a button click. can anyone help me to save state in shared Preferences and on a button click i need to replace the initial state with the new one.
You can't save a layout. You can save a layout id- but I wouldn't suggest it. Resource ids are not stable across builds, so any update would break it. Your best bet is to create an enum with whatever values you wish it to have, save the enum, and later on convert the string back into an enum when you read it in. Then hold a map<enum, resourceId> in code that maps to the correct resource id.
So for a small assignment, I was tasked with making a program that will generate a random image from a file on the computer
what I did was move the icons from the desktop to the drawable folder
in the image you can see that the files are there under the drawable folder
Here is the user interface
the code is also available in the picture. The issue is that the program would crash after I press the button and no picture is ever shown.
It looks perfect to me and at this point, I do not know what is wrong with it
Thank you for whoever answers this
Ps. I am using a mac for this (not sure how important this piece of info is )
image.setImageResource(i) you are passing random number in the setImageResources
You are passing wrong Image Resource Id
so replace with the image.setImageResource(array[i]) because your image resources ids stored in this array
Simply replace image.setImageResource(i) by image.setImageResource(array[i]).
What you want is to access the resource id which is stored in the array.
I've scoured for the answer for this. I have a simple text based game. I make a choice with a radio button, and confirm it with a button click. I've set the game to save for when I change to landscape from portrait view.
But I cannot for the life of me find how to save the game when the back button is pressed.
I'd like to have a simple menu on the title screen with three buttons, one of them being "Continue" which would restore the game's saved settings. And obviously, one in the action menu I already have set up which would reset all the game data.
I've tried sharedPreferences etc. if someone can tell me the way to save the game data by using SQL so no user can mess with the data, that'd be better.
Thank you in advance for you help.
on edit I realized my question is not specific enough. I am trying to retain the state of a TextView which I am modifying with user choices, thus changing the text. Every time the app is restarted, the first text loads.
It kind of depends on how much information you want to save, but if it's very little I'd look in to shared preferences which saves key value pares. If it contains a bit more, you should look in to SQLLite which is just a database for your app on the device.
Further reading:
SharedPreferences:
https://developer.android.com/training/basics/data-storage/shared-preferences.html
SQLLite:
https://developer.android.com/training/basics/data-storage/databases.html
Have you tried serialization? Put the informations in a file, like a text file, then simply un-serialize it and take back the informations. You can always encrypt it if you don't want anyone to change the datas, like a real game save file.
In my app, i have you put in some information through some edit text. Then you hit this button and it starts another activity that does some calculations and then displays results through text views. Well i want to be able to save all of those text views, and then open them up later. On the home screen i have a load button. When you click it, I want to be able to see the stuff I've saved and be able to open it by clicking on it. I'm new to android, so I'm having a hard time figuring this out. How should I go about doing this?
Use SharedPreferences...Check out..
http://www.vogella.com/articles/AndroidFileBasedPersistence/article.html
http://androiddeveloperspot.blogspot.in/2013/01/sharedpreference-in-android.html
this will help.
I see two questions in your question. The first is how to pass data from one Android activity to another.
The best way to do that is by using the putExtra method of the Intent class and then the getExtras method to extract the data in the receiving activity. Please see this SO quest and answers for more details.
For your second question, how to save data that can be recalled at a later by the main activity, you can use the SharedPreferences APIs. See this web page for more information on that. Basically the shared preferences APIs allow you to save key-value pairs of information which in your case can be field names (keys) and their associated values. For this you would want private preferences and pick a unique name for this preference file.
I have an Android settings screen (i.e. using classes related to the Preference class) where the user can configure 3 different colors. Each color is stored as an integer using the shared preferences functionality.
I want to let users save and restore the colors chosen (i.e. color schemes). At the top of the settings screen, I want a button that pops up a list of all current saved color schemes. Picking a color scheme would set the 3 color settings to the colors for that color scheme. If the user chooses to save the current colors, they are asked to name the color scheme and this color scheme will then appear in the color scheme selection list.
What's the simplest way to implement this functionality?
I think using the Android built-in SQLite database is your best option. You can create an SQL table colorscheme with 4 columns: id, color1, color2, color3. Then query the SQLiteDatabase.query method.
Have a look at the NotePad example at http://developer.android.com. Or at this tutorial.