Android EditText in RecyclerView - android

I want to implement a search. But unfortunately edit text must be in recycler view. I have multiple types of items.
When user enters a letter I refresh whole adapter, searching is done locally. That means that keyboard disappears and edit text is losing focus, because of this constant updating.
I managed to fix it showing keyboard and focus an edit manually while binding.
if (item.inEditableMode) {
edit_text.requestFocus()
showKeyboard(edit_text)
}
But edit text works not that good as I expect. A problem can be seen when user types fast or wants to clear input.
Thanks for help.
Layout:

"unfortunately edit text must be in recycler view" - I bet it do NOT have to be a part of RecyclerView not part of Adapter list item View. better inspect your layout architecture instead of posted workarounds with hiding-showing (blinking) keyboard
besides that even when it must be then don't notify whole Adapter with notifyDataSetChanged(), instead use notifyItemChanged(...) - notify range of of your items, but not list item with EditText - it won't be redrawn, so keyboard should stay visible and focus kept on that field. still this isn't good approach, your EditText should be separated from RecyclerView almost for shure

Related

Focus between EditText and ListView item

My row in list that has editText in it, beside other things. If I set android:focusableInTouchMode="false" on editText, I am able to select my entire row, highlight it, get data I need, but I no longer can edit my editText, because keyboard doesn't pop up. If I set android:focusableInTouchMode="true", then I can edit editText, but i can't click on entire row, highlight it, get data. How can I achieve both?
ListView isn't adapted for inputs. It keeps in memory only visible items + 2. So even if you will achieve the that what you want, all your edits will dessapear after every time, your EditText leaves the screen.
Take a look at RecyclerView view documentation. This class is better adapted to this kind of stuff.
UPD
Actually, there is a tutorial exactly for foucsable EditTexts in ListView. But as you see, it was relevant for 2011.

EditText inside list view android loses focus and data getting copied into another records

I am working on a custom listView which contains editText in every list item.
My listItem is in fragment, and its activity has already adjustResize property which i can not change to adjustPan. Problem is when I click the edittext it is losing focus and even after two three clicks if it gains focus , then upon scrolling that value gets copied into another records and keyboard gets hanged.
I am using
android:descendantFocusability="beforeDescendants"
on my listView. Also i am using ViewHolder pattern in my adapter . Any tested links or working piece of code is really appreciated.
I had a similar issue dealing with RecyclerView with EditText's in the rows recently so this might help you.
When the keyboard comes up your list resizes and maybe the row that caused the keyboard to come up is not visible any more. You need to scroll to it and give it focus back.
How would you know that the keyboard is up? There is no elegant way, I do it by setting GlobalLayoutListener on the recyclerView, saving it's original size, and listening for a smaller size being reported. Search for "android keyboard listener" and you'll find some code.
When user taps on a EditText save the position of the row.
Listen for global layout changes to figure out that the keyboard is up.
Scroll to the row with the saved position.
Give the EditText focus. (probably in a posted Runnable to wait for the scroll to actually happen)

Which is better way to implement edit - listview with edittext or table layout with edittext

Need to implement edit in runtime. Which one is the best way to achieve it.
Edittext in Listview or dynamic table layout(inflating row xml) with edittext
Update:
My listview contains 7-8 view(Text view) in a list item. On click edit button using view switcher changed textviews to edit text. To get the entered value in edittext listening onfocuschanged. It brings very slow performance. Any better way to achieve it?
Update:
If my listview have 100 list items. Each item having 7-8 edittext. Need to listen all the edittext focuschange. My app hangs. What should i do?
EditText within ListView can cause you great grief down the lane given that views are recycled in listview.
Say you have tapped on the second row in a listview where all item rows contain a edittext and you have set adjustResize in your AndroidManifest.xml; after the soft keyboard pops up, the focus goes into the first view that can accept the focus which in this case will be the first edittext (row) of your listview. Yes, you can tap again on the desired edittext to regain focus. But it is an annoyance nevertheless. If you set adjustPan, then I have seen the the problem does not exist as such; if I recollect correctly, you cannot scroll down all the way to the end of your list. Again, another annoyance.
I'd suggest, you go with a ScrollView if the number of items in the list are less. I have been trying to solve this for the last couple of days - I ended up doing this - I replaced the edittext's with textview's in the listview; tapping on the textview would bring up a dialog fragment that contains the edittext.
Have you looked into the concept of a ViewHolder to keep a reference to the items of the listview? That should solve your multiple focus listener problems.

android where to put code that will execute when ListView is done displaying?

I am building an edit in place listView. That is, the user is looking at a list of TextView items. Then the user touches one, indicating he'd like to edit it. The selected item is now shown as an EditText, in the same ListView as the other TextView items.
After this, the soft keyboard is shown, but the EditText has actually lost focus because of all the redrawing. I've got a handle on the EditText in SimpleCursorAdapter.getView(). But, calling EditText.requestFocus() is futile unless I can be sure the EditText is there on the screen.
In which method of which class will I be able to execute something like, getListView().findItemById(n).requestFocus(); ?
Thank you very much.
You can execute
getInstrumentation().waitForIdleSync()
to wait for all UI events.

EditText.setMovementMethod with LinkMovement Method in ListView - loses ability to "Click" on rows in the list

So, basically I have a custom View that contains a ListView with a custom Adapter that has the ability to read in information from the network (dumbed down HTML) and display it on a row by row basis.
All of this works, but I need the text that is read in to be parsed as HTML which contains links that can be tapped on and launched in browser.
I managed to do this by:
text.setText(Html.fromHtml(textBuffer), TextView.BufferType.SPANNABLE);
text.setMovementMethod(LinkMovementMethod.getInstance());
Works great, any links in the text are displayed as links and can be tapped.
This is a side effect of now making it so the rest of the rows in the ListView cannot be tapped (or tapped and held). Basically, all the rows are non-selectable now.
Is there any way to achieve both of what I need?
thanks!
Try setting text.setFocusable(false). By removing focus from the EditText, events will be passed onto the ListView or other items.
When you put an item that is focusable inside a ListView, it automatically makes the ListView not focusable. This is the behavior you are seeing.
To make them both focusable, I believe you can adjust the descendantFocusability property of the ListView.
See this question for a similar problem.

Categories

Resources