user2620232 user2620232 - 18 days ago 7
Android Question

Grouping EditText

I was wondering if there is a way to group EditText in a way that you can only traverse among a group of them.

Diagram

The problem is that I rely on reaching the end of the first group and use

EditorInfo.IME_ACTION_DONE
in order to trigger a certain action.

The inner group of EditTexts is inside a LinearLayout.

Answer

Put the text boxes in a layout(linear or relative) only those EditText which you want to make a group. layout will act as group. Add this on your every Edittext xml--

 android:maxLines="1"

if it does not works the you can handle focus.

Focus Handling Focus movement is based on an algorithm which finds the nearest neighbor in a given direction. In rare cases, the default algorithm may not match the intended behavior of the developer.

Change default behaviour of directional navigation by using following XML attributes:

android:nextFocusDown="@+id/.."  
android:nextFocusLeft="@+id/.."    
android:nextFocusRight="@+id/.."    
android:nextFocusUp="@+id/.."  

Besides directional navigation you can use tab navigation. For this you need to use

android:nextFocusForward="@+id/.."

To get a particular view to take focus, call

view.requestFocus()

To listen to certain changing focus events use a View.OnFocusChangeListener

Keyboard button

You can use android:imeOptions for handling that extra button on your keyboard.

Additional features you can enable in an IME associated with an editor to improve the integration with your application. The constants here correspond to those defined by imeOptions. The constants of imeOptions includes a variety of actions and flags, see the link above for their values.

Value example

ActionNext :

the action key performs a "next" operation, taking the user to the next field that will accept text. ActionDone :

the action key performs a "done" operation, typically meaning there is nothing more to input and the IME will be closed.

Code example:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="32dp"
    android:layout_marginTop="16dp"
    android:imeOptions="actionNext"
    android:singleLine="true"
    android:ems="10" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText1"
    android:layout_below="@+id/editText1"
    android:layout_marginTop="24dp"
    android:imeOptions="actionDone"
    android:singleLine="true"
    android:ems="10" />

</RelativeLayout>

If you want to listen to imeoptions events use a TextView.OnEditorActionListener.

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            performSearch();
            return true;
        }
        return false;
    }
});

Edit: For dynamic edittext you can try..

EditText etCurrent;

Set an OnTouchlistener on each EditText:

valueET.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_DOWN) {

            if (etCurrent != null) {
                if (etCurrent.getId() != v.getId()) {

                    if (!check(etCurrent.getId())) {
                        etCurrent.setSelection(0,
                             etCurrent.getText().toString().length());
                        return true;
                    } else {
                        etCurrent = (EditText) v;
                    }
                }
            } else {
                etCurrent = (EditText) v;
            }
      }
    return false;
    }
});