I am having problem with limiting edittext length in android. Everything works perfect except when delete message.
My edit text control has max length of 16 characters and it will limit it for recognizing, displaying and reading first 16 characters but when I continue to type, it memorizes text somewhere in background and if I want to delete text it doesn’t start to delete backwards from 16th character but from last one I have entered.
See below code.
I have even tried to add TextChangedListener it doesn’t trigger 16 character is entered as there isn’t any action.
I am using Nexus 7 for testing this functionality
LAYOUT
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@drawable/background"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="@+id/send_new_message_lblTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/send_new_message_to"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/send_new_message_lblToValue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/send_new_message_lblTo"
android:ems="10"
android:textAppearance="?android:attr/textAppearanceLarge" >
</TextView>
<EditText
android:id="@+id/send_new_message_txtMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/send_new_message_lblToValue"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="@string/send_new_message_hint"
android:inputType="textMultiLine"
android:lines="2"
android:maxLength="16"
android:scrollbarStyle="outsideOverlay" />
<Button
android:id="@+id/send_new_message_btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/send_new_message_txtMessage"
android:layout_marginTop="10dp"
android:text="@string/send" />
</RelativeLayout>
ACTIVITY
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class NewMessageSendActivity{
private TextView to;
private EditText message;
private Button send;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.send_message);
getViewInstances();
setControlActions();
}
private void getViewInstances() {
to = (TextView) findViewById(R.id.send_new_message_lblToValue);
message = (EditText) findViewById(R.id.send_new_message_txtMessage);
send = (Button) findViewById(R.id.send_new_message_btnSend);
to.setText(AppSettings.getInstance().getSelectedDevice().toString());
}
private void setControlActions() {
send.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String smsMessage = message.getText().toString();
Toast.makeText(this, smsMessage, Toast.LENGTH_SHORT).show();
}
});
}
}
I cannot reproduce your behavior. It seems everything is ok, and the text is correctly limited to 16 characters.
The only place where the keyboard keeps appending text, is in the field above it, but even when you select the suggested word, it is truncated before being inserted in the text field. That’s why you don’t see the event fired, because it’s an internal event in the IME, not an Android one inside the
EditText.