6

Background

Suppose you have multiple EditText instances.

You wish to be able to switch between them using the next button of the keyboard (that is used as a replacement of the ENTER key).

Each EditText might have a long content that can be shown in multiple lines (suppose I wish to limit it to 3 lines, and if the text is still too long, use ellipsize).

The Problem

As I've noticed, both TextView and EditText have really weird behaviors and lack on some basic features. One of them is that if you wish to go to the next view, you need to have a singleLine="true" for each of the EditText instances.

What I've tried

I've tried the next xml layout (and other trials), but it doesn't work :

<LinearLayout 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" android:orientation="vertical"
  android:gravity="center" tools:context=".MainActivity">

  <EditText android:id="@+id/editText1" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:nextFocusDown="@+id/editText2"
    android:singleLine="false" android:imeOptions="actionNext"
    android:maxLines="3" android:ellipsize="end"
    android:text="@string/very_long_text" />

  <EditText android:id="@+id/editText2" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:nextFocusDown="@+id/editText3"
    android:singleLine="false" android:imeOptions="actionNext"
    android:maxLines="3" android:ellipsize="end"
    android:text="@string/very_long_text" />

  <EditText android:id="@+id/editText3" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:singleLine="false"
    android:maxLines="3" android:ellipsize="end"
    android:text="@string/very_long_text"/>

</LinearLayout>

I've also tried the next code, but it's really a silly solution:

...
final EditText editText=(EditText)findViewById(R.id.editText1);
editText.setOnEditorActionListener(new OnEditorActionListener()
  {
    @Override
    public boolean onEditorAction(final TextView v,final int actionId,final KeyEvent event)
      {
      if(actionId==EditorInfo.IME_NULL)
        {
        final View view=findViewById(editText.getNextFocusDownId());
        if(view!=null)
          {
          view.requestFocus();
          return true;
          }
        }
      return false;
      }
  });

It works, but it's a silly solution because of the next reasons:

  • I need to set this behavior per each EditText (or extend EditText and add some kind of logic there).
  • It doesn't show "next" for the key in the soft keyboard. Instead it shows the ENTER key.
  • Playing with the XML didn't allow me to set the ellipsize at the end and i kept getting a scrolling behavior.

The Question

Is there a better way to achieve this? One that is elegant, works and shows the "next" key instead of the ENTER key ?

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • Did you manage to solve this issue ? I am also facing the same issue. Please share your solution if possible. – h4ck3d May 30 '14 at 10:37
  • @h4ck3d i forgot about this.looking at the old code I made, it seems I just used android:singleLine="true" . Only place I see that I have "actionNext" without singleLine is here: https://github.com/AndroidDeveloperLB/ChipsLibrary , but most of the project is based on Google's library, so I have no idea how (and even if) this feature works there. I don't have the time to dig it, but please, if you understand what is going on there and find it helpful for answering this question, post about it... – android developer May 30 '14 at 11:58

2 Answers2

0

For an answer here- you can set them both if you want. However forcing it to actually display next and have the enter key act as a next key depends on the keyboard. I know at Swype we purposely overrode any text field that was multi-line to ALWAYS display the enter key and act as a newline and NEVER show the next key. There's no way to force this to work like you want across all keyboards.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
0

I find this question similar to the Multi-line EditText with Done action button post. Since I have already written an answer for that, let me put it in again. Though, I will be able to answer only your first 2 points, for I have yet to use Ellipsize (I think it should be relatively easy once you are set up with the next button part).

The following Java Code can be used to change the Keyboard Enter to "DONE"/"NEXT" button:

////////////Code to Hide SoftKeyboard on Enter (DONE) Press///////////////
editText1.setRawInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD|InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
editText1.setImeActionLabel("DONE",EditorInfo.IME_ACTION_DONE);              //Set Return Carriage as "DONE"
editText1.setImeOptions(EditorInfo.IME_ACTION_DONE);

editText1.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) 
    {
                if (event == null) {
                    if (actionId == EditorInfo.IME_ACTION_DONE) {
                        // Capture soft enters in a singleLine EditText that is the last EditText
                        // This one is useful for the new list case, when there are no existing ListItems
                        editText1.clearFocus();

                        editText2.requestFocus();       //Call the next Edit_Text into Focus  
                        //Comment above requestFocus() for edit_text3

                        //Hiding SoftKeyboard. Uncomment it for edit_text3
                        //InputMethodManager inputMethodManager = (InputMethodManager)  getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
                        //inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);

                    }

                    else if (actionId == EditorInfo.IME_ACTION_NEXT) {
                        // Capture soft enters in other singleLine EditTexts
                    } else if (actionId == EditorInfo.IME_ACTION_GO) {
                    } else {
                        // Let the system handle all other null KeyEvents
                        return false;
                    }
                } 
        else if (actionId == EditorInfo.IME_NULL) {
                    // Capture most soft enters in multi-line EditTexts and all hard enters;
                    // They supply a zero actionId and a valid keyEvent rather than
                    // a non-zero actionId and a null event like the previous cases.
                    if (event.getAction() == KeyEvent.ACTION_DOWN) {
                        // We capture the event when the key is first pressed.
                    } else {
                        // We consume the event when the key is released.
                        return true;
                    }
                } 
        else {
                    // We let the system handle it when the listener is triggered by something that
                    // wasn't an enter.
                    return false;
                }
                return true;
        }
});
Community
  • 1
  • 1
Kaushik NP
  • 6,733
  • 9
  • 31
  • 60
  • Upon pressing the next/done/enter button on the first EditText, the keyboard dissappears – android developer Feb 15 '17 at 08:06
  • Oh sorry. Forgot to comment the Hiding Softkeyboard function (had kept it for you to use with edit_text3). Have edited, should work perfectly now. – Kaushik NP Feb 15 '17 at 12:21
  • Seems to work, but you should use IME_ACTION_NEXT instead of IME_ACTION_DONE, because the "next" is used when going to another field, while "done" is used when you are done with editing. Also, I think it can be generalized more, to work on all of the EditTexts, by getting the next view to focus on (maybe use getNextFocusRightId and getNextFocusDownId, if there are no other functions that can do it ). But, since it now works well, I accept this answer . Thank you :) – android developer Feb 16 '17 at 11:35
  • Ofcourse, it needs a bit of modification, since I mainly wrote it for DONE case, but happy to hear you got through it. Glad to help as always. :D – Kaushik NP Feb 16 '17 at 14:32
  • Do you know perhaps how to get the next focused view ? Meaning the one that pressing NEXT should go to, if all was working by the default rules? – android developer Feb 16 '17 at 16:17