Save input user in Edittex - java

Good morning, I need help,
I´m new using Java in Android development and need to save user input in a textbox without needing a button to trigger it.
Could someone give an example please?

You will need a TextWatcher, since you do not want to trigger a click event
Here! is a good example of TextWatcher control,
once you get families to TextWatcher, you can save your value either in a SharedPreference (Local tiny storage) or in a Databse, Refere This! for storing and retrieving values from SharedPreference

Related

How to make a save function for a java based android app?

I have an app that encrypts inputs and I want to put a save function that will let users access the "notes" at any time. I thought about adding each in output to an ArrayList, but I still don't know how to save it and display it for the user to select. If you need me to clarify anything I'd be happy to. Thanks.

Java storing user preferences

I'm making a program with a simple GUI, where the user have to do the login before starting use the program.
I want to store username and password and, if the user check the "Remember me" checkbox, i want that he doesn't need to enter data next time.
I tried doing with Java Preferences API, this way:
private Preferences prefs = Preferences.userRoot().node(this.getClass().getName());
prefs.put("codCliente", txtCodCliente.getText());
prefs.put("username", txtUsername.getText());
prefs.put("password", DigestUtils.md5Hex(String.valueOf(txtPassword.getPassword())));
But if i close and reopen the app, if i print the content of variables, it returns "Null key" NullPointerException.
I tried to save them in a .properties file, but I don't want to overwrite every time the whole file, and if I try to use apache commons configuration, with setProperty, I can't put the value on the variables.
Which is the best way to store this kind of data?
Thank you all.
Yes preferences is really easy and good way of doing this , problem is that:
he doesn't need to enter data next time
That means he WONT PUT ANYTHING INTO TEXT FIELDS.
But if i close and reopen the app, if i print the content of
variables, it returns "Null key" NullPointerException.
What did you expected? You put this NULL values into preferences again as he starts his app again.
Problem is in your design
application starts->
read preference of checkbox state->
{reread login credentials} else {nothing} ->
show login ui , user enters credentials ->{compare credentials to whose with preferences} DONT STORE ANYTHING , why woud you store what user is trying to login with every single time you start application?
You only store credentials at user creation ,use encryption and store your data, for both name , and password.
In your question there is not enough code to help you out directly with code sample , you have problem with design , where you reading this information in your code? Why dont you post this part here? You only show us that you store soemthing , well ok , thats useless to person thats trying to help you out.
I recommend you to SIT DOWN, stop writing code , and write yourslef flowchart of your application startup and login process all the way with when you read/write anything to preferences etc.

How to keep track of information regarding fragments in Android

I have a fragment which is sort of like a test. The user can choose from a spinners menu if this test has passed, failed or undecided. Once the user leaves this fragment and then comes back to it later, I want the spinners menu to display what the user had previously selected.
Can anyone tell me how to keep track of this information? I am a beginner with Java and Android programming. Also, let me know if further explanation is required.
It probably depends on what scope you want to be able to refer to these tests in. For example, do you want these fragments to be emptied after the user closes the app, or not?
If it is just while in the app "session", you could just create an object in your fragment and override the onResume method to populate the spinners with the appropriate data.
If you want to save the info after the app is closed, there are a couple of options. For smaller sets of data you could use shared preferences
if you have an undetermined amount of "tests", I would advise saving whatever data you need to a file, and parsing it to fill the fields in the fragments
Take a look at this to get an idea of how to save data: http://developer.android.com/intl/es/training/basics/data-storage/files.html
I've done something similar to what your doing. I used shared preferences to save the string like so
Saving the string you got from the spinner in a fragment
SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
SharedPreferences.Editor mSharedPreferencesEditor = mSharedPreferences.edit();
mSharedPreferencesEditor.putString("my_unique_key", myString);
// commit changes
mSharedPreferencesEditor.apply();
Remember to always apply the changes after you made them and you can save as many strings as you want as long as they each have a unique key.
Then to retrieve the value just
SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
String myStringValue = mSharedPreferences.getString("my_unique_key", null);
The second argument (null) will be returned if it could not find any data that had that unique key. (i.e if you haven't saved it yet)
You can also use another string instead of null like this
String myStringValue = mSharedPreferences.getString("my_unique_key", "could not find value");
or if you want to bundle a bunch of data together like say create an object to save a bunch of strings then save that object to a file I could show you that if you wanted to

How/where can I "stash"/keep data in my application?

I display dates to the user in a layout that are let's say are textual.
When the user presses a button I want to get the information in those fields that represent these "dates" but if I get the text in them is not of value to me.
I would need to store somewhere the original dates that created these "textual" elements and fetch them from there.
Is there a specific construct in android that one can use as a stash area or should I just use a static class with variable to hold them?
In your case, you should use SharedPreferences to store the data by converting it into a String (text) or int/long first.
This will allow you to easily write and retrieve data, and you should use this.
You can also use the file system to save almost any Java object using serializable, on Internal Storage.
Either way, the data will stay there even if your app is closed or the device is turned off.

Saving and Adding to A List (Android)

Plain and, hopefully simply...
-What I would like to do is make a list of strings.
-I would like to add to this list while in the application.
-Finally, I want to get each String from this list.
This must be saved somehow so that when you close it and open it back up, the list will save...
How should I get around to this? SharedPreferences? An SQL Database? What should I use to accomplish this?
I vote SharedPreferences since it will be faster and will persist throughout the life span of the application.
If you need to have the saved data made available during the next time the app is run, you will most likely want to use a DB

Categories