I'm trying to do it but I get a "Stack Overflow error" so I accepted the advice and came here searching for help.
I want to limit the lenght of the text the user can insert, so when he type a character above the length limit the character is erased
sms_input.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable s) {
String cs = sms_input.getText().toString();
if (cs.length() > 160) { sms_input.setText(cs.substring(0, MAX_MSG_LENGTH + 1)); }
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}
});
Logcat:
09-25 17:07:34.972: E/AndroidRuntime(22733): FATAL EXCEPTION: main
09-25 17:07:34.972: E/AndroidRuntime(22733): java.lang.StackOverflowError 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.graphics.Paint.getTextRunAdvances(Paint.java:1754) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.graphics.Paint.getTextRunAdvances(Paint.java:1720) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.text.MeasuredText.addStyleRun(MeasuredText.java:164) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.text.MeasuredText.addStyleRun(MeasuredText.java:204) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.text.StaticLayout.generate(StaticLayout.java:281) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.text.DynamicLayout.reflow(DynamicLayout.java:284) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.text.DynamicLayout.(DynamicLayout.java:170) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.makeSingleLayout(TextView.java:5986) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.makeNewLayout(TextView.java:5884) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.checkForRelayout(TextView.java:6423) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3696) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3554) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.EditText.setText(EditText.java:80) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3529) 09-25 17:07:34.972: E/AndroidRuntime(22733): at com.easyray.lemon.SmsChat$5.afterTextChanged(SmsChat.java:211) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.sendAfterTextChanged(TextView.java:7247) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3703) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3554) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.EditText.setText(EditText.java:80) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3529) 09-25 17:07:34.972: E/AndroidRuntime(22733): at com.easyray.lemon.SmsChat$5.afterTextChanged(SmsChat.java:211) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.sendAfterTextChanged(TextView.java:7247) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3703) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3554) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.EditText.setText(EditText.java:80) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3529) 09-25 17:07:34.972: E/AndroidRuntime(22733): at com.easyray.lemon.SmsChat$5.afterTextChanged(SmsChat.java:211) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.sendAfterTextChanged(TextView.java:7247) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3703) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3554) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.EditText.setText(EditText.java:80) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3529) 09-25 17:07:34.972: E/AndroidRuntime(22733): at com.easyray.lemon.SmsChat$5.afterTextChanged(SmsChat.java:211) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.sendAfterTextChanged(TextView.java:7247) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3703) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3554) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.EditText.setText(EditText.java:80) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3529) 09-25 17:07:34.972: E/AndroidRuntime(22733): at com.easyray.lemon.SmsChat$5.afterTextChanged(SmsChat.java:211) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.sendAfterTextChanged(TextView.java:7247) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3703) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3554) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.EditText.setText(EditText.java:80) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3529) 09-25 17:07:34.972: E/AndroidRuntime(22733): at com.easyray.lemon.SmsChat$5.afterTextChanged(SmsChat.java:211) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.sendAfterTextChanged(TextView.java:7247) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3703) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3554) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.EditText.setText(EditText.java:80) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3529) 09-25 17:07:34.972: E/AndroidRuntime(22733): at com.easyray.lemon.SmsChat$5.afterTextChanged(SmsChat.java:211) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.sendAfterTextChanged(TextView.java:7247) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3703) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3554) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.EditText.setText(EditText.java:80) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3529) 09-25 17:07:34.972: E/AndroidRuntime(22733): at com.easyray.lemon.SmsChat$5.afterTextChanged(SmsChat.java:211) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.sendAfterTextChanged(TextView.java:7247) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3703) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3554) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.EditText.setText(EditText.java:80) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3529) 09-25 17:07:34.972: E/AndroidRuntime(22733): at com.easyray.lemon.SmsChat$5.afterTextChanged(SmsChat.java:211) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.sendAfterTextChanged(TextView.java:7247) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3703) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3554) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.w