Android EditText: need to combine multiline and long string - android

I'm trying to implement something like a text editor.
I want to have a possibility to edit a long string without word-wrapping.
The edited string should be scrolled horizontally.
New line should be entered only when I press Enter.
So, my editor should be able to edit as long string as user desires (with horizontal scrolling) and be simultaneously multi-lined.
<EditText
android:id="#+id/editor"
android:background="#android:color/transparent"
android:scrollHorizontally="true"
android:lineSpacingExtra = "3sp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:typeface="monospace"
android:textSize="16sp"
android:inputType="textMultiLine"
android:scrollbars="vertical|horizontal"
android:gravity="left|top"
android:text="Code editor"
tools:ignore="HardcodedText"/>
In this case long strings are word-wrapped.
And
android:inputType="text"
gives me a possibility to edit long strings in one line... but how to enter a new line?
Pressing [Enter] doesn't enter a new string, just the next focusable element is selected.
I tried all combinations that could imagine, but nothing worked properly.
Either the text is multi-lined with word-wrapping or it's presented in one wide line.
I wondering if it's possible at all, to get such desired behaviour.
Now I'm thinking about subclassing EditText and manually handling Enter pressing.
But I don't like this approach.
How to get horizontally scrollable EditText with 'multiline' feature?
Any ideas?

You can combine the MUTILINE input type and the ability to scroll horizontally:
editText.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_FLAG_MULTI_LINE);
editText.setHorizontallyScrolling(true);
or in xml :
<EditText
android:inputType="text|textMultiLine"
android:scrollHorizontally="true"
.../>

Related

Android XML - EditText word-wrapping without newlines

I have this stupid and seemingly trivial problem with the properties of an EditText.
The properties I am trying to achieve for the EditText, are the following:
The contents of the view should NOT contain newlines. It can be text, numbers, symbols, but no newlines.
The soft keyboard should NOT display the enter button because of the above. It should instead display something like "Send" or "Done".
The contents of the view should NOT continue horizontally when reaching the edge of the screen. Instead I want to wrap the text, displaying it on multiple lines.
I have tried many different combinations, but I can not achieve this combination.
What I currently have is this, which is inside a RelativeLayout:
<EditText
android:id="#+id/comment_box"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_below="#id/preparation_text"
android:hint="#string/comment_hint"
android:inputType="textCapSentences|textAutoCorrect|text"
android:scrollHorizontally="false"
android:maxLength="400"
android:imeOptions="actionSend"/>
It achieves 2 of 3. No newlines possible, keyboard displays "Send" rather than the enter-key for me, but the text continues on one line.
Changing inputType="text" to "textMultiLine" wraps text correctly on multiple lines, but also overrides the keyboard to always display the enter button.
I have tried different solutions around the Internet, including setting the properties maxLines="4", singleLine="true" and possible others that I have forgotten again.
I can not find a combination that works.
Output:
Exp:
The op is on right track. I did some research found that some options gets ignored which are specified in XML. but if the same options are set in code then it should do the trick. I used the same XML fragment specified in the question.
<EditText
android:id="#+id/edit_text"
android:layout_width="match_parent"
android:layout_height="80dp"
android:hint="hint"
android:inputType="textCapSentences|textAutoCorrect|text"
android:scrollHorizontally="false"
android:maxLength="400"
android:imeOptions="actionSend"
/>
And by adding the following lines in the code, it helped in achieving what you want.
edit_text.setMaxLines(Integer.MAX_VALUE);
edit_text.setHorizontallyScrolling(false);
textShortMessage is the inputType that you are looking for:
<EditText
android:id="#+id/commentEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="100dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:gravity="left"
android:text=""
android:hint="hint"
android:textSize="16dp"
android:textColor="#android:color/black"
android:lineSpacingExtra="0dp"
android:lineSpacingMultiplier="1"
android:background="#android:color/white"
android:selectAllOnFocus="true"
android:inputType="textShortMessage|textMultiLine|textCapSentences"
android:singleLine="false" />
Refere to this to prevent paste function. To prevent pasting a new line.
Please use android:imeOptions="actionSend"
to solve your problem.
I have the same issue a long time ago, you have to programmatically change Edittext property on OnCreate().
So in XML, create your Edittext like this
<EditText
android:id="#+id/comment_box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="80dp"
android:hint="Comment here"
android:maxLength="400"
android:inputType="textMultiLine" />
And on onCreate() (I wrote in kotlin not Java)
comment_box.imeOptions = EditorInfo.IME_ACTION_SEND
comment_box.setRawInputType(InputType.TYPE_CLASS_TEXT)

How to make Android EditText expand vertically when full

I have this EditText
<EditText
android:layout_width="match_parent"
android:layout_height="72dp"
android:hint="#string/write_message"
android:textColorHint="#color/primary_color"
android:ems="10"
android:imeOptions="actionDone"
android:inputType="textImeMultiLine"
android:id="#+id/message_input"
android:layout_gravity="center_horizontal"
android:backgroundTint="#color/primary_color"/>
When the box is filled up with user inputted text, it scrolls to the right to make room for more text, I do not like this behavior, and would prefer it if the EditText box expanded upwards when it needs more room? Is there a way to do this? Thanks.
Yes, this actually involves two things:
Making the EditText accept multi-line input.
Having its height grow as more text lines are added.
Therefore, to achieve this, you need to set up:
android:inputType="textMultiLine"
android:layout_height="wrap_content"
(Be mindful of the difference between textMultiLine and textImeMultiLine).
The full XML snippet would be:
<EditText
android:layout_width="match_parent"
android:inputType="textMultiLine"
android:layout_height="wrap_content"
android:hint="#string/write_message"
android:textColorHint="#color/primary_color"
android:ems="10"
android:imeOptions="actionDone"
android:id="#+id/message_input"
android:layout_gravity="center_horizontal"
android:backgroundTint="#color/primary_color"/>
Use both flags: textMultiLine will wrap your input, and textImeMultiLine will provide a break-line key in your keyboard.
<EditText
...
android:layout_height="wrap_content"
android:inputType="textImeMultiLine|textMultiLine"
... />
In my case I have multiline text, but it showed one line and a keyboard:
Though I have already set android:inputType="textCapSentences|textAutoCorrect|textMultiLine", it didn't help. Then I understood that when the keyboard appears in DialogFragment, it collapses the EditText. See DialogFragment and force to show keyboard to show keyboard when DialogFragment shows.
Then I added a short delay (100-300 ms) before showing the keyboard. Now I have:
In AndroidManifest I set android:windowSoftInputMode="adjustResize" for current activity.

android:nextFocusForward is going to the wrong EditText

I am working on an android project and I am using the android:nextFocusButton so the user can press the next button on the soft keyboard to move through EditText without needing to tap each edit text to change the focus.
In the layout I am asking for the users first name and last name, in separate EditText within a Linear Layout which is in horizontal
Then on the next line, in another linear layout, I then ask for the company name. Below is a screenshot to show what I mean.
What I am trying to do, is in the first name edit text, the user can press next, which should then switch focus to the last name edit text, they then press next again and it moves to the company edit text.
Instead what is happening, is the first name has focus, the user presses the next button, and it goes to the company edit text instead of the last name.
Below is a snippet of my XML layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText android:id="#+id/createAccount_txtFirstName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/first_name"
android:singleLine="true"
android:inputType="textCapSentences"
android:nextFocusForward="#+id/createAccount_txtLastName"/>
<EditText android:id="#+id/createAccount_txtLastName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/last_name"
android:singleLine="true"
android:inputType="textCapSentences"
android:nextFocusForward="#+id/createAccount_txtCompanyName"/>
</LinearLayout>
<EditText android:id="#+id/createAccount_txtCompanyName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/company_name"
android:singleLine="true"
android:inputType="textCapSentences"
android:nextFocusForward="#+id/createAccount_txtEmail"/>
just place this inside your first edittext. Also replace nextFocusForward with nextFocusDown
<EditText android:id="#+id/createAccount_txtFirstName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/first_name"
android:singleLine="true"
android:inputType="textCapSentences"
android:nextFocusDown="#+id/createAccount_txtLastName">
<requestFocus />
</EditText>
It seems to be bugged when running this code on the emulator. However, it works on phones
It might be that your XML is ignored by Android focus algorithm. Documentation says following:
In rare cases, the default algorithm may not match the intended
behavior of the developer. In these situations, you can provide
explicit overrides with the following XML attributes in the layout
file: nextFocusDown, nextFocusLeft, nextFocusRight, and nextFocusUp.
I had similar problem and i solved it by using nextFocusDown.
More on official documentation
None of the solutions worked for me except for listening for editor action listener and programmatically setting the focus to the next EditText.
mFirstName.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
mLastName.requestFocus();
return true;
}
});

EditText automatically to the latest char?

I want create simple calculator for android, and having problem with displaying long equations. Everything is OK (one line, horizontal scrolling), but I don't see end of line (always see start of line). Would it be possible to scroll it automatically to the latest character?
Example:
Hello there
...lo there
--------
-
<EditText
android:id="#+id/etDisplay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="4"
android:clickable="false"
android:cursorVisible="false"
android:ems="10"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="right"
android:textSize="15sp"
android:scrollHorizontally="true"
android:singleLine="true"
android:ellipsize="end"
android:hint="0." />
Image: SimpleCalc image
Try this one:
EditText editText = (EditText) findViewById(R.id.editText1);
editText.setSelection(editText.getText().length());
The "official" way of doing that is (as suggested in the comments), via the ellipsize attribute set to end. If it's not working, it might be due this attribute is a bit tricky, meaning that it has some circumstances to be happening in order to work (like for example, singleLine=true).
You might want to see this, it helped me when I had this issue.

Programmatically scrolling an EditText

I'm writing a simple caesar-encryption-activity. Two EditTexts on screen, one clear-text, one crypted. Here's an example for the crypted EditText - the cleartext one is similar.
<EditText
android:layout_below="#id/Caesar_Label_CryptText"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="#+id/Caesar_Text_CryptText"
android:hint="Enter crypted text"
android:maxLines="2"
android:lines="2"
android:minLines="2"
android:inputType="text|textMultiLine|textVisiblePassword"
android:scrollbars="vertical"
android:gravity="top" />
Now when entering cleartext I have an TextChangedListener running that programatically crypts and fills that crypto-EditText. So far, so good.
When the cleartext entered gets really long, the cleartext-EditText scrolls with my imput, but the crypto-EditText stays at the top of the text. I'd really like the crypto-EditText to scroll so that it always shows the bottom line of its content.
How can that be done, preferably from the onTextChanged()-Method of the TextWatcher?
Ok, found it. It was the cursor (called Selection on EditText and TextViews).
This is how I got it to work:
ivClear // assigned the EditText that has the input
ivCrypt // assigned the target EditText, that I want to scroll
aText // the input from ivClear, crypted
Then use:
ivCrypt.setText(aText); // assign the Text
ivCrypt.setSelection(ivClear.getSelectionStart()); // scroll
Phew, finally :) Always underestimated the power of the Spannable ;)
The base class android.view.View has methods getScrollX(), getScrollY() and scrollTo() that may be helpful, though I haven't tried it.
http://developer.android.com/reference/android/view/View.html#scrollTo(int, int)

Categories

Resources