Android - Losing focus on Item in ListView - android

I have ListView with extendable items. Everything is working great until I trigger the dialog popup with EditText. Right after it animation just stop working. If I tap other button to animate the ListView container, everything coming back to normal.
In my ListViewAdapter I got item view in extends FrameLayout class and Animation is trigger when is called override onMeasure method with is not trigger after Soft Keyboard appear.
I think this is causing because there is no focus on particular item on the list?
Off course regular click on the list item is working fine.
Strange is also this on that when I hide soft keyboard by pressing back button everything also working as it should be.
Thank you for any suggestions.

When you trigger the dialog popup with EditText, save the focus of the list view:
int focusedIndex = lstView.SelectedItems[0].Index;
You can then restore it by using:
lstView.Items[focusedIndex].Selected = true;

Dialogs are modal, which means that they take away all the focus and get the user to only focus on their own UI. You need to not use a dialog, instead you need to use a Fragment or a Activity with wrap_content and a RelativeLayout to keep it small and in the center.

Related

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)

ListView row with EditText, clickability, and context menu -- how to put it together?

I have a ListActivity-based activity that uses a context menu for the items. After adding an EditText to the row of a ListView, the context menu stopped working, and also the item does not react on a click. It seems that it is blocked somehow by the focus of the EditText. I can enter the EditText value, but I cannot get the earlier context menu, and I cannot start another activity via clicking the item.
I have possibly found the related comment that says:
Android doesn't allow to select list items that have focusable elements (buttons). Modify the button's xml attribute to:
android:focusable="false"
It should still be clickable, just won't gain focus...
... so I did the same for the EditText (I am not sure if the button case can be generalized for the EditText). Anyway, the item is clickable again, the context menu appears... However, the EditText part of the text stopped working now. (Actually, I did not implement the reaction to the EditText -- the keyboard simply does not appear.)
Is it possible to have the clickability of the list item and also make the EditText work the expected way?
I don't know if this would help you but it is a post I've found when I was trying to use a ListView with buttons inside.
ListView Tips & Tricks #4: Add Several Clickable Areas
Hope this helps.

Android GridView Focus Issueon children?

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

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.

Android Spinner does not close after using setSelection()

I have a scenario where I want to programmatically change the value of my spinner using setSelection(). This works great, except that if the spinner is open (expanded?) an the time, the spinner does not close. Eg, the menu stays up, despite calling setSelection().
How can I programmatically close the spinner? I have tried performClick() to no avail.
edit: more details:
The reason I'm trying to do this is that my spinner actually uses a compound layout for each selection row. Namely, I have a linearlayout which contains an image, text, and button. The idea was that the button serves as an "edit" button (which opens an activity), while pressing on the image/text select the row (per usual).
The problem came when I added the button. Suddenly, the image & text no longer captured the press event to change the combo. In other words, adding a button to the row destroyed the touch-handling capacity of the entire row. So I tried to manually implement a click handler for the image/text, which subsequently executed a setSelection... which is where I ran into this problem.
You say that after adding the button you lost the click handle on the entire row. Try adding this android:descendantFocusability="blocksDescendants" to your row layout, and see if you can get the clicks to work properly.

Categories

Resources