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.
Related
I am unable to get the first button in a gridView to change colour. The button works fine, and it operates as normal when it is clickd but it wont update the colour of the first button. I am using a custom list adapter to generate the gridView. This only seems to happen if I want to change it in real time, if i load the buttons at a later stage the buttons do change colour. I've tried to hard code it for the first button in the list but that doesn't seem to work either.
In Grid view First you have to create a custom adapter for it and then in the Override method "get View()" you will get the specific cell and then change its background color.
For Details click the link : https://developer.android.com/guide/topics/ui/layout/gridview.html
It's easy to make a row in a ListView do something when it is clicked. But add something else in the row--such as a Button or other control--that has an OnClickListener, and suddenly nothing happens when the row is clicked.
Before you close this question as a duplicate, I am perfectly aware that there are a number of StackOverflow questions about that. They generally either recommend setting the Button android:focusable="false" or setting android:descendantFocusability="blocksDescendants" on the ViewGroup containing the row Views.
These work, but one problem: if you're navigating with the keyboard (tabbing or arrow keys), it skips right over the Button.
Is there a way to solve the issue and allow people to "click" either row or Button without having to actually tap the touchscreen?
Setting an OnClickListener on each row (instead of setting an item click listener for the ListView) did not help.
I did find some more info, such as using ListView.setItemsCanFocus(true) Using Android, how can I select rows from a ListView which contains Button controls and using beforeDescendants Focusable EditText inside ListView but can't get both the row and the button to be usable with a keyboard.
BUT it occurred to me to try and test out Gmail with a keyboard. I can't find a way to mark a row as a favorite (select the star). So apparently even they aren't making it fully accessible! Oh well. A lose cause I suppose.
I have couple of issues in the process of making my app accessibility compliant.
I have a spinner with some items in it, after I select one of the item, spinner closes and focus is moving to the top of the page action bar but I want the focus to remain on the last used element i.e, the spinner. Tried:
spinnerId.requestfocus(); or spinnerId.performAccessibilityAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS,null); in the code but nothing worked.
2.How can I set the focus to last accessed element even if onResume(); is called to refresh the data ?
3.Page reverse order doesn't work all the time when I scroll back through the elements.
4.I have a listview with elements populated dynamically, accessibility doesn't scroll the page all the times instead it goes to right fragment( i have left and right fragments in my tablet) skipping the elements below the screen.
As your spinner is already focus, you have to clear this focus, send this event to Accessibility and request it again:
yourView.clearFocus();
yourView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
yourView.requestFocus();
yourView.setFocusableInTouchMode(true);
He took me awhile to find this tricks, but we used it in few different place in our app, and it work well.
I have a listview and a corresponding listview adapter.
The views displayed in the Listview have I written myself and it's a framelayout with one button and one imagebutton. The imagebutton is a red cross that deletes the entry and the regular button selects the entry.
When the user presses the regular button, that item is selected and I want to show this to the user by setting the background of that item to green. The application stores which item the user selected and the next time the listview is rendered, THAT item should be selected and green.
Notice that there should be one and EXACTLY one selected item in my listview at all times.
In the getView(.. method in my adaper, it's very easy to change color of the button when the user clicks it. But the button that was green before the user clicked is impossible for me to reference.
I tried storing a reference the previously selected button, but it never repainted
I tried removing and adding the data item from the list to trigger notifyDataSetChanged but it never repainted
I tried setChoiceMode(ListView.CHOICE_MODE_SINGLE), but that led nowhere because I don't know how to catch the choice in my getView method and paint differently depending on wheter it's selected or not.
All guides I see suggest using notifyDataSetChanged. However, the underlying data is NOT changed in this case and it is not correct (or possible) solution.
So I think that my problem boils down to: HOW can I reference another view in my listview??
(And Yes, I have seen this post: Highlight selected item in ListView on Android . It describes my problem and is answered with "and then change the color of previous selected item's background back to normal" but I still can't refer to the PREVIOUS selected item.
Thanks!
Why don't you use Radio Buttons ? This is exactly the design you need. You can add a "red cross" on each line to delete the line. The selected line is unique and you can iterate on RadioButton.isChecked(); of your RadioGroup to set the background color accordingly.
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.