I'm trying to get what modification user has made to EditText, either insert or delete. I use TextWatcher but I don't get right result, moreover sometimes "getChar(start, end) has end before start" error.
editText = (EditText) findViewById(R.id.MyEditText);
editText.addTextChangedListener(new TextWatcher() {
#override
public void afterTextChanged(Editable s){}
#override
public void beforeTextChanged(CharSequence s, int start, int count, int after){
showToast("text removed: " + s.subSequence(start, count));
}
#override
public void onTextChanged(CharSequence s, int start, int before, int count){
showToast("text added: " + s.subSequence(start, count));
}
}
As you can see I use beforeTextChanged to get any text that's removed by user, and onTextChanged for insertion. Please shed some light here. Thanks!
API is right here: http://developer.android.com/reference/android/text/TextWatcher.html#afterTextChanged(android.text.Editable)
EDIT:
I seem to figure it out...it's quite silly: s.subSequence(start, count)) should really be s.subSequence(start, start+count))
Just keep your functions inside the afterTextChanged and see what happens
Sample Code
seachbox.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
fillData(SEARCH_ORDER ,s.toString());
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
});
I hope it will work
Try this, I'm not sure whether you want the remaining word(after insert/update) or the letter(added/removed).
public class MainActivity extends Activity implements TextWatcher {
private EditText myEditText;
private String inputText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myEditText = (EditText) findViewById(R.id.testEditText);
myEditText.addTextChangedListener(this);
}
#Override
public void afterTextChanged(Editable s) {
if (inputText.length() < s.toString().length()) {
Toast.makeText(
this,
("Text Added: " + s.toString().substring(inputText.length(),
s.toString().length())), Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(
this,
("Text Removed: " + inputText.substring(s.toString().length(),
inputText.length())), Toast.LENGTH_SHORT).show();
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
inputText = s.toString();
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
}
Related
Trying to get an integer from Edit Text entered by the user(such as Total Amount) and to display it by multiplying with value(such as 5%) and display it using TextView automatically.
For the 1st part i.e. simply displaying the integer from EditText to TextView got NumberFormatException Error.
'''
temp = (Integer.parseInt(editText.getText().toString()));
textView.setText(temp);
'''
And for the second part i.e. automatically displaying the value from EditText to TextView, no idea how to approach.
Just use textwatcher and put the logic in it. Just make sure change the id of edittext and textview
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) {
String value = s.toString();
double outputValue = Integer.parseInt(value) * 0.05;
textView.setText(String.valueOf(outputValue));
}
});
Make sure in edittext that it only capture the numerical value.
you could try this approach
editText.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) {
String userInputText = s.toString();
try {
val userInputAsNumbger = userInputText.toInt();
}catch (e:NumberFormatException){
print(e.message)
}
}
});
to ensure it doesn't error
I'm new to Android development and I want to create a method that runs every time my editText is changed.
In swift I use this function:
func fieldChanged(textfieldChange: UITextField){
}
I can't find anywhere a similar function for Java and Android Studio, are there any suggestions?
// Globally declare a variable
private TextWatcher mTextWatcher;
// Call textChanger() method in onCreate
private void textChanger() {
mTextWatcher = 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) {
if (s.length() == 6 && mEditText.getText().toString().trim().equals("some value to check"))
mEditTextOther.requestFocus();
else
mEditText.setError("enter correct");
}
#Override
public void afterTextChanged(Editable s) { }
};
mEditText.addTextChangedListener(mTextWatcher);
}
try this
editText.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) {}
});
I have 2 buttons and 1 number field, if I press a button without something in the field, it crashes, so what I want to do is disable the buttons unless the number field has something in it, I have looked around for an answer but either they aren't relevant, or I'm not sure how it would fit into my code, here are the two onClick functions for each button. Thanks
public void toPounds(View view){
EditText amount = (EditText)findViewById(R.id.amount);
Double omrAmount = Double.parseDouble(amount.getText().toString());
Double gbrAmount = omrAmount * 1.79;
Toast.makeText(getApplicationContext(), "£" + gbrAmount.toString(), Toast.LENGTH_LONG).show();
}
public void toRiyals(View view){
EditText amount = (EditText)findViewById(R.id.amount);
Double gbrAmount = Double.parseDouble(amount.getText().toString());
Double omrAmount = gbrAmount / 1.79;
Toast.makeText(getApplicationContext(), omrAmount.toString() + " Riyals", Toast.LENGTH_LONG).show();
}
yourField.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if(s.length() == 0)
button1.setEnabled(false)
else
button1.setEnabled(true)
}
});
link
If you want to disable buttons if edit text is empty then you can do the following :
EditText amount = (EditText)findViewById(R.id.amount);
Button button = (Button) findViewById(R.id.button1);
if(amount.getText().toString().isEmpty()){
button.setEnabled(false);
}
amount.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if(s.length() == 0)
button1.setEnabled(false)
else
button1.setEnabled(true)
}
});
Not specifically an answer to your question, but generally speaking you would want to add some sort of check before you call your code which now crashes your application. It's not a good idea to to have code which crashes your app lingering around.
Maybe make a method like: isMyEditTextValid(...){..}
I tried the following code, but android doesn't let me do that, as I'd enter into an infinite loop.
mEditText = (EditText) getActivity().findViewById(R.id.input_content);
mEditText.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) {
//I want to format the already existing text in s in a certain way as the user is typing
mEditText.setText(s.toString() + " \n");
}
});
Please any ideas would be very helpful. Thanks
3 possible solutions :
Check for the carriage return presence in the string
Like this :
mEditText = (EditText) getActivity().findViewById(R.id.input_content);
mEditText.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)
{
//I want to format the already existing text in s in a certain way as the user is typing
if (s.toString().charAt(s.length() - 1 ) != '\n')
{
mEditText.setText(s.toString() + " \n");
}
}
});
Not the best solution, according to what's in your input string
Do it in beforeTextChanged
Like this :
mEditText = (EditText) getActivity().findViewById(R.id.input_content);
mEditText.addTextChangedListener(new TextWatcher()
{
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
//I want to format the already existing text in s in a certain way as the user is typing
mEditText.setText(s.toString() + " \n");
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
#Override
public void afterTextChanged(Editable s) {}
});
Cancel the listener, temporarily
Like this :
final TextWatcher tw = 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)
{
//I want to format the already existing text in s in a certain way as the user is typing
mEditText.removeTextChangedListener(tw);
mEditText.setText(s.toString() + " \n");
mEditText.addTextChangedListener(tw);
}
});
mEditText = (EditText) getActivity().findViewById(R.id.input_content);
mEditText.addTextChangedListener(tw);
You are most likely entering an infinite loop because you are changing the text in public void afterTextChanged(Editable s) which then causes the method to be called again.
The best solution would be to change the text in one of the other methods, or on the loss of focus of the editable. See View.OnFocusChangeListener for more details on this.
Here is an example:
textView.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) {
textView.setText(s.toString() + " \n");
}
}
});
I have e project where I have a Edittext from where i have to get input on every keypress or something like every character input.And Have to perform a livesearch. What kind of Listener i have to use to get input for every character input .
Do this way
editSearch.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
#Override
public void afterTextChanged(Editable edit) {
if (edit.length() != 0) {
// Business logic for search here
}
}
});
An editable text view that shows completion suggestions automatically
while the user is typing. The list of suggestions is displayed in a
drop down menu from which the user can choose an item to replace the
content of the edit box with.
http://developer.android.com/reference/android/widget/AutoCompleteTextView.html
Here is a good tutorial:
https://developers.google.com/places/training/autocomplete-android
http://android-er.blogspot.com.au/2010/07/example-of-autocompletetextview.html
You need to use 'TextWatcher' as I have already answered here
It will give you result as shown in below images:
Try this..
edittext.addTextChangedListener(new TextWatcher()
{
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
String textl = edittext.getText().toString().trim();
}
});