Hello everybody I'm doing an app which has an uneditable string at the end of the edittext.
For instance,
Quantity ___.00
I want to do something like this.
This is what I've tried so far:
etQuantity.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(".00");
String str = s.toString();
String finalString = str + stringBuilder.toString();
etQuantity.setText(finalString);
Log.d("String", "" + finalString);
}
@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) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(".00");
String str = s.toString();
String finalString = str + stringBuilder.toString();
etQuantity.setText(finalString);
Log.d("String", "" + finalString);
}
});
But I got an error:
01-30 09:49:33.541: E/AndroidRuntime(4294): FATAL EXCEPTION: main
01-30 09:49:33.541: E/AndroidRuntime(4294): java.lang.StackOverflowError
01-30 09:49:33.541: E/AndroidRuntime(4294): at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1013)
01-30 09:49:33.541: E/AndroidRuntime(4294): at android.text.SpannableStringBuilder.getChars(SpannableStringBuilder.java:913)
01-30 09:49:33.541: E/AndroidRuntime(4294): at android.text.TextUtils.getChars(TextUtils.java:74)
01-30 09:49:33.541: E/AndroidRuntime(4294): at android.text.method.ReplacementTransformationMethod$ReplacementCharSequence.getChars(ReplacementTransformationMethod.java:151)
01-30 09:49:33.541: E/AndroidRuntime(4294): at android.text.TextUtils.getChars(TextUtils.java:74)
01-30 09:49:33.541: E/AndroidRuntime(4294): at android.text.MeasuredText.setPara(MeasuredText.java:106)
01-30 09:49:33.541: E/AndroidRuntime(4294): at android.text.StaticLayout.generate(StaticLayout.java:239)
01-30 09:49:33.541: E/AndroidRuntime(4294): at android.text.DynamicLayout.reflow(DynamicLayout.java:284)
01-30 09:49:33.541: E/AndroidRuntime(4294): at android.text.DynamicLayout.<init>(DynamicLayout.java:170)
01-30 09:49:33.541: E/AndroidRuntime(4294): at android.widget.TextView.makeSingleLayout(TextView.java:5986)
01-30 09:49:33.541: E/AndroidRuntime(4294): at android.widget.TextView.makeNewLayout(TextView.java:5884)
01-30 09:49:33.541: E/AndroidRuntime(4294): at android.widget.TextView.checkForRelayout(TextView.java:6423)
01-30 09:49:33.541: E/AndroidRuntime(4294): at android.widget.TextView.setText(TextView.java:3696)
01-30 09:49:33.541: E/AndroidRuntime(4294): at android.widget.TextView.setText(TextView.java:3554)
01-30 09:49:33.541: E/AndroidRuntime(4294): at android.widget.EditText.setText(EditText.java:80)
01-30 09:49:33.541: E/AndroidRuntime(4294): at android.widget.TextView.setText(TextView.java:3529)
01-30 09:49:33.541: E/AndroidRuntime(4294): at com.innovalynx.vms.fragments.SalesOrderDetailFragment$1.afterTextChanged(SalesOrderDetailFragment.java:92)
01-30 09:49:33.541: E/AndroidRuntime(4294): at android.widget.TextView.sendAfterTextChanged(TextView.java:7247)
01-30 09:49:33.541: E/AndroidRuntime(4294): at android.widget.TextView.setText(TextView.java:3703)
01-30 09:49:33.541: E/AndroidRuntime(4294): at android.widget.TextView.setText(TextView.java:3554)
01-30 09:49:33.541: E/AndroidRuntime(4294): at android.widget.EditText.setText(EditText.java:80)
01-30 09:49:33.541: E/AndroidRuntime(4294): at android.widget.TextView.setText(TextView.java:3529)
01-30 09:49:33.541: E/AndroidRuntime(4294): at com.innovalynx.vms.fragments.SalesOrderDetailFragment$1.afterTextChanged(SalesOrderDetailFragment.java:92)
01-30 09:49:33.541: E/AndroidRuntime(4294): at android.widget.TextView.sendAfterTextChanged(TextView.java:7247)
Any thing I'm doing wrong please point me to it. Your help will be truly appreciated.
onTextChanged() is not a place to modify text of EditText.
From doc
This method is called to notify you that, within s, the count characters beginning at start have just replaced old text that had length before. It is an error to attempt to make changes to s from this callback.
So afterTextChanged() can do the work.
Using current approach, you need to some steps as remove the TextWatcher, set the text, and add the TextWatcherback to the EditText. That way, nothing is listening to your EditText changes, and will not cause the StackOverflowError error.
etQuantity.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
String str = s.toString();
if (str.contains(".00")) {
if (! str.endsWith(".00")) {
str = str.replace(".00", "") + ".00";
}
} else {
str = str + ".00";
}
etQuantity.removeTextChangedListener(this); // Remove listener
etQuantity.setText(str); // Set Text
etQuantity.setSelection(str.length() - 3); // Set selection
etQuantity.addTextChangedListener(this); // Add back the listener
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});
Another way is to add one more view like TextView for .00.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With