Background
Suppose you have multiple instances of EditText.
You want to be able to switch between them using the next button on the keyboard (which is used as a replacement for the ENTER key).
Each EditText can have long content that can appear in multiple lines (suppose I want to limit it to three lines, and if the text is still too long, use an ellipsis).
Problem
As I noticed, both TextView and EditText have really strange behavior and lack of some basic functions. One of them is that if you want to go to the next view, for each instance of EditText you need to have singleLine = "true".
What i tried
I tried the following xml layout (and other tests), but it does not 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 also tried the following code, but this is a really stupid 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; } });
This works, but it is a stupid solution due to the following reasons:
- I need to set this behavior for each EditText (or extend the EditText and add some logic there).
- It does not show the βnextβ for a soft keyboard key. Instead, it shows the ENTER key.
- Playing with XML did not allow me to set the ellipsis at the end, and I continued to scroll through the behavior.
Question
Is there a better way to achieve this? Elegant, working and showing the "next" key instead of the ENTER key?
source share