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
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 using a ListView with a custom Adapter and custom item layout where every item has a description with a switch.
Depending on what the item is, I give the switch different thumb selector-drawables per code in getView().
swtch.setThumbDrawable(getContext().getResources()
.getDrawable(R.drawable.green_switch)); // or red_switch
This works fine, but:
When I scroll the ListView, the custom images are gone, until I touch a switch again, then the custom image of it comes back immediately.
When the state list contains an element of the “disabled” state at first, the switch will have this image instead of the image it should.
It's either a bug or an improperly use of the ListAdapter by me, I think.
A workaround for me would be to include switches with different styles in the layout and show one of them depending on the item type I have and hide the others.
Is this the only possibility?
Sound like you have problems with view recycling. You have to set all the fields in the getView each time.
Please read How ListView's recycling mechanism works. Understanding view recycling is very important when using ListViews.
The title pretty much says it all. I've got a ListView I'm using in my navigation bar/side menu and want to change the colour of the first item. I can't seem to get a hold of the view itself to setTextColor() or whatever the method would be, so how else can I do this?
You want to change the colour of the textview (or whatever view) in your adapter by overriding or implementing getView().
I assume you don't have a custom adapter or you wouldn't be asking this question, so you might want to check out some tutorials on how to use a custom adapter to create a custom appearance for your list items.
I want to create a gallery view, in which I want to select multiple images using an orange rectangular box.
It should look something like the following:
screen look like http://i.imgur.com/qAO0Q.jpg
Issues in your design:
Doing the selection by dragging the thumbs on either sides is going to be an extra headache. First of all, handling scrolling while doing selection. Secondly, handling touch/drag and deciding whether to include an item in the selection or not.
Design change:
Let me suggest a simpler way to extend your selection rectangle :
I assume the selection starts with a long click on any item. The orange selection rectangle appears over this initial item. Then a single tap on any neighboring item should include all items placed in-between the tapped and the initial item including the tapped one. That will make selection handling significantly simpler and less buggy.
Implementation:
Now this design shouldn't be too difficult to implement. Extend the Gallery widget and have members that indicate whether a selection is in progress and what is the range of selection (startIndex and endIndex) .
Override the onDraw method and draw the selection rectangle from startIndex to endIndex items.
Then you can define a custom adapter for your GalleryView, define a row XML layout file with ImageView and CheckBox and inflate this XML layout inside the custom adapter class. FYI, you will define custom adapter class by extending BaseAdapter class.
Check this example: Android custom image gallery with checkbox in grid to select multiple
What you could do:
Give ids all the images in the slide show
Use javascript to set
onclick events for the images Enable borders on the onclick event
Add to img tags
<IMG id=”1” namespace=”clicked” onclick="TriggerEvent(this)"></IMG>
Create script trigger event that changes the namespace of the selected id.
Include css to enable the borders:
.clicked{
border-style:solid;
border-width:3px;}
I have created one list view.. it is having 5 items...
Now I want split the list items...
when user clickon the first listitem or focus on first item then immediately it has to show followed some text views or other things..but it has to show same list..
and agian same when he clickon or focus on the second item that first item has to be close and second item has to act some thing....
I think you need to implement the concept of "Expandable Listview", so that the clicking on one item, it will be expanded with their sub-items.
Refer the android-sdk page of Expandable ListView: http://developer.android.com/reference/android/widget/ExpandableListView.html
For having an example, check this site: http://mylifewithandroid.blogspot.com/2008/05/expandable-lists.html
Pls, check the below image, do you want to perform as same ????
If you want to do the same, it is already given in the "API-Demos" at Views/Expandable Lists/1. Custom Adapter.
Enjoy !!
The problem is that you cannot use the standard ListView in your case, because in the standard ListView, the View of each row has to be one TextView.
In your case, you need it to be at least two TextViews (The standard Text, and the one that's gonna show up onClick/onFocus).
You have to create your custom ListAdapter, and override the getView() function.
Here is a code snippet that shows how to do it properly:
Custom Adapter
In the getView(), you have to inflate the XML file that describes your List Row, and return it.
In your case, I believe your XML file should contain 2 TextViews, one visible and one invisible.
Then, to handle the clicks, you can set a onItemClickListener on your ListView in your Activity class.
The best way may be to have your Activity class implementing onItemClickListener, and to use this onItemClickListener to handle those.
In the onClick() function, you just have to set the Visibility of your hidden TextView to VISIBLE.
You need to build custom rows, and handle showing more text on each row, there is no easy magicall way of doing it, but inflating your own rows, and setting a couple of attributes visibility isnt all that hard either.