Hi I am learning Android programming and have run into an issue that I couldn't get a clear answer to through researching.
I have a TextView which serves as a label for my EditText. I have a method which checks if the EditText is an empty String. If the string is empty I want to be able to get a reference to the TextView that corresponds to that EditText in order to make a toast saying something like "please enter a value for ".
I've looked into getLabelFor/setLabelFor but is there a way to do this in the layout XML?
What is best practice for this type of functionality.
You're describing a functionally that is build in to EditText. There is a special field you can define in xml called hint, which is the recommended way to label an EditText rather than a nearby TextView. Additionally, EditText has a method called setError() (link). If the user attempts to hit a submit button, for example, you can check to see if the EditText is empty and if so, call setError().
I wonder if the following is the thing that you need
TextWatcher inputTextWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after){
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().equals("")) {
textView.setText("please enter a value for ..");
} else {
textView.setText("<the textedit is not empty>");
}
}
public void afterTextChanged(Editable s) {
}
};
editText.addTextChangedListener(inputTextWatcher);
Related
I'm making a simple app, where there is one TextInput field and a button next to it.
The TextInput field loads #string/duration_time from strings.xml.
The button next to it will display a simple message ("hello" + the string duration_time). However, it will always display the original information from string duration_time.
How do I set up a TextChangedListener to update the string/duration_time to reflect the input from the user? How do I access the information from TextInput and use it? Because as in the example below, under "public void afterTextChanged" I am trying to save the information, but I don't know where it's stored.
Kind regards
button2.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
String duration_input= WHAT_Do_I_Put_Here;
}
});
Suppose you are using editText and button as Android Wigets.
Now if you want to get the the text inside editText on click of button.It can be done like this
String textEdit = "";
button.setOnClickListener(->view{
textEdit = editText.getText().toString();
});
Now your textEdit will contain the text which you entered in your editText.
I have an application where user inputs text into EditText field. After user clicks OK (in keyboard input mode), a correct value is in the EditText (lets say "Smile").
if (answers.get(counter).getText().equals(opponentAnswers.get(counter)))
But this if statement fails, because the same EditText has the values that were suggested by T9 option, when user was inputing his answer (for example values of EditText would be "Smile Smiling Smiled"), while it should only have a value "Smile".
Any ideas how to solve this issue?
That's really weird. These are kind of guesses, but this is what I'd try next if I were you:
A. Instead of doing an equals against getText(), try doing a toString on getText(), so:
if (answers.get(counter).getText().toString().equals(opponentAnswers.get(counter)))
B. If that doesn't work then you could try adding a TextWatcher using addTextChangedListener on the EditText, and getting the value from that. Calling toString() on the editable returned in afterTextChanged might give you the value you want.
private class SearchTextWatcher implements TextWatcher {
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
#Override
public void afterTextChanged(Editable s) {
//Get the text the user sees
String textShownToUser = s.toString();
}
}
Hope this helps! Best of luck!
I have 3 widgets on my screen, Text View, Edit Text and a Button. What ever I insert in my edit text, when I click the button, the text view gets the string from the edit text. Now, what I want to do is that, if I have already inserted the character "\" or "," or what ever character I want, it will not be inputted anymore. It's like, you can only put that character once in the edit text. Do you guys have any idea about it?
Well what I am thinking is that, I have to search from the edit view then validate it. But I don't know what code to use. Could somebody please help me? Thank you!
Use Android TextWatcher on EditText.
There are delegates which returns the charsequence that is entered
onTextChanged
afterTextChanged
beforeTextChanged
Fill the entered character in set everytime. If size is not incrementing that means a duplicate.
then avoid adding of that character in edittext
I think you need to add a TextChangedListener to your editText.
et.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
I need to control pressed buttons, before they goes to my EditText widget. It's should work like filter.
For example: I need that user could fill EditText only with digits 1,2,3,4,5 other symbols must be ignored. So the part of buttons on virtual keyboard should be disabled or I need to catch last pressed symbol, analyze it and disable for EditText.
Who knows the way how to solve this problem?
Thanks..
statusEdt.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {
//do stuff
charTxt.setText(statusEdt.getText().length() + "/140");
}
});
I used this TextChangedListener(TextWatcher) to keep a count of how many characters had been typed into an EditText for a Twitter client I made. You could probably use a listener like this. You'll want to override beforeTextChanged or onTextChanged. These methods will pass you whatever CharSequence has been typed. You can check what was typed in and if it is not valid input you can remove it by calling setText() and passing in whatever has been typed so far minus the invalid characters.
What you probably need is an InputFilter.
For example to allow only digits, sign and decimal point:
editText.setFilters(DigistKeyListener.getInstance(true, true));
I want to cause the focus of one edit text box to move to another on editting (meaning you can only type on letter before it automatically moves on to the next edit text).
It's the "on edit" that I can't get my head around. Can anyone help me out with a simple example? Theres a lot I need to implement it into, so just a basic understanding should set the ball rolling ^_^
I do not really recommend this. With soft keyboards and multiple languages, what exactly is "one letter"? After all, a soft keyboard might enter in an entire word, like it or not.
CommonsWare makes an excellent point: you can't prevent the user from adding more characters to the EditText box, however you can listen to what's changed and act on that. Here's how to:
EditText editbox = (EditText) findViewById(R.id.MyEditBoxName);
editbox.addTextChangedListener(new TextWatcher()
{
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
public void afterTextChanged(Editable s)
{
// Test s for length, request focus for the next edit.
// editbox2.requestFocus();
}
});
Be careful not to get yourself into an infinite loop changing the editbox, any changes you make will cause these methods to be called again recursively.