How do I save some textviews and load them from another class? - java

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.

Related

How to save a state in Shared preferences on Android Java?

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.

Looking for a way to save game data in my text based game in android studio (java)

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.

Add additional suggestion at top of AutoCompleteText suggestions

I want to make the first suggested line for every AutoCompleteTextView be what the user has currently typed, similar to how google search bar works in smartphones. How can this be achieved?
you have to store the value that user typed in sqlite or shared preferences first.
Then you can filter the value based on user's new search...
Take a look at this link: it uses a web service to load data
http://makovkastar.github.io/blog/2014/04/12/android-autocompletetextview-with-suggestions-from-a-web-service/

Android: Using an intent to start any activity

I have been working on an application that has a bunch of formulas in it. The user can select which formula they need to use, input the numbers/variables, and the program will return whatever answer they are looking for. Each formula has its own class and since there are so many classes I sorted them into separate packages.
For Example, I might have 3 packages (Volume, Area, and Main).
In the Volume package, I might have 2 classes (Cube_Volume, and Sphere_Volume).
In the Area package, I might have 3 classes (Square_Area, Circle_Area, and Triangle_Area).
In the Main package, I might have 1 class (Main_Activity).
I am currently trying to allow the user to bookmark their favorite formulas. If a user is in the Sphere_Volume class, they can bookmark that class. How I am currently attempting to do this is when the user chooses to bookmark the class, the application saves the string returned by this code.
this.getLocalClassName()
The string that is saved (I am using an SQL database to store the string) will look something like this:
com.example.area.Triangle_Area
The bookmarks that the user saves (like the one shown above) are displayed in a Listview in the Main_Activity class. Obviously, when the user clicks on a bookmark in the Listview, I would like the program to start the corresponding activity.
So finally, the question is: How can I use an Intent to start any activity in any other package when the string provided is something like com.example.area.Triangle_Area?
Thanks for any and all answers! Please comment if I need to elaborate more on my issue.
Since Intent constuctor accepts the component class as its parameter, it's as simple as this:
context.startActivity(new Intent(context, Class.forName("com.example.area.Triangle_Area"));

Show an option list after click on EditText

I've one seen on an application for iPhone an interesting idea which consists of showing a list of options when we click on an area for text edition. For example, imagine I have a field called "City" (EditText) which I am suposed to enter the name of the city. When clicking on the EditText, a list automatically shows up with a few city suggestions (e.g. defined by the programmer) which can be selected. If the user doesn't like the suggestions, he writes the city himself and this city can be saved for this list for future.
I want to programme this on my App for android. I need that, when clicking on the EditText for introducing the data, a list automatically shows up with some suggestions defined by me (Programmer). However, I don't know how this can be done. I've googled but maybe it's hard to find the correct keywords to find a similar topic.
The idea is that the user should use the names already written in the list in 90% of the time and just write himself the new ones when it's necessary. Something like a dropdown list but with possibility of writing new stuff instantly without specific option.
How can this be achieved?
You Need Something Like View in android Named as
AutoTextComplete
Examples How to use this :
help
Best of luck
You can consider focus changed listener for Edittext,
when focus is gained you can show a context menu to choose data from.

Categories

Resources