0

I want the focus to change like in the picture after clicking enter. Then i click enter from left one focus changes as it should, but when i click enter from right one focus just going down and down.I have no idea idea how to get editTexts from the next row. How can i change that ?

My ListView in xml:

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:dividerHeight="0dp"
    android:divider="@null"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/textNamesOfTeams"
    android:layout_above="@+id/start"
    android:descendantFocusability="afterDescendants"/>

And my CustomAdapter:

 class CustomAdapter extends BaseAdapter {
    private Context context;
    private String[] rValues = {"R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9", "R10", "R11", "R12", "R13", "R14", "R15", "R16", "R17", "R18", "R19", "R20", "R21", "R22", "R23", "R24"};

    CustomAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getViewTypeCount() {
        return getCount();
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getCount() {
        return quantityOfTeam;
    }

    @Override
    public String getItem(int position) {
        return names[position] +"\n" + names[position + quantityOfTeam];
    }


    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

        if (convertView == null) {
            holder = new ViewHolder();
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.brazylian_item_list, null, true);

            names = new String[quantityOfTeam * 2];
            holder.editText = convertView.findViewById(R.id.brazItemListEditTextName1);
            holder.editText2 = convertView.findViewById(R.id.brazItemListEditTextName2);
            holder.textView = convertView.findViewById(R.id.brazItemListtextViewR);



            convertView.setTag(holder);
        } else {
            // the getTag returns the viewHolder object set as a tag to the view
            holder = (ViewHolder) convertView.getTag();
        }

        holder.textView.setText(rValues[position]);



        holder.editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (!b) {
                    names[position] = holder.editText.getText().toString();

                }
            }
        });

        holder.editText2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (!b) {
                    names[position + quantityOfTeam] = holder.editText2.getText().toString();
                }
            }
        });

        return convertView;
    }

    private class ViewHolder {

        protected EditText editText;
        protected EditText editText2;
        protected TextView textView;

    }

}

Item of the List:

<TextView
    android:id="@+id/brazItemListtextViewR"
    android:layout_width="40dp"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    />

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:weightSum="2"
    android:orientation="horizontal"
    android:layout_toEndOf="@+id/brazItemListtextViewR"
    >
    <EditText
        android:id="@+id/brazItemListEditTextName1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textSize="15sp"
        android:background="@drawable/rounded_edittext"
        android:layout_marginTop="5dp"
        android:maxLength="15"
        android:singleLine="true"
        android:nextFocusDown="@+id/brazItemListEditTextName2"/>

    <EditText
        android:id="@+id/brazItemListEditTextName2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textSize="15sp"
        android:background="@drawable/rounded_edittext"
        android:layout_marginTop="5dp"
        android:maxLength="15"
        android:singleLine="true"
        />
</LinearLayout>

Move to another EditText when Soft Keyboard Next is clicked on Android I tired in this way but as i said before i can change focus then i click enter from left one to right. I can't found id to EditTexts from next row.

toja6755
  • 23
  • 7
  • Possible duplicate of [Move to another EditText when Soft Keyboard Next is clicked on Android](https://stackoverflow.com/questions/17989733/move-to-another-edittext-when-soft-keyboard-next-is-clicked-on-android) – shriakhilc Oct 13 '18 at 20:58
  • I tried this way but it doesn't work in this case :( – toja6755 Oct 13 '18 at 21:13

2 Answers2

1

use this code in your last Editextview of the row. This is working for me

android:imeOptions="actionNext"
android:nextFocusDown="@+id/nextRowEdittextId"
Praful C
  • 162
  • 1
  • 3
  • 14
0

Kotlin.

In the ListViewAdapter class, fun getView(), add lastEditText.setOnKeyListner as below:

    lastEditText.setOnKeyListener { v, keyCode, event ->
    var setOnKeyListener = false
    if (keyCode == KeyEvent.KEYCODE_ENTER && event.action == KeyEvent.ACTION_UP) {
        try {
            val nextRow = getViewByPosition(position + 1, parent as ListView) as LinearLayout
            val nextET = nextRow.findViewById(R.id.firstEditText) as EditText
            nextET.isFocusableInTouchMode = true
            nextET.requestFocus()
        } catch (e: Exception) {
            // do nothing
        }
        setOnKeyListener = true
    }
    setOnKeyListener
}

add the fun getViewByPosition() after fun getView() as below:

    private fun getViewByPosition(pos: Int, listView: ListView): View? {
    val firstListItemPosition: Int = listView.firstVisiblePosition
    val lastListItemPosition: Int = firstListItemPosition + listView.childCount - 1
    return if (pos < firstListItemPosition || pos > lastListItemPosition) {
        listView.adapter.getView(pos, null, listView)
    } else {
        val childIndex = pos + listView.headerViewsCount - firstListItemPosition
        listView.getChildAt(childIndex)
    }
}
David Chai
  • 11
  • 3