ListView highlight unselected rows - android

Scenario.
Say list view has 5 items. When I click on a particular item, that particular item gets highlighted. When i click on another item, the previous item is unselected and the new item is highlighted.
I am able to do so by holding 2 views. CurrentSelectedItemView and PreviousSelectedItemView. Using these two views I am changing the text color and backgrounds of these two rows. But how do i change the color and background of other rows. I do not want to reset the adapter every time a row is selected.
Say text color is black and background is white. When a row is selected,the colors reverse. For other rows,I need to change the text color to grey. I able to do fr 2 rows only... current and previous.
Is there any simple way of doing this?

You should keep track of last selected index to change the background of previous selected item.

To make changes in listview you need to alteast called adapter.notifydatachanged()

Related

How to set different Selectors (color) to specific rows in a gridview on Android

I have a gridview of icons with the size of 5x6, total 30 itens. Each one of the rows (there are 6 rows) must have a different selector colour (selector is that colour that appears when an item is pressed). I need to change the select colour of the items no the background.
Can u help me with that?
enter image description here
In your adapter, inside getView() method (or onBindViewHolder()),
you can set the background color for a specific row based on current item position (you can use a switch case),
convertView.setBackgroundColor(Color.BLACK);

Issue with ListView selected item color

I've created a list view which shows some data. When the user clicks on a specific item, its color should change.
This works, except that every 10th item changes color, not just the item clicked.
try add in your activity
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
this shloud make your selction single not multiaple selction

Highlighting selected view in Listview Android

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.

How to set background colour for listview selected item

Working fine when we use selector. but this is temporary. if focus change of list item then selected color also change. But i want different. When we click on list view item then that particular item color change. After that we can move another items of list view. When we click another item. then again this particular selected item want to set color, and last colored list item release from color

android change view of selected item in adapterview

I have a listview with a textview as each item. I want to change the textcolor of the selected item. For this use the onItemSelected method to make the changes. Say first I select the 1st row, the first row's textcolor changes. Now when I select the 2nd row, its text color also changes, but I want the 1st row's color to change back to its default color. How do I do that, since in the onItemSelected I only get the refernce of the currently selected item and not the last selected. Is there any way other than holding a reference to the last selected view.
When first item is selected store its position in instance variable of your activity, lets name it currentlySelected. Then combine with this Android: Access child views from a ListView
in order to get the view at position currentlySelected and change it's textcolor.
Instead of doing it in Java, you can let android handles it in the xml file. See Color State List Resource.

Categories

Resources