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 ?