TomTaila
TomTaila

Reputation: 1694

Enter key starting a new line in EditText instead of submitting the text (Android)

I have a simple todolist app I'm creating whereby new todo items are added via an EditText View. So I'm trying to have it submit the new item to the list when I press the enter key. For some reason though it just enters a new line instead. I have also noticed that it works fine when I use the enter key via an AVD through eclipse but the problem only occurs when I'm running it through my Nexus 4.

Here is my code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.todo_main);

    //Create reference to the ListView in the main layout
    ListView myListView = (ListView)findViewById(R.id.myListView);

    //Create a reference to the EditText view in the main layout for adding new items
    final EditText editText = (EditText)findViewById(R.id.myEditText);

    //Create a an arraylist to hold all the todo items
    final ArrayList<String> todoList = new ArrayList<String>();

    //Create an ArrayAdaptor object to be able to bind ArrayLists to ListViews
    final ArrayAdapter<String> arrayAdaptor = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoList);
    myListView.setAdapter(arrayAdaptor);

    //Listens for certain keys to be pushed, particular the dpad center key or enter key
    editText.setOnKeyListener(new View.OnKeyListener() {    
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if(event.getAction() == KeyEvent.ACTION_DOWN)
            {
                if((keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_DPAD_CENTER))
                {
                    //add item in the EditText to the todo list 
                    todoList.add(0, editText.getText().toString());
                    //Update the view by notifying the ArrayAdapter of the data changes
                    arrayAdaptor.notifyDataSetChanged();
                    editText.setText("");
                    return true;
                }
                return false;
            }
            return false;
        }
    });
}

And XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ToDoListActivity">

<EditText
    android:id="@+id/myEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/addItemHint"
    android:imeOptions="actionDone"
/>

<ListView
    android:id="@+id/myListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"  
/>

Please note that I have found similar questions on stack overflow but none seem to solve my problem. I've tried!

Upvotes: 9

Views: 19725

Answers (5)

Sebastian Gomes
Sebastian Gomes

Reputation: 930

android:singleLine="true" is depricated, use

android:maxLines="1"
android:inputType="text" 
android:imeOptions="actionNext"

Upvotes: 3

Shehan Sachintha
Shehan Sachintha

Reputation: 159

If you want to single line
The easy way is android:inputType="text"
Or you can use android:MaxLine="1"
If you want to Multiline don't insert those codes.

Upvotes: 0

Blasco73
Blasco73

Reputation: 2990

android:inputType="textImeMultiLine"

In my case this have solved the problem!

Upvotes: 0

Priyankchoudhary
Priyankchoudhary

Reputation: 826

android:inputType="textMultiLine"

adding this property in edittext will solve the problem

Upvotes: 9

MobileMon
MobileMon

Reputation: 8651

Set the singleLine property on your EditText widget to true:

<EditText
    android:singleLine="true"
    android:id="@+id/myEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/addItemHint"
    android:imeOptions="actionDone"
/>

Upvotes: 15

Related Questions