So I have a listview that shows random texts. The textview may sometimes be regular text, but can also be URLs and the like. I added the following line to my BaseAdapter's code:
Linkify.addLinks(mHolder.content, Linkify.WEB_URLS);
Where mHolder is an instance of a ViewHolder to speed the listView up, content being the TextView inside that viewHolder, and I am only willing to highlight Web Urls.
First Problem,
Since adding that line of code some of the items that only have regular text like "asdfg" are now no longer highlighted when clicked. And just to clarify, When I mean "highlighted" I only mean the regular highlight that happens when a listView item is clicked, whether or not there was code supplied to handle the onClick event.
I know the issue here is that line of code, since removing it makes all items on the listView highlight when clicked. I tried to describe this via this screenshot
Second Problem,
I added a MultiChoiceModeListener to the listView to handle ActionMode; however, given that the links inside the textViews are now clickable and will launch the browser, they are no longer functioning properly when in ActionMode, as they do not care that we are in ActionMode and still launch the browser instead of following the code inside the onItemCheckedStateChanged method.
For example, when in ActionMode, every item that the user clicks is added to an ArrayList called itemsChecked that is later used via onActionItemClicked. However, when using linkify this is no longer viable, as when the user clicks a ListView item that has a web Url as text, the app will be sent to the background as the browser is called to handle the link.
My questions is What can I do to potentially fix these issues in my code?
I want to have links highlighted and clickable, but not at this cost. Should I use something else other than Linkify to highlight links and make them clickable? Or is that "clickability" always going to interfere with the regular behavior of a ListView item.
FYI, the code that I am running for the ActionMode and ListView are the following: (Keep in mind that the code works appropriately when NOT using Linkify
private MultiChoiceModeListener modeListener = new MultiChoiceModeListener() {
@SuppressLint("NewApi")
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
String text = listItems.get(position);
if (checked) {
itemsChecked.add(text);
}
else {
int index = itemsChecked.indexOf(text);
itemsChecked.remove(index);
}
}
@SuppressLint("NewApi")
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case 0:
mode.finish();
return true;
case R.id.menu_merge:
mergeStrings();
mode.finish();
return true;
case R.id.menu_star:
return false;
default:
return false;
}
}
@SuppressLint("NewApi")
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
inActionMode = true;
itemsChecked = new ArrayList<String>();
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
return true;
}
@SuppressLint("NewApi")
@Override
public void onDestroyActionMode(ActionMode mode) {
inActionMode = false;
}
@SuppressLint("NewApi")
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
};
@SuppressLint("NewApi")
private void initViews () {
listView = (ListView) findViewById(R.id.home_list_view);
if (isOldAPI) {
registerForContextMenu(listView);
}
else {
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(modeListener);
}
mAdapter = new MyAdapter(this, listItems, isOldAPI);
listView.setAdapter(mAdapter);
}
Alright, so after some time lurking in the shadows for an answer, I finally got it thanks to the AOSP MMS code. Credit goes to Rascarlo for posting on github, although i think I may also be available through CM and other projects.
So, the problem was that I was using this line of code inside my adapter:
Linkify.addLinks(mHolder.content, Linkify.WEB_URLS);
When all that can be better address on the XML of the textview, by using the pre-built properties android:autoLink, which must be set to whatever scheme you prefer - web in my case; and android:linksClickable, which MUST be set to FALSE if you want the clicks to highlight the line item. Setting it to true will do as well, context menu via longclick works, but it just doesn't highlight. Please note that by setting it to false, you will have to handle the opening of such links via code, and not via click.
Here is the XML code for my ListView item:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/clip_list_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="4dp"
android:autoLink="web"
android:linksClickable="false"
android:textAppearance="?android:attr/textAppearanceMedium" />
Hope that helps those who could not figure it out.