2

I am trying to have the user enter text into a edit text. When the user hits the enter key I want to take the text from the edit text and add it to my ArrayList. I am having issues controlling the keyEvent from the edit text.

public class MainActivity extends Activity {
ArrayList<String> array_list;
ListAdapter lv;
ListView list;
EditText edittext;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    array_list = new ArrayList<String>();
    edittext = (EditText) findViewById(R.id.textView);
    lv = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, array_list);
    list = (ListView) findViewById(R.id.lstView);

    list.setAdapter(lv);
    edittext.requestFocus();

    edittext.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            boolean handled = false;
            if (actionId == EditorInfo.IME_ACTION_GO) {
                array_list.add(edittext.getText().toString());
                edittext.setText("");
                edittext.requestFocus();
            }
            return handled;
        }
    });
}
}

So what I need to happen is when the user presses enter, it takes the text from the field and adds it to an arrayList. Then it erases the text from the textfield and gives it focus again.

Caleb Novess
  • 43
  • 1
  • 1
  • 6
  • 1
    Please show your code? It is difficult to guess your issues. – Christopher Apr 26 '17 at 13:59
  • I've seen people use onKeyListeners and textWatchers. I'm just wondering which one would be the easiest to use and how to implement it. – Caleb Novess Apr 26 '17 at 14:03
  • They actually do different things. `OnKeyListener` will listen to certain keyboard keys and act upon then, on the other hand `TextWatcher` just receives the new text and will probably not receive the \n character unless the `EditText` is set as multiline. Regardless, I believe the `OnKeyListener` to be the best approach. – Edson Menegatti Apr 26 '17 at 14:06
  • `I am having issues controlling the keyEvent from the edit text.` that's really not specific enough – njzk2 Apr 26 '17 at 15:29
  • @njzk2 I added code to show what I have written. – Caleb Novess Apr 26 '17 at 15:38
  • @CalebNovess I can see that, but it doesn't really help understand what issues you are facing – njzk2 Apr 26 '17 at 15:43
  • Possible duplicate of [Android - Handle "Enter" in an EditText](http://stackoverflow.com/questions/1489852/android-handle-enter-in-an-edittext). Also, you may want to take a look at this if you need to handle the 'back' button. http://stackoverflow.com/questions/32541009/onbackpressed-with-a-softkeyboard-open – Gary99 Apr 26 '17 at 16:00

1 Answers1

7
editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    boolean handled = false;
    if (actionId == EditorInfo.IME_ACTION_GO) {
        //Perform your Actions here.

    }
    return handled;
   }
});

and in your xml you do something like this:

<EditText
    android:id="@+id/editText2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text|textUri"
    android:imeOptions="actionGo"
    android:imeActionId="666" 
    android:imeActionLabel="Some Label"
    android:maxLines="1"/>

The key to this are the xml lines that contain android:ime...

Gaurav Bharti
  • 1,065
  • 1
  • 14
  • 22
bogdanN
  • 183
  • 10
  • I have implemented both of those into my code and the code is not executing when the enter button is hit – Caleb Novess Apr 26 '17 at 15:21
  • 1
    Apparently you have to specify android:maxLines="1", otherwise it just inputs the new line command. Maybe it's related to the keyboard you use or something, but if you don't need multi line edittext I suggest you to use it this way. I tested and it seems to work on my end. I edited the answer – bogdanN Apr 27 '17 at 11:14