How should I go about implementing Java or XML code for highlighting (or showing a checkbox over) a list row or grid item in Multiple-Choice Modal mode in Android for use with a contextual action bar?
I have implemented the contextual action bar and the ListView/GridView and I can select them and run functions on the selected items but there is no visual feedback except for the brief highlighting of the list row/item when you long-click on it, which disappears when it is released.
My first thought was to set the background color of the row/item in the adapter but I cannot seem to get it to work. I have also tried the solution suggested by the accepted answer of this question: Android ListView Multi-Choice don't show highlight after chlicking but it didn't have any effect on the behavior of my ListView and GridView.
I am mostly interested in knowing the standard way of doing this according to material design guidelines and/or the most common way. Thank you in advance for any advice or solution.
EDIT
I tried Redman's answer (in fact something similar to it, as I am using the Contextual Action Mode and a multiple-choice listener) but I didn't get any result. Here's what I did in the listener:
public void onItemCheckedStateChanged(ActionMode actionMode, int i, long id, boolean checked) {
if (checked) {
selectedItems.add(listAdapter.getItem(i));
((CheckBox) listAdapter.getView(i,null,listView).findViewById(R.id.listCheckBox)).setChecked(true);
}
else {
selectedItems.remove(listAdapter.getItem(i));
((CheckBox) listAdapter.getView(i,null,listView).findViewById(R.id.listCheckBox)).setChecked(false);
}
}
It runs without an error but it doesn't do anything to the checkbox so I'm not sure what the problem is. Any help is really appreciated.
"I am mostly interested in knowing the standard way of doing this according to material design guidelines and/or the most common way."
I'm not sure my answer is material design or common but I've done a GridView based off handling of selections in the Google "Photos" app. I have tested it and know that it will both include a highlighted border and a 'checkbox'.
** disclaimers **
I considered using a selector but there seems to be problems with that if your view scrolls (see this). Also, I used a custom checkbox (really just two different drawables), partially because of this. Also, this answer just basically handles a phone in portrait mode. More would need to be added to handle different configurations (screen size, orientation, etc.)
There's a lot here, but like I said, I've tested it. The whole project has been posted to GitHub.
1st, in MainActivity onCreate()
Next, overide getView() in your Adapter (I'm using an ArrayAdapter)
Here's the core of the GridItem class, left out the getters & setters.
That's it for the Java. Now for some xml
grid_item.xml
And this would go in content_main.xml or activity_main.xml, etc.
And now 2 files for your drawable folder.
custom_check_box_unselected.xml
custom_check_box_selected.xml