EditText cannot get focus back after set Focusable to false

Viewed 31

I set Focusable of my EditText to false and then set it back to true. But it cannot get the focus back.

public void onClick(View v) {
                if (et.isFocusable()){
                    et.setFocusable(false);
                } else {
                    et.setFocusable(true);
                }
            }
2 Answers

In this case you should reques focus again:

public void onClick(View v) {
   if (et.isFocusable()){
     et.setFocusable(false);
 }else {
     et.setFocusable(true);
     et.requestFocus();

 }
}

When you declare a component is not focusable, the system assigns the focus to the next focusable component as a side effect. Making the component focusable again does not undo that side effect, so you have to make it happen.

Ensure you call the component's requestFocus() method.

Related