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.
Related
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
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)
How can i create an android listview with row 1: textview + spinner row 2: textview + edittext etc.?
I have to make a form and i make it with xml layout but it's too long and bad for perfomance as the eclipse says.
Thank you!
ListView can be used if there is a repetition of same views in every row. This is where you get performance benefits given by adapters' recycling view mechanisms. With every row having different views they cannot be recycled and therefore no point in using the listview there.
So based on what kind of rows you have here are two solutions:
Solution 1:
Row 1 has textview + spinner, Row 2 has textview + EditText. And this combination repeats for the rest of the rows then you can try this. The xml where you create the layout of individual row for the ListView should be made a LinearLayout having two rows. ListView will create the rest of the rows for you by appending this LinearLayout.
Solution 2:
If each row is completely different from other (in terms of views used) then I would suggest using pagination. Divide your form questions into different relevant groups put each group on one page (scrollview). When user answers a set they have to navigate to the next page and so on.
As suggested by #Xaver Kapeller, I'd also suggest not using EditText in a ListView. It is very painful to debug keyboard issues and when the device orientation changes with keyboard open then as well. Instead of EditText ,use TextView which on tapping opens a Dialog box with a EditText. To keep the user interaction minimum.
You can focus the EditText as soon as the dialog open so that keyboard opens and user does not have to tap on the EditText. You can also dismiss the dialog by pressing the imeAction like Done on the soft keyboard or by pressing the hard back button. Dismissing the dialog should trigger a textview.setText("Data entered in the dialog box").
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.
I have a GridView to which a custom adapter is set. getView() method of the custom adapter returns a LinearLayout with two TextView and an EditText arranged horizontally. I have made a custom numeric keyboard for entering text in the EditText. Keyboard contains NEXT and PREV buttons as well which are creating problems. I want NEXT button to automatically focus the next EditText in the next row and similarly PREV button. NEXT button onKeyPress seems like:
View v=getWindow().getCurrentFocus().focusSearch(View.FOCUS_FORWARD);
if(v!=null)
v.requestFocus();
The code seems right. The problem is, suppose currently only three rows are visible of gridView,if the focus is on the third EditText and NEXT is pressed,it then focuses on nothing. I dont know how to solve this issue. If anyone knows how to solve it.
Thanx in advance.
Can you maintain a collection of ids of all the editText's in the same order, while adding them for rendering.
In that case, for clickHandler or NEXT/PREV button, u can check following:
clickHandler of PREV: if present selection is first in list, then you might want to set focus on last element in list.
clickHandler of NEXT: if present selection is last in list, then you might want to set focus on first element in list.
Alternatively, if you do not want a list roll back functionality, you can just check the position of element in list, and handle the brink elements any way u want them to be.
Hope I understood your problem correctly.
HTH