Hello Android Developers. I did my research and i cant seem to know how to do the following: 1.-I want the user to enter some input (Numbers) in an EditText and i want whatever number he wrote on that edittext to stay there until he taps that edittext, and decides to input another number. What i mean by "staying there" is that when he leaves the app and he comes back in, for the input to be there. Please guide me step by step since im a begginer please.
Note:I would like for it to not have a "save" button, just for the input to stay put.
First, in your xml for the EditText, make sure to use android:inputType="number" so it is easier for the user to enter numbers.
Second, in onPause() of your Fragment or Activity (whichever holds the EditText, I will assume an Activity for the example code), save your EditText value :
String numbers = mET.getText().toString();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().putString("numbers", numbers).apply();
Then, in onResume(), put it back:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String numbers = prefs.getString("numbers");
mET.setText(numbers);
I hope that helps!
Related
I want to make a smart keyboard that can learn and save new words from user. I already made note and keyboard separately, the problem is :
how to read all keystrokes and write it to my note in background?
how to save my note automatically?
thanks for your help
Keep a String or StringBuilder that stores all the text that the user types. All text sent through your soft keyboard will have to pass through the onKey method.
So, I'd do something like this:
1) In onKey, check to make sure primaryCode (the keycode that was pressed) is a letter/number/apostrophe using the corresponding functions. So, something like
Character.isDefined(primaryCode)
2) Concatenate primaryCode onto the end of your StringBuilder/String.
You'll also have to deal with the user moving the cursor/backspacing. In my keyboard, I only store the most recent two words (resetting this whenever the user moves the cursor). That way, the keyboard can learn what the most likely word is given the last word.
You can save your "note" using an ObjectOutputStream or (if it's fairly small) using sharedPreferences.
Send me an email if you run into an more issues: I've been writing a soft keyboard for a while so I'm pretty familiar with it.
If you have time , please can you take some time for me ? I really need some help.
Let me explain;
Im working on an android app.
There is a layout and it has 5 ImageButton and a webview. When the users click on a imagebutton, without problem it calls a website below..but more or less i have 20 web site.i want to add an option for users..for example the user will choose a web site from Prefs. screen then automatically one of imagebuttons values (i mean icon AND its loadurl function) will change.
I created Pref Screen and i can see my website in this which i wrote in array.xml
but totally im not able to set them to Imagebuttons..
im beggin u.its our last curve..then it ll finish.
Im tried to use this code :
Data = getSharedPreferences(filename, 0);
SharedPreferences.Editor e = data.edit();
e.putString("website", websiteVariable);
e.commit();
but i couldnt.
Please explain me step by step clearly.
also i dont want that only for me,on internet there is no source for this issue.im searching and trying everything what i can think more than 6 days but nothing.
Thank you so much
SharedPreference Data = getSharedPreferences(filename, 0);
SharedPreferences.Editor e = data.edit();
e.putString("website", websiteVariable);
e.commit();
Basically what this does is allows you to store user information such as scores for a game, stats, and other variables.
The first line gets reference or creates the file to write the data to.
The second line allows you to edit the file to write new information to it..
e.putString()
Takes two parameters the first one being the Key to pull the value you out later, and the second being the value you want to put into the file.
The last line commits the data so that it is saved to the file.
You can get more info from the docs here
Also if you want to pull the data out just do
SharedPreference Data = getSharedPreferences(filename, 0);
String value = Data.getString("website"); // use the key here to pull the data out
EDIT:
So for example if the user selects a certain image you could use a key to refer to each image and get the value later to decided which icon the user picked before.
I have an app where I am asking for input a username and password.
Right now. It has been changed. Once the user clicks on login in the login menu.
The username should be refilled with a known username and we only ask the user to input password to improve user experience.
Could we use the exist username edit text in my layout file or i have to change to label instead of edit text? I know i can use setText() to populate the username edit text field.
The question is not about saving username (using sharedpreference). what is the best way to
change my code to use ( either edit box or label ).
Please let me know what is the best case to avoid too much change.
Use SharedPreferences to store the usreanme and then everytime you load the layout in your code, check your sharedPreferences and fill the username edittext using setText
I was wandering if there is anyway to save a String from an EditText and clear it within the same onClick button.
The reason is I am creating a jumble word game where 1 player can jumble a word and then clear the word so the 2nd player cannot see it. The reason why I want to save the String is so I can match it with what player 2 enters.
Any advise would be helpful. Thanks
Place the following fragments of code in your onClick:
// Save
String text = someEditText.getText();
// Clear:
someEditText.setText("");
Currently I have an edittext field that when the user presses enter it does mostly what I want it to, validate an IP Address format and inform the user if it is wrong. How do I make it so when the user presses enter it checks it like it is supposed to be does NOT enter the newline character?
Here is my code for it.
public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
if(validateIPaddress(m_etIPAddress.getText().toString()))
{
ConfigData.m_IPAddress = m_etIPAddress.getText().toString();
}
else
{
showAlertDialog("Invalid IP Address\n Example: \n255.255.255.255\n0.0.0.0","Error: ");
m_etIPAddress.setText(ConfigData.m_IPAddress);
m_etIPAddress.requestFocus();
}
return false;
}
Another problem I have is that in the false condition of the validation, that it will not bring up the soft keyboard to allow the user to reedit that text field. If the user clicks on another edit text the window gives it focus, and allows the user to edit the second text field while still maintaining the 'green outline' around the original edittext. Is there a way to fix this?
EDIT:
Thanks for the response. The EditText still creates a newline. I tried calling that when I create the EditText and it shows the dialog then inserts a newline character at the beginning.. which is weird because the
m_etIPAddress.setText(ConfigData.m_IPAddress);
should automatically overwrite anything in that field to the static IP saved within ConfigData. (my settings class) and I think the focus might work, the problem is that after requestFocus, that EditText shows it has focus but is unresponsive.
I can click on other EditText's and modify them, while it still shows the focus outline on the IP EditText. If I click on the IP EditText it doesn't actually do anything. Its kind of strange.
I think for your EditText creating a new line, you can do that by replacing the enter button by a done button like that :
yourEditText.setImeOptions(EditorInfo.IME_ACTION_DONE);
And to go with that, you can put this to your xml file describing your EditText :
android:maxLines="1"