Riddhish.Chaudhari
Riddhish.Chaudhari

Reputation: 853

EditText stop loosing focus on enter key

When I press Enter Key EditText Loosing focus

and Listview's first item highlighted BUT no event occurred
1) No ListView focus change found
2) No ItemCLick event occurred AND also on scroll ListView Highlighted item disappeared

How I can prevent losing focus of EditText on Enter Key Press and stop highlighting

below is my xml and Java code

<EditText
android:id="@+id/et_scanitemcode"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="60"
android:background="@drawable/edittext_background"
android:gravity="right|center_vertical"
android:maxLines="1"
android:paddingLeft="5dp"
android:textColor="@drawable/edittext_color_background" />


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

et_scanitemcode.setOnKeyListener(new EditText.OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                Log.i("==>",""+KeyEvent.keyCodeToString(event.getAction()));
                Log.i("==>",""+keyCode);
                // If the event is a key-down event on the "enter" button
                //This is the filter


                if (event.getAction()==KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
                    // Perform action on key press
                    String scanItemCode = et_scanitemcode.getText().toString().trim();


                    return true;
                }
                return false;
            }
        });

Upvotes: 1

Views: 4370

Answers (3)

Erik Meisel
Erik Meisel

Reputation: 21

You can set the next item that will has focus on enter and down key with setNextFocusDownId with the same EditText view id (getId()).

In this example barcode is the EditText.

barcode.setNextFocusDownId(barcode.getId());

That works for me

Upvotes: 2

Cristian Marin
Cristian Marin

Reputation: 51

What happens is that after ACTION_DOWN, the KeyEvent.ACTION_UP is triggered causing the control to loose focus. You have to take care of both events.

This worked for me

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_ENTER) {
        if (event.getAction() == KeyEvent.ACTION_DOWN){
            //your code here
        }
        return true;
    }
    return false;
}

Upvotes: 5

Suraj Nair
Suraj Nair

Reputation: 1837

The problem is when you press enter the focus moved to list view, so you can prevent this by setting focus of list view to FALSE on pressing enter.

listView.setFocusable(false);

so your code looks like this

            if (event.getAction()==KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
                // Perform action on key press
                String scanItemCode = et_scanitemcode.getText().toString().trim();

                listView.setFocusable(false);
                return true;
            }

Upvotes: 2

Related Questions