My list view is a multiple selection list view. i have to show the selected list view items in one color(say green) and the other items in some other color(red). How to achieve this?
It depends what you mean by showing "items in [...] color." The easiest way to change items' colors is to do it in the Adapter's getView() or bindView() method.
In the onItemSelectListner change the item background to whichever you need.
You could call the setBackgroundColor() method in the select event to change the background color.
Related
When you touch some grid or listview items in an android app, he change the background color telling you that you have pressed it.
I'm having a problem in my app. When I create that a listview for instance, and I press it in my phone, he change color, but if I set the OnClick property, the item dont change it color. Whats wrong? The OnClick works as its suppose.
Thanks
Generally with ListView you want to use an OnItemClickListener.
This gives you a chance to perform an action based on the position in the list that was clicked.
If you need access to the view adapter from within onItemClick then you can use AdapterView#getAdapter() on the AdapterView that is supplied to you.
I want to create a single choice listview in which when an item is pressed i should be able to change the background color of the item pressed. I have tried setting the backgroundcolor of the view parameter passed to the onItemClick function but this changes the backgrouncolor of someother item. After the background color change i want to make the Listview non clickable. I have tried setting clickable and focusable to false in the onItemClick handler function but that doesnt help. I also want to be able to populate this ListView with new data and then make it clickable again later. I will have only four listviewitems so is it better not to use the listview at all and may be use four buttons.
Thanks
You need to use a stateful drawable. See the question for more details.
I have a list view. In that list view, I have to grey out and disable some items, and enable the rest list items with seperate color. How to do this?
You should write a custom adapter extending BaseAdapter for your ListView. To disable certain items, you have to override the "boolean isEnabled(int position)" in this adapter, and return false for every position you'd like to be disabled.
As for changing the background color for certain list elements: you could store the background color value in the data structure you're displaying. In your custom adapter's 'getView()' method, you should check this color value for the current element, and return a View with the correct background color set.
Or you could just call 'getChildAt()' on the ListView, getting back the View object for the desired element in the list, and change it's background color. I think I'd rather use the previous solution.
Remember to call 'notifyDataSetChanged()' on your ListView's adapter after you make changes like this.
I have a list and when I do setOnClickListener for an Item I don't have highlight on Items in a List.
That is not expected behavior because I want to give user a way to see which item was selected on touch.
Is there some way to make it highlighted?
tnx.
Probably you can try to set color to your list item xml. Define your list item color depending on the state. Try this out.
I am trying to update color of listview from async task postexecute method.
I am doing ((View) lstChoices.getAdapter().getView(0,null, lstChoices)).setBackgroundColor(Color.RED);
But this is not doing anything, but I tried the same in getView method of my custom adapter then it worked, row.setBackgroundColor(Color.RED);
Any ideas what I am doing wrong?
Thanks
You should not change this that way. All what is related to the row should be handled in adapter. It means that if you changed anything that could influence background color, then all you should do is notifyDatsetChanged() which would trigger list redraw. And b/g color should be then changed by adapter.
A very clearly presented approach to Listview Item Background color changing based on the item's State is at Programmatically select item ListView in Android
Since you are looking for 'postexecute' perhaps changing the Item's State and using that approach can help you get what you are after.