Kathryn Crawford Kathryn Crawford - 6 months ago 60
Java Question

Android handle soft enter key

I'm building a very simple list app on android where I have an edittext for input, a listview to store saved input, and a button to push text from the edittext to listview. Right now I am trying to handle the error key, so not only does it not create a new line on press, but also so that it will send the text to the listview when I click the enter button.

I set the edittext to singleline to prevent new lines, and I added an onEditorActionListener to my edittext to handle the keyevents. Here's my code.

activity_main.xml

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myEditText"
android:imeOptions="actionSearch|actionGo"
android:hint="Enter a new item"
android:singleLine="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/myImageButton"
android:layout_toStartOf="@+id/myImageButton"/>


MainActivity.java

public class MainActivity extends AppCompatActivity {
// Declare our View variables
private ListView mListView;
private EditText mEditText;
private Button mDoneButton;
private ImageButton mImageButton;
private static final String TAG = MainActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Assign the view from the layout file to the corresponding variables
mListView = (ListView) findViewById(R.id.myListView);
mEditText = (EditText) findViewById(R.id.myEditText);
mDoneButton = (Button) findViewById(R.id.myDoneButton);
mImageButton = (ImageButton) findViewById(R.id.myImageButton);


// Listview items adapter
items = new ArrayList<>();
readItems();
itemsAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items);
mListView.setAdapter(itemsAdapter);

//setup enter listener
mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
//handle search key click
onAddItem(v);
Log.d(TAG, "Handled!");
return true;
}
if (actionId == EditorInfo.IME_ACTION_GO) {
//Handle go key click
onAddItem(v);
return true;
}
return handled;
}
});
}

//Add item to list from either the button or enter
public void onAddItem(View v) {
String itemText = mEditText.getText().toString();
itemsAdapter.add(itemText);
mEditText.setText("");

writeItems();
}


The code giving me trouble. I'm not even getting anything back from that log.d, but everything else in the code, except for this listener, works as expected.

//setup enter listener
mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
//handle search key click
onAddItem(v);
Log.d(TAG, "Handled!");
return true;
}
if (actionId == EditorInfo.IME_ACTION_GO) {
//Handle go key click
onAddItem(v);
return true;
}
return handled;
}
});

Answer

In order to use IME_ACTION_SEARCH and others you need to specify these actions in your xml-layout as well:

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/myEditText"
    android:imeOptions="actionSearch|others"
    ...
    />
Comments