Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView onClick event doesn't fire with Linkified email address

I have a straight forward ListView with a ListAdapter and custom onItemClick method for the list. My ListView items are clickable to perform other functions. However, some of my ListView elements contain an email address that should be clickable too.

I found Linkify, and added it to the email address textview per the docs.

After doing so, the ListView item containing such a "clickable" textview is now no longer clickable to perform the other functions.

I have tried adding the OnItemClickListener after I create the entire view, to no avail :(

Any idea's on how to have my ListView items clickable eventhough they contain a Linkified textView?


My deepest appologies for not searching further before asking the question to begin with. I have found the answer to my question, and posted it as an answer.

like image 969
djBo Avatar asked Sep 22 '11 13:09

djBo


2 Answers

Solution:

After a lot of more searching (which I already did, but not far enough apparently), I finally found this page.

Example code: My page (LinearLayout containing the listView) assigns a ListViewAdapter which creates the various ListView items, assigns itself as the OnClick listener and adds the ListView:

list.setAdapter(adapter);
list.setOnItemClickListener(this);
addView(list);

The ListViewAdapter creates each ListView item and assigns it's content, linkifying email addresses when needed:

public View getView(int position, View convertView, ViewGroup parent) {
    View view = layoutInflater.inflate(R.layout.list_item_layout, null);
    TextView textView = (TextView) view.findViewById(R.id.item_text);
    textView.setText(<some email address>);
    Linkify.addLinks(textView, Linkify.EMAIL_ADDRESSES);
}

The solution is easy, using the code on the page I found. I created a LinkTextView class descending from TextView and added the method described on the page. After the Linkify.addLinks call above I added the statement:

textView.setMovementMethod(null);

The ListView item and linkified text are now both clickable once again.

Again, my deepest appologies for not searching further before asking the question to begin with.

like image 189
djBo Avatar answered Oct 16 '22 18:10

djBo


Just add android:descendantFocusability="blocksDescendants" to xml definition of list item.

Source: http://www.thekeyconsultant.com/2013/06/android-quick-tip-linkify-and-listviews.html?m=1

like image 1
palindrom Avatar answered Oct 16 '22 18:10

palindrom