Is there any way to set a maximum of Chars to a line in an EditText
Field?
I tried it with a TextWatcher
, But I cant really do what I want.
I would like to set a maximum of lines and a maximum of chars per line.
Example:
Inserted Text:
Lorem ipsum dolor sit amet consetetur sadipscing elitr.
Result (Max-Lines: 10, Max-Chars per line: 20):
Lorem ipsum dolor
sit amet consetetur
sadipscing elitr.
EDIT XML:
<EditText
android:id="@+id/edittext"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_alignParentRight="true"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:background="@drawable/bgedit"
android:gravity="top"
android:lines="11"
android:paddingLeft="35dp"
android:paddingTop="20dp"
android:singleLine="false"
android:inputType="textMultiLine"
/>
Add this in the layout:
EDIT:
As you can see in above answers , you cant limit line length in edit text. But you can use text watcher that you mentioned to detect any changes in text. The override methods are self-explanatory in text watcher. Just make sure you dont edit, ie add text to the associated view when using text watcher. It will go in a recursive loop.
Add this
android:maxLength="5"
You can use following properties:-
if want single line:-
else for fix lines:-
The following filter adds a new line after every n characters:
Usage:
Result:
Also, you can add more code to append new-line char at word beginning, to make it wrap by word.
Set these parameters in the layout:
Experiment with the value of
android:layout_width
to find the value that fits your needs.