So, I have a list that shows random texts. A textual representation can sometimes be plain text, but it can also be URLs, etc. I added the following line to the BaseAdapter code:
Linkify.addLinks(mHolder.content, Linkify.WEB_URLS);
Where mHolder is an instance of ViewHolder to speed up the display of listView, and the content is a TextView inside this viewHolder, and I'm ready to specify only web addresses.
First problem
With the addition of this line of code, some elements that have only plain text, such as "asdfg", are no longer highlighted when pressed. And just for clarification. When I mean “highlighted,” I mean only the regular highlighting that occurs when I click on the listView element, regardless of whether the code was turned on to handle the onClick event.
I know that the problem is that the line of code, because when you delete it, all elements in the listView element are highlighted. I tried to describe it through a screenshot
Second problem
I added a MultiChoiceModeListener to the listView to handle the ActionMode; however, given that links inside text elements are now clickable and launch the browser, they no longer function properly in ActionMode, since they don’t care that we are in ActionMode and are still launching the browser instead of following the code inside onItemCheckedStateChanged .
For example, when in an ActionMode each element that the user clicks is added to an ArrayList named itemsChecked , which is later used through onActionItemClicked . However, when using linkify, this is more unviable, because when a user clicks on a ListView element that has a web url as text, the application will be sent to the background as the browser is called to process the link.
My questions What can I do to fix these problems in my code?
I want the links to be highlighted and accessible by click, but not at this price. Should I use something other than Linkify to highlight links and make them clickable? Or, this “interactivity” will always interfere with the regular behavior of the ListView.
FYI, the code that I run for ActionMode and ListView, is as follows: ( Keep in mind that the code works properly 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); }