0

I have 8 edittext fields. Each field is for 1 number (It's a phone number input screen). After 1 number is put in, I want my cursor to automatically move to the next edittext.

But there is a delay in the input, making the user experience not smooth. I tried both:

onEditorAction(EditorInfo.IME_ACTION_NEXT)

and:

secondNumber?.requestFocus()

But no difference. This is a very common screen, so there should be a simple solution to this I assume? I'm not allowed to use external libraries.

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View {

    _binding = FragmentFirstBinding.inflate(inflater, container, false)

    val views = arrayListOf(
        _binding?.firstNumber,
        _binding?.secondNumber,
        _binding?.thirdNumber,
        _binding?.fourthNumber,
        _binding?.fifthNumber,
        _binding?.sixthNumber,
        _binding?.seventhNumber,
        _binding?.eighthNumber
    )
    views.first()?.requestFocus()

    _binding?.firstNumber?.addTextChangedListener {
        _binding?.firstNumber?.onEditorAction(EditorInfo.IME_ACTION_NEXT)
        //_binding?.secondNumber?.requestFocus()
    }

    _binding?.secondNumber?.addTextChangedListener {
        _binding?.secondNumber?.onEditorAction(EditorInfo.IME_ACTION_NEXT)
        //_binding?.thirdNumber?.requestFocus()
    }

    _binding?.thirdNumber?.addTextChangedListener {
        _binding?.fourthNumber?.requestFocus()
    }

    _binding?.fourthNumber?.addTextChangedListener {
        _binding?.fifthNumber?.requestFocus()
    }

    _binding?.fifthNumber?.addTextChangedListener {
        _binding?.sixthNumber?.requestFocus()
    }

    _binding?.sixthNumber?.addTextChangedListener {
        _binding?.seventhNumber?.requestFocus()
    }

    _binding?.seventhNumber?.addTextChangedListener {
        _binding?.eighthNumber?.requestFocus()
    }

    _binding?.eighthNumber?.addTextChangedListener {
        Toast.makeText(context, "Reached last digit", Toast.LENGTH_SHORT).show()
    }

    return _binding?.root!!
}

My edittext:

    <EditText
        android:id="@+id/first_number"
        android:layout_width="30dp"
        android:layout_height="40dp"
        android:layout_margin="8dp"
        android:focusable="true"
        android:imeOptions="actionNext"
        android:inputType="number"
        android:maxLength="1"
        android:maxLines="1"
        android:singleLine="true"
        android:textColor="@color/black" />
Jim Clermonts
  • 1,694
  • 8
  • 39
  • 94
  • this seems to duplicate question, [check here](https://stackoverflow.com/questions/12470067/how-to-automatically-move-to-the-next-edit-text-in-android) please – M.K Oct 23 '21 at 21:02
  • no it's a different problem, the provided question has the same lag. But it's easier to experience on older devices like Samsung S8, so I've updated the title of this thread. – Jim Clermonts Oct 23 '21 at 21:38

0 Answers0