I have made NavigationDrawer by reading the http://developer.android.com/training/implementing-navigation/nav-drawer.html
there is a mention of android:choiceMode attribute and I want to know its significance. Can anyone explain.
It defines the choice mode means at a time how many items you can select at a time. By default, ListView don’t have any choice made implemented. By setting the choiceMode:
By setting the choiceMode To singleChoice, the list allows up to one item to be in a chosen state.
By setting the choiceMode To multipleChoice, the list allows any number of items to be chosen.
Related
I have a ListView whose choice mode is set to single-choice. I want the selected item, if any, to be highlighted (i.e., to have a background of a different color from the rest of the items).
I had been relying on setting the list selector in order to achieve this. However, I found that, when my ListView is on a tab (i.e., contained within a TabHost), then if I switch to a different tab and back again to the tab containing the ListView, the selected item is no longer highlighted in the UI, even though getCheckedItemPosition() on the ListView still returns the same value as before.
This makes me think that I have misunderstood the purpose of the list selector.
On the other hand, I find that if I change the layout resource used for my list items to one whose background color varies according to whether the android:state_activated flag is set, I can achieve my desired highlighting behavior, and the selection highlight is retained across tab switches.
So I guess my question is, what is the purpose of the list selector if not to indicate the selected item? Is it not for "persistent" highlighting?
android:state_selected attribute in selector xml
View.setSelected()
I'm working on a GridView with setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL). By default, rows are not getting highlighted (with the blue highlight you see on WhatsApp or Gallery apps), so I am wondering, does the Android API already takes care of highlighting the selected items and I am missing something (if so, what am I missing?) or do I have to take care of highlighting selected rows by myself (if so, how to highlight, not just change the background)?
Here's my code:
gridView.setOnItemClickListener(this);
gridView.setEmptyView(view.findViewById(R.id.empty));
gridView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
// multiChoiceModeListener is a subclass of AbsListView.MultiChoiceModeListener
// with no particular code on its abstracts methods.
gridView.setMultiChoiceModeListener(multiChoiceModeListener);
Use this for your ListItem background (XML layout)
android:background="?android:attr/activatedBackgroundIndicator"
It's basically only a selector, you can also build yourself: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/drawable/activated_background.xml
EDIT:
Given your comment, this solves it:
android:foreground="?android:attr/activatedBackgroundIndicator" with a FrameLayout
Also related question: How to set foreground attribute to other non FrameLayout view
I have a listview witch supports multiple selections (check boxes).I also have a filter that when i type text in a edit it sorts the listview.
The problem is when I type something and my list gets filtered if I selected a item and then I search another string using the filter it appers checked a diffrent item.
How can I preserve the checked items regardless any filter that I apply to the ListView ?
You need to maintain the state of CheckBox manually due to the recycling behaviour of ListView. I had already written a blog for the same explaining how you can manage that.
ListView with CheckBox Scrolling Issue
I have a list view with black text items with single choice mode.
I would like the checked item to be red.
Is there any straightforward way to do this, or do I need to listen for item click callbacks, change the colors manually, etc.?
Thanks
There's nothing that will "automatically" change the background of an entire listview item based on a checkbox, so you'll pretty much have to handle the event.
I have a ListView with a custom adapter. I'd like to visually mark the list element the user clicked on by changing it's background permanently (until the user clicks on another element that is). How do I achieve that? I believe that there's a built-in feature inside LisView for this, but I had no luck finding it yet.
By default ListView animates the background color of the list-element clicked, I just want that modified color to "stay". I've already set the ListView's ChoiceMode to single choice, but it doesn't affect it visually.
Have you tried ListView.setSelector?