Android listSelector vs setting the list item background - android

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()

Related

android:choiceMode attribute in ListView of Navigation Drawer

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.

ListView selected item highlight

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

android - adding a custom view to listview makes the selector color disappear

I have written code for a custom ListView. It is just a view with three textfields. I have used setBackgroundColor to set the row background to white. However, when I click on a row, the color no longer changes. I am doing this entirely programmatically. Is there a method to get back the Android selector color on clicking a ListView row?
The changing selection colour is a property of the background drawable belonging to the list item. You will have to specify one yourself, using a state list drawable.
More info here:
Drawable Resources: State List
And here:
What is the default drawable for pressing a list item
Annoyingly, the system's default list item background is not accessible from apps, so you'll have to copy the original (or use your own style if you prefer).

How to mark an element visually in ListView the user clicked on?

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?

Android ListView - Multiple <Selector> Tags

Is it possible to have multiple selector tags on a single listview...ie: change the background color of each item of the listview (alternating between two drawables), but still have the selector of the list view active for focus and click events?
Also, is it possible to ensure that the margin stays constant when using selector tags? For some reason, the margins and paddings all reset to zero...
You can extend your own Adapter and override the getView function as shown in this example:
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html
Then simply use setDrawableResource to specify which selector you want based on the row position.

Categories

Resources