Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditText: how to enable/disable input?

I have a 7x6 grid of EditText views. I want all of them disabled when the application starts, ie they should behave like normal TextViews and not to be editable. Then the user taps one cell in the grid, it changes its background and performs something visual. If the user clicks on the cell one more time it should allow editing. I'm struggling with OnClick() and OnFocusChange() listeners, but I can't accomplish such a basic interaction.

Playing with setEnabled() and setFocusable() doesn't help. I wonder why even a simple task like this has been made so difficult on Android

like image 292
Raffaele Avatar asked Oct 07 '11 09:10

Raffaele


1 Answers

I finally found a solution. It's a matter of calling

  • setFocusableInTouchMode(boolean)
  • setFocusable(boolean)

when the EditText is first created, so it can intercept the clicks. Then one can set those flags back again to make the EditText editable, request the focus, and manually show/hide the soft keyboard with InputMethodManager methods

like image 123
Raffaele Avatar answered Oct 21 '22 14:10

Raffaele