I am attempting to add multiple EditTexts to a GridLayout and want to be able to specify which EditText is focused when the user clicks the "Next" button on the keyboard.
I thought this was going to be as simple as setting the setNextFocusDownId(...)
method on the each EditText but unfortunately this doesn't seem to work.
My Layout file simply consists of a GridLayout with an EditText at the bottom:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:grid="http://schemas.android.com/apk/res-auto"
android:id="@+id/Grid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
grid:columnCount="2">
</android.support.v7.widget.GridLayout>
<EditText
android:id="@+id/Last"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Last"/>
</LinearLayout>
And the code in my MainActivity.java file dynamically adds 10 EditTexts and attempts to assign the 'NextFocus' appropriately:
public class MainActivity extends AppCompatActivity
{
final Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridLayout grid = (GridLayout) findViewById(R.id.Grid);
int N=10;
grid.setRowCount((N+1)/2);
EditText prev = null;
for (int i=0 ; i<N ; i++)
{
EditText t = new EditText(context);
t.setInputType(InputType.TYPE_CLASS_TEXT);
t.setMaxLines(1);
t.setHint("" + i);
grid.addView(t);
if (prev != null)
prev.setNextFocusDownId(t.getId());
prev = t;
}
}
}
This does indeed create a grid with two columns of 5 elements however, clicking in the top cell and clicking Next arrow (">") on the keyboard takes you down a row rather then to the expected "NextFocused" ExitText.
Starting in the '0th' cell the "next" button takes you successively through the 'default' sequence: '0' > '2' > '4' > '6' > '8' > "Last" (rather than the expected '0' > '1' > '2' > ... > '9' > "last").
Interestingly though, setting the NextFocused to findViewById(R.id.Last).getId()
(instead of t.getId()
) does cause focus to jump to "Last" from every cell in the grid. Any idea why this isn't working for t.getId()
.
The answer to this question seems to suggest simply setting 'setNextFocusDownId(...)' should do the trick however it's not working for me. Is there something different about accessing ID's of dynamically created View's that is different from XML defined Views that I don't quite understand?
Thanks, Slarti.