ArtKorchagin
ArtKorchagin

Reputation: 4853

KeyCode_Enter to next EditText in ListView

I have multiline ListView with a few EditText.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/edit_tail"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="number" />

    <EditText
        android:id="@+id/edit_cost"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="number" />

   <EditText
        android:id="@+id/edit_rest"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="number" />

</LinearLayout>

In EditText, after typing 'Enter' key, system establishes focus as follows:

[EditText]  [EditText]  [EditText]
     ↓           ↓          ↓
[EditText]  [EditText]  [EditText]
     ↓           ↓          ↓
[EditText]  [EditText]  [EditText]
     ↓           ↓          ↓
    ...         ...        ...

But I want:

     [EditText] -> [EditText] -> [EditText] ->

   -> [EditText] -> [EditText] -> [EditText] ->

  ->  [EditText] -> [EditText] -> [EditText]

How solve this issue?

Upvotes: 1

Views: 1723

Answers (3)

Maksym Kalin
Maksym Kalin

Reputation: 1713

To provide focusing from left to right and from top to bottom for EditText in ListView, you can use following XML options:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/edit_1"
        android:nextFocusRight="@+id/edit_2"
        android:imeOptions="actionNext"
        />

    <EditText
        android:id="@+id/edit_2"
        android:nextFocusRight="@+id/edit_3"
        android:imeOptions="actionNext"
        />

   <EditText
        android:id="@+id/edit_3"
        android:nextFocusRight="@+id/edit_4"
        android:imeOptions="actionNext"
        />

   <EditText
        android:id="@+id/edit_4"
        android:nextFocusDown="@+id/edit_1"
        android:imeOptions="actionNext"
        />

</LinearLayout>

It works for me and hope it helps you.

Upvotes: 0

Illotus
Illotus

Reputation: 285

I just had the same problem of moving focus horizontally in EditTexts. In my case I just had a few EditTexts so I added android:imeOptions="actionNext" to each EditTexts xml and then added OnEditorActionListener to each EditText. Code sample:

_heightEdit.setOnEditorActionLister(getGotoNextListener(_weightEdit));
private OnEditorActionListener getGotoNextListener(final EditText next) {
        return new OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_NEXT) {
                    next.requestFocus();
                    return true;
                }
                return false;
            }
        };
    }

This is reasonably nice solution to the problem.

Upvotes: 1

Alex Paino
Alex Paino

Reputation: 250

One way would be to set the ime title to "Next" manually (can be done in xml or Java) and then manually override the onClick listener for that activity and listen for the "next" key being pressed. Then you could just call requestFocus() on the EditText that you are wanting to switch the focus to. Honestly though this would be quite a hassle but I know it would work. Hopefully theres a better way.

Upvotes: 1

Related Questions