Allow Multi-Line in Edittext View in Android

Allow multi-line in EditText view in Android?

By default all the EditText widgets in Android are multi-lined.

Here is some sample code:

<EditText
android:inputType="textMultiLine" <!-- Multiline input -->
android:lines="8" <!-- Total Lines prior display -->
android:minLines="6" <!-- Minimum lines -->
android:gravity="top|start" <!-- Cursor Position -->
android:maxLines="10" <!-- Maximum Lines -->
android:layout_height="wrap_content" <!-- Height determined by content -->
android:layout_width="match_parent" <!-- Fill entire width -->
android:scrollbars="vertical" <!-- Vertical Scroll Bar -->
/>

How to set Edittext to multiline?

reqdesc = (EditText) myView.findViewById(R.id.reqdesc);
reqdesc.setInputType(InputType.TYPE_CLASS_TEXT |
InputType.TYPE_TEXT_FLAG_MULTI_LINE |
InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
InputFilter[] fArray = new InputFilter[1];
fArray[0] = new InputFilter.LengthFilter(maxLength);
reqdesc.setFilters(fArray);

It now works, I put these lines of codes under onCreate.

EditText multiline with next view button instead of new line button

I think what you are looking for is the same as in this answer:

In code:

editText.setImeOptions(EditorInfo.IME_ACTION_NEXT);
editText.setRawInputType(InputType.TYPE_CLASS_TEXT);

and in xml:

android:inputType="textMultiLine"

how to add multiline edittext with image background and shadow

you can do line this :

          <TextView
android:id="@+id/toolbarTitle"
android:marqueeRepeatLimit="marquee_forever"
android:layout_width="150sp"
android:text="User Name"
android:inputType="textMultiLine"
android:lines="<number of line>"
android:textSize="15sp"
android:textStyle="bold"
android:textColor="#000000"
android:elevation="10dp"
android:layout_height="wrap_content"
/>

Unable to make EditText multi-line

Okay so, I knew that the problem arises when I click on the edit button. Clicking the button made the editText single lined automatically even though I had mentioned the InputType as Multi-line in XML very clearly.

What I did to make the editText multi-line after the button click is that I wrote editTextContent.setSingleLine(false); in my java file inside the onClick button code.
And that is it. :)

Android EditText MultiLine with Sentence Caps

Recently, I found out that when we set inputType, it will override singleLine settings.

  1. Set input type.

  2. Set single line = false.

--

editText.inputType = InputType.TYPE_TEXT_FLAG_MULTI_LINE or InputType.TYPE_TEXT_FLAG_CAP_SENTENCES
editText.setSingleLine(false)

I am not setting imeOptions. This seems to work well.

This seems to work on Emulator and Google Pixel 3xl.

How to retrieve multi-line text from Edittext?

After too much searching and waiting for an answer to this question. I have resolved this issue.

Solution:
I have measured each and every line & words for preserve this as Multiline text, you can use below function for that.

DisplayMetrics metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
float density = metrics.density;

String result = fitString(ipText, ipText.getText().toString());

private String fitString(EditText editText, String message) {

Log.i(TAG, "fitString: Default String : " + message);

StringBuilder finalMessage = new StringBuilder();

if (isTooLarge(editText, message)) {
Log.i(TAG, "fitString: isTooLarge 1 : " + true);
List<String> lineList = Arrays.asList(message.split("\n"));
Log.i(TAG, "fitString: stringList" + lineList);

if (lineList != null && lineList.size() > 0) {

for (int i = 0; i < lineList.size(); i++) {

if (lineList.get(i) != null && !lineList.get(i).isEmpty()) {

if (isTooLarge(editText, lineList.get(i))) {
Log.i(TAG, "fitString: isTooLarge 2 : " + lineList.get(i) + " == " + true);

List<String> wordList = Arrays.asList(lineList.get(i).split(" "));
Log.i(TAG, "fitString: wordList" + wordList);

if (wordList != null && wordList.size() > 0) {
Log.i(TAG, "fitString: wordList : " + wordList.size());

StringBuilder temp = new StringBuilder();
String lastWord = "";

for (int j = 0; j < wordList.size(); j++) {

if (wordList.get(j) != null && !wordList.get(j).isEmpty()) {

if (isTooLarge(editText, wordList.get(j))) {
Log.i(TAG, "fitString: isTooLarge 3 : " + wordList.get(j) + " == " + true);
String newString = fitCharacter(editText, wordList.get(j));
Log.i(TAG, "fitString: fitCharacter == " + newString);

if (j == (wordList.size() - 1) && i == (lineList.size() - 1)) {
finalMessage.append(newString);
} else {
finalMessage.append(newString + "\n");
}

} else {

if (j == 0) {
lastWord = wordList.get(j);
} else {
lastWord = " " + wordList.get(j);
}

temp.append(lastWord);
Log.i(TAG, "fitString: temp : " + temp);
Log.i(TAG, "fitString: lastWord : " + lastWord);

if (isTooLarge(editText, temp.toString())) {
temp.setLength(0); // clear String Builder, new StringBuilder()
temp.append(lastWord);
if (j == (wordList.size() - 1) && i != (lineList.size() - 1)) {
Log.i(TAG, "fitString: ###### 1");
finalMessage.append("\n" + lastWord.trim() + "\n");
} else {
Log.i(TAG, "fitString: ###### 2");
finalMessage.append("\n" + lastWord.trim());
}

} else {

if (j == (wordList.size() - 1) && i != (lineList.size() - 1)) {
Log.i(TAG, "fitString: ###### 3");
finalMessage.append(lastWord + "\n");
} else {
Log.i(TAG, "fitString: ###### 4");
finalMessage.append(lastWord);
}

}

Log.i(TAG, "fitString: finalMessage : " + finalMessage);
}

} else {
Log.e(TAG, "fitString: Word is Null or Empty.");
finalMessage.append(" ");
}

}

} else {
Log.e(TAG, "fitString: wordList is Null or Empty.");
}

} else {
Log.i(TAG, "fitString: isTooLarge 2 : " + lineList.get(i) + " == " + false);
if (i == (lineList.size() - 1)) {
finalMessage.append(lineList.get(i));
} else {
finalMessage.append(lineList.get(i) + "\n");
}
}
} else {
Log.e(TAG, "fitString: Line is Null or Empty.");
finalMessage.append(lineList.get(i) + "\n");
}
}
} else {
Log.e(TAG, "fitString: stringList is Null or Empty.");
finalMessage.append("");
}

return finalMessage.toString();

} else {
Log.i(TAG, "fitString: isTooLarge : " + false);
return message;
}
}

public String fitCharacter(EditText editText, String message) {

Log.i(TAG, "fitCharacter2: Default Word : " + message);

StringBuilder finalWord = new StringBuilder();
int startIndex = 0;
int endIndex = 1;

for (; ; ) {

String tempSplitWord = message.substring(startIndex, endIndex);
Log.i(TAG, "fitCharacter2: startIndex : " + startIndex + " endIndex : " + endIndex + " tempSplitWord : " + tempSplitWord);
if (!isTooLarge(editText, tempSplitWord)) { // isTooLarge
if (endIndex < message.length()) {
endIndex = endIndex + 1;
Log.i(TAG, "IF fitCharacter2: endIndex < message.length() " + endIndex + " < " + message.length());
} else {
String result = finalWord.append(tempSplitWord).toString();
Log.i(TAG, "IF RETURN RESULT : " + result);
return result;
}
} else {
endIndex = endIndex - 1;
String splitWord = message.substring(startIndex, endIndex);
Log.i(TAG, "ELSE fitCharacter2: startIndex : " + startIndex + " endIndex : " + endIndex + " splitWord : " + splitWord);

boolean isTooLarge = isTooLarge(editText, splitWord);
if (!isTooLarge) {
finalWord.append(splitWord + "\n");
}
startIndex = endIndex;
endIndex = endIndex + 1;
Log.i(TAG, "ELSE fitCharacter2: startIndex : " + startIndex + " endIndex : " + endIndex);
}
}
}

private boolean isTooLarge(EditText editText, String newText) {
if (editText != null && editText.getPaint() != null) {
float textWidth = editText.getPaint().measureText(newText);

return (textWidth >= (editText.getMeasuredWidth() - (12 * density))); // editText.getMeasuredWidth();
} else {
return false;
}
}


Related Topics



Leave a reply



Submit