2

I have the following issue. I have an edit text where the user puts in the movie name. If the user presses enter I want the focus to go to the next edittext, where the user types the description of the movie. I do not want the user to be able to put a movie title with two lines, ie I want it all on one line without enter. However when I use android:singleLine="true", or android:maxLines, the movie title is printed on one line, but if the movie title is long it will not be all displayed at once (have to scroll across). I want the edittext to grow in height to numerous lines if necessary, but I don't want the user to use enter to make new lines. How do I do this. Here is my code:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Movie title"
    android:textSize="15sp"/>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="15sp"
    android:id="@+id/et_AddMovieTitle"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Movie description"
    android:textSize="15sp"/>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="15sp"
    android:id="@+id/et_AddMovieDescription"

    />

Thank you

6 Answers6

1

Maybe and idea would be to create your custom editText, you could override the onKeyDown method and check if the user has pressed enter, if so you could hide the keyboard or something.

class CustomEditTexxt extends EditText
{
    ...
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (keyCode==KeyEvent.KEYCODE_ENTER) 
        {
            // hide the keyboard
            return true;
        }
        // handle all other keys in the default way
        return super.onKeyDown(keyCode, event);
    }
}
cylon
  • 735
  • 1
  • 11
  • 26
  • This worked well for me, however a interesting thing happened. Even though the enter key on my keyboard was disabled, but on the emulator on the virtual keyboard the enter button still returned a new line. Why is this? Would it work on a device (not an emulator)? –  Dec 23 '15 at 07:25
1

use

android:inputType="textMultiline"

& remove

android:imeOptions="actionDone"

Ahamadullah Saikat
  • 4,437
  • 42
  • 39
0

try it.
Add input type in the edit text.

for Single line

android:inputType="text"

for multiline

android:inputType="textMultiline"
Deepak Goyal
  • 4,747
  • 2
  • 21
  • 46
  • Tried this. Doesn't work. Just scolls along and doesn't display it all at once. TextMultiline, allows the user to press enter and go to a new line. I don't want that –  Dec 22 '15 at 18:54
0

I think you will have to override the enter key functionality in order to prevent the user from using "Enter" to add a line in an EditText. You should be able to simply change the focus to the next EditText here and override the new line logic.

I think this post might be of value to you. Handling "Enter" key in an EditText

Community
  • 1
  • 1
sargturner
  • 540
  • 2
  • 18
0

In the past, I've used the android:imeOptions="actionNext" to force the user to the next input field.

You can check the action docs to verify imeOptions is what you need. You may also want to take a look at using the actionDone option for easily submitting the form when the last field is selected.

So a quick/cheap way is to setup as a single-lined EditText with enabled "Next" button:

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="15sp"
    android:imeOptions="actionNext"
    android:singleLine="true"
    android:id="@+id/et_AddMovieTitle"
    />

And then force the EditText to do soft-wrapping, by applying the following programmatic configuration:

editText.setHorizontallyScrolling(false);
editText.setMaxLines(Integer.MAX_VALUE);

This worked for me testing on android 5.1, but is not really conducive for re-use. So if your needs are more demanding, you can also consider subclassing EditText

Community
  • 1
  • 1
Brad
  • 513
  • 3
  • 8
  • To make sure I understand your problem, you want to: (1) override the "enter" key to move the user to the next available input, (2) don't allow the user to create a new line within the `EditText`, (3) format the `EditText` to properly wrap lines when applicable? – Brad Dec 22 '15 at 19:46
  • Correct, this takes me to the next editview and the user cannot press enter. However I still have the problem that the edittext remains on one line, and cannot be viewed all at the same time without scrolling across. –  Dec 22 '15 at 19:52
  • Edited answer after doing some more research and validating locally. It's definitely a hack, though... – Brad Dec 22 '15 at 20:41
0

I had the same problem as you, and setting only textMultiLine will not work, add android:scrollbars="vertical" and you want to define a android:maxHeight="max_size_in_dp" if it overflow the size user must scroll.

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enviar seu comentário"
    android:scrollbars="vertical"
    android:maxHeight="100dp"
    android:inputType="textCapSentences|textMultiLine"/>

editText.setText("big_text_description_here");

The text will be displayed as you want.

diogojme
  • 2,309
  • 1
  • 19
  • 25
  • I do not want the user to be able to press the enter button. Does you picture show this? –  Dec 22 '15 at 19:51
  • @RBerman The image was a little confusing, i edited my answer, try it now and let me know if it help you. – diogojme Dec 22 '15 at 20:15
  • The user can still press the enter button to get to a new line. –  Dec 22 '15 at 20:24
  • @RBerman You can't disable or remove this buttom from keyboard check this answer (http://stackoverflow.com/a/10564062/1879661) , until you `disable it programatically` for do that, check this answer (http://stackoverflow.com/a/6991402/1879661) and combine it with my answer. – diogojme Dec 22 '15 at 21:28