user1401232
user1401232

Reputation: 61

Dynamically added EditTexts focus goes from first to last when keyboard next button is clicked

I am adding edittext dynamically in a linear layout. when I start editing the first edit text and then press next button on the keyboard, the focus goes to the last one rather than going to the second one. Here is my Activity code..

LinearLayout layout =(LinearLayout) findViewById(R.id.activity_main);
for (int i = 0; i < 3; i++) {
       final View childView = this.getLayoutInflater().inflate(R.layout.tabitem, null);
       TextView questionText = (TextView) childView.findViewById(R.id.questionId);
       EditText editText = (EditText) childView.findViewById(R.id.enterAnswerTextId);

       questionText.setText("Whats up");
       layout.addView(childView);
       childView.setTag(i);
    }

Activity is just a Linear Layout. And the tab item layout which I am inflating

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">


<LinearLayout
    android:layout_width="match_parent"
    android:id="@+id/separatorId"
    android:orientation="horizontal"
    android:layout_height="1dp"
    />

<TextView
    android:layout_width="wrap_content"
    android:id="@+id/questionId"
    android:layout_height="wrap_content"
    android:paddingLeft="20dp"
    android:paddingTop="20dp"
    android:paddingBottom="15dp"
    />

<RelativeLayout
    android:id="@+id/enterAnswerLayoutId"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >

    <EditText
        android:id="@+id/enterAnswerTextId"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="20dp"
        android:layout_marginLeft="20dp"
        android:maxLines="1"
        android:layout_centerHorizontal="true"
        android:inputType="textPassword"
        android:hint="Enter Answer"
        android:padding="20dp"
        android:imeOptions="actionNext|actionDone"/>

</RelativeLayout>

Upvotes: 0

Views: 944

Answers (2)

user1401232
user1401232

Reputation: 61

Solved it by doing this

editText.setImeOptions(EditorInfo.IME_ACTION_NEXT);

Upvotes: 1

Elias Fazel
Elias Fazel

Reputation: 2123

you should define where to go for next focus

  editText.setNextFocusForwardId(R.id.secondView);

also you can set id in this way

view.setId(/*some unique integer as id*/)

Upvotes: 0

Related Questions