Is it possible to change the color of a listview item when clicked, and that it will be that colour until it clicked again? I use adapter to get data from firebase.
Asked
Active
Viewed 343 times
2 Answers
0
Yes it is possible to change the colour of an item of a listview when clicked and it will be that colour until you click it again. Just in your adapter write an item click and based on your condition change colour.
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//Here you have view and position . so use both in a way you want.
}
});
If you need another example let me know. #KeepCoding

Karsh Soni
- 170
- 1
- 12
-
Did you ever try that with a really long list? – Bö macht Blau Nov 29 '18 at 19:15
-
Can you please give me another example? – Alex Nov 29 '18 at 19:18
0
If you just want to change the color once:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
view.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
}
});
You can toggle the change of a list view item with something like this:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
LoadListerListViewObject currentObject = loadListerListViewObjectArrayList.get(position);
//If the object is inactive...
if (!currentObject.getIsActivated()) {
//Set the object as active and change the color to green
loadListerListViewObjectArrayList.set(position, new LoadListerListViewObject(currentObject.getDate(), currentObject.getTagNumber() true));
view.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
//If the object is active...
} else {
//Set the object as active and change the color to grey
loadListerListViewObjectArrayList.set(position, new LoadListerListViewObject(currentObject.getDate(), currentObject.getTagNumber(), false));
view.setBackgroundColor(getResources().getColor(R.color.colorGreyForButton));
}
}
});
This uses a property of the associated list view object to check if the item's been selected or not, then changes colors based on this. I'd imagine you'd want to "un-change" the color, too. Something like this is likely what you'd need.

BBurchfield
- 556
- 4
- 17
-
-
@Alex Not a stupid question! That's a custom object I made to handle some logic in that particular code. I use it (among other things) to reference whether or not the list view item has been tapped. That's what the getIsActivated query is about. You could also do this by creating an associated array with boolean values, and checking it to see if a particular item has been tapped. – BBurchfield Nov 29 '18 at 19:31
-
Thank you for that explanation. But you see, I'm a beginner at this android coding stuff, so can you please explain some more on where to create this array and how? I tried the first one, and it worked. But I want to save the value when I click on it, and that it will be the same when I restart my app. – Alex Nov 29 '18 at 19:40
-
Honestly, @Alex, that's a much bigger monster. You'd simply create an array with the size of your list view. Fill the array with false booleans. Then, when you select a list view item, use the position parameter of OnItemClick to select that item of the array. Change the value to true. You'll need to save the information to internal storage whenever you leave the activity for any reason; and call it at the beginning of the activity. You'll also need to add and delete boolean values from that list when you add and subtract list view items. – BBurchfield Nov 29 '18 at 19:55
-
Ok, sounds complicated! But I have an arrayadapter that uses a textview for displaying each item in my listview. Does that create problems? – Alex Nov 29 '18 at 20:24
-
I'm unsure what you mean by that. Are you sure this adapter uses a TextView and not a String? – BBurchfield Nov 29 '18 at 21:53
-
No, I use String, I have have a Model class with constructor and getters. – Alex Nov 30 '18 at 07:19
-
Ah! Gotcha. No. It shouldn't. Essentially, the array I'm proposing would be fully separate from the rest of your data. You would just effect it when you would effect list view items. For example: OnItemClick { if(array[position].equals(false) { view.setBackgroundColor; array[position] = true } else { view.setBackgroundColor; array[position] = false } – BBurchfield Nov 30 '18 at 13:58
-
-