0

I'm new to android studio. I was a bit confused with the following code.

EditText emailEt;
if(!Patterns.EMAIL_ADDRESS.matcher(email).matches()){
    emailEt.setError("Invalid email address");
    emailEt.setFocusable(true);
}

What is the purpose of this setFocusable() method? what will happen if the boolean value passed was set to false?

Debargha Roy
  • 2,320
  • 1
  • 15
  • 34
  • Call this to try to give focus to a specific view or to one of its descendants. A view will not actually take focus if it is not focusable (isFocusable() returns false), or if it can't be focused due to other conditions (not focusable in touch mode (isFocusableInTouchMode()) while the device is in touch mode, not visible, not enabled, or has no size). See also focusSearch(int), which is what you call to say that you have focus, and you want your parent to look for the next one. This is equivalent to calling requestFocus(int, android.graphics.Rect) with arguments FOCUS_DOWN and null. – Mangal Sep 05 '20 at 18:25

2 Answers2

0

I don't think it is much useful but it helps in request focus to next edit text when the next button is pressed android keyboard

enter image description here

0

If you're not sure of what focusable is, here's a brief explanation.

If there's a form in a view, there can be 2 cases -

  1. The keyboard comes up automatically, or when you hit the next/enter key on the keyboard the next input field is selected automatically for receiving the input
  2. You need to manually press an input field to bring the keyboard for typing. And on hitting enter, the keyboard might just go away.

The above phenomenon is called Focusing.

Getting back to question, if you want the case (1) to occur, then you set setFocusable() to true, else if you want case (2) to occur, you can set it to false.

Reference the below docs for more info

Dharman
  • 30,962
  • 25
  • 85
  • 135
Debargha Roy
  • 2,320
  • 1
  • 15
  • 34