0

I have a ListView filled with EditTexts. They are unfocusable on start, but user may enter "edit mode" after long clicking a ListView item - the corresponding ListView. The first weird thing is when the very first long click happens. EditText gets focus, but the keyboard does not appear. EditText needs to be tapped for it to finally appear. When after that the other list item is long clicked, the keyboard appears. The only difference in internal behaviour I managed to observe is that in the first case onClick event doesn't fire to EditText, while in the second case it does.

But the most frustrating thing happens sometime when I edit a couple of items, scroll here and there, edit a couple more and suddenly I end up in a focused EditText without keyboard shown and without any way to bring the keyboard up again in that EditText, tapping doesn't help. I try to be really careful in my apadter and I save EditText state and re-create it in my ListView adapter in getView method.

Any suggestions on how to debug this stuff? Perhaps, someone could explain how the keyboard decides to show up, and how to debug this event if it's possible.

devmiles.com
  • 9,895
  • 5
  • 31
  • 47
  • 3
    I ran into a **lot** of trouble using `EditTexts` inside a `ListView`. The view recycling thing wreaked havoc with the focus and the key listeners. I moved on to using a `ScrollView`, which is not perfect, but works much better. Hopefully you don't have to add thousands of items, then the scrollview approach wouldn't work. – dmon Jan 30 '12 at 17:32
  • I'm already thinking about `ScrollView` because you're totally right - there's havoc with focus and keyboard. I'm not planning to have hundreds of items but I need to read and save my items from/to sqlite db. What is the best way to do that with ScrollView? – devmiles.com Jan 30 '12 at 18:25
  • I just add the required TextViews and EditTexts manually to a LinearLayout inside a ScrollView. It's a bit fancier because I have an Adapter that I cycle through calling getView(), and a model backing it, but it boils down to just calling `addView()`. – dmon Jan 31 '12 at 03:06

1 Answers1

1

I've found a really good explanation of what's going on with requestFocus() calls here: Android Actionbar Tabs and Keyboard Focus In a couple of words: when you call requestFocus() (or it's called due to xml setting in layout) while the whole view tree isn't laid out yet, your view would think that it has focus but in fact it might not. One solution would be calling post(Runnable) on your view or it's parent to request focus after layout is done.

Community
  • 1
  • 1
devmiles.com
  • 9,895
  • 5
  • 31
  • 47