Can I switch to another EditText when a specific character is pressed on keyboard?

Viewed 96

I have 3 EditTexts in my app. I want to make it so that if the user is typing in EditText1 and presses space, he will be 'sent' to the second EditText, and the same for going from EditText2 to EditText3. However, when a space is given in EditTexts, it should simply not appear.

Is this possible? How can I do it?

3 Answers

Yes sure you can use TextWatcher

Java version

et1.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        if (s != null && s.endsWith("Your_special_char")){
          yourDesiredEditText.requestFocus();
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

    @Override
    public void afterTextChanged(Editable s) {}
});

Kotlin version

et1.addTextChangedListener(object : TextWatcher {
  override fun afterTextChanged(p0: Editable?) {
  }

  override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
  }

  override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
      if (p0?.endsWith("Your_special_char") == true){//it will eliminate nullability (trick)
          yourDesiredEditText.requestFocus()
        }
  }
})

EDIT

the only issue is I have with this is you end up with a ton of code in each event.

Ok I will provide a clean and neat solution using Kotlin extension functions

fun EditText.forwardFocusOnSpecialCharEntered(targetEditText: EditText) {
    this.addTextChangedListener(object : TextWatcher {
        override fun afterTextChanged(p0: Editable?) {}
        override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}

        override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            if (p0?.endsWith(" ") == true) targetEditText.requestFocus()
        }
    })
}

Usage

firstEditText.forwardFocusOnSpecialCharEntered(secondEditText)
secondEditText.forwardFocusOnSpecialCharEntered(thirdEditText)
thirdEditText.forwardFocusOnSpecialCharEntered(firstEditText)
 

Now using one line of code you can achieve your goal as well as your code stays clean and readable

You should add this TextWatcher to your first and second EditText with the first one doing requestFocus() on the second one and the second one doing requestFocus() on the third one.

Java:

editText1.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

    @Override
    public void afterTextChanged(Editable s) {}

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (s.endsWith(" ")) {
            editText2.requestFocus();
        }
    }
});

Kotlin:

editText1.addTextChangedListener(object : TextWatcher() {
    override afterTextChanged(s: Editable?) {}
    
    override beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
    
    override onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {     
        if (s?.endsWith(" ") ?: false) {
            editText2.requestFocus()
        }      
    } 
});

Sure. You need to create a TextWatcher on each of the textboxes and simply monitor the keypresses. When the appropriate keypress (a space) is sent, .requestFocus() on the next one.

I don't have time to throw together a sample for you, so i'll just link to one. https://android--code.blogspot.com/2015/08/android-edittext-textchangedlistener.html

There is a lot of boilerplate code to a textwatcher but you don't need to put code in all of the functions, just one of them.

Related