Reputation: 57
How to limit number of character in a line with multi-line EditText in android I am doing like this by this is not allowing multiline
<EditText
android:id="@+id/MsgText"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_margin="5sp"
android:layout_weight="1"
android:background="@drawable/message_field"
android:inputType="textMultiLine"
android:maxLength="10"
android:padding="5sp"
android:hint="Type a Message"
android:textColor="@color/textFieldColor" />
Upvotes: 0
Views: 4989
Reputation: 11550
You can use this.
final EditText edit = (EditText)findViewById(R.id.MsgText);
final int maxLineLength = 10;
edit.addTextChangedListener(new TextWatcher() {
final Integer mark = 1;
String textBeforeEdit = null;
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
textBeforeEdit = s.toString().substring(start, start + count);
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
edit.getText().setSpan(mark, start, start + count, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
@Override
public void afterTextChanged(Editable s) {
String str = s.toString();
int spanStart = s.getSpanStart(mark);
int spanEnd = s.getSpanEnd(mark);
int lastNL = str.lastIndexOf('\n', spanStart);
int nextNL;
while(lastNL < spanEnd) {
nextNL = str.indexOf('\n', lastNL + 1);
if(nextNL == -1)
nextNL = str.length();
if(nextNL - lastNL > maxLineLength + 1) {
// reject the entire change
s.replace(spanStart, spanEnd, textBeforeEdit);
break;
}
lastNL = nextNL;
}
s.removeSpan(mark);
}
});
What the code does is that it watches, at each change to the text (which may equally be keyboard input or paste), whether any affected line has grown to more than maxLineLength
characters (plus one for the \n
). We don't care about lines before or after the change, so we can start counting at the last \n
immediately preceding the start of the region that was rewritten. (If the lastIndexOf
gives -1
that's fine, too.) We find the next \n
, if it's no more than maxLineLength + 1
characters past the last, that's fine and we advance until after the span (or at the end of the string).
Things get interesting when the condition is ever broken: for this reason we store an invisible mark at the beginning and at the end of the region that has been changed (relative to the start of the new text) as well as the original text that was rewritten. By replacing the new region by the old contents we effectively reject the change, so the action will be ignored.
What's good about this approach is that
Upvotes: 2
Reputation: 468
private final TextWatcher textWatcher = 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) {
if (s.length() == maxCharacterForLine) {
s.append("\n");
editText.setText(s);
editText.setSelection(s.length());
}
}
};
editText.addTextChangedListener(textWatcher);
maxCharacterForLine is the character limit of the each line.
Also you should check for Copy&Paste.
Upvotes: 0
Reputation: 13555
You can try LengthFilter
EditText editText = new EditText(this);
int maxLength = 3;
editText.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maxLength)});
Upvotes: 1
Reputation: 843
You can set android:maxWidth="size". Choose the width in which your number of characters can come. Also use size in dp so that it behaves similarly in all devices.
To limit on total number of characters -
Below code in Java to check the length of the edittext. If it exceeds the limit you can show it as a warning.
your_edit_text.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) {
int len = question.length();
warning_text_view.setText(100 - len + " char. left");
if (100 - len < 0) {
warning_text_view.setText("limit Exceeded!");
warning_text_view.setTextColor(Color.RED);
} else
warning_text_view.setTextColor(Color.GRAY);
}
});
Upvotes: 0
Reputation: 1044
You can try android:ems="10"
, which will not exactly limit your characters to 10 per line but it will make the EditText
's width equivalent to 10 "M" characters.
Upvotes: 1