Tuesday, June 4, 2013

on 1 comment

Android Quick Tip: Linkify and ListViews

Those who have worked with Android for a while know that it has some oddities that you learn about with time. I was working on a project recently that utilized the twitter API to pull down a feed and display it in a ListView. When the user clicks the listview item it takes them to the twitter post. However if the text of the tweet has a link in it I wanted to use the TextView attribute android:autoLink="web" to automatically parse the links. 






AutoLink, or "Linkify" as it is often called, worked like a charm. After setting the attribute I was able to click on a link and open it in the browser automatically. The problem was that after setting this attribute I was unable to click  anywhere else on the listview row. 

It took a good bit of searching and documentation browsing but I finally found a solution. In order to have both the link and the listview item clickable you need to set the descendantFocusability attiribute in the parent layout to "blocksDescendants". android:descendantFocusability="blocksDescendants" in the parent layout. If you check the documentation for ReleativeLayout you will find that blocksDescendants is described as:


"This view will block any of its descendants from getting focus, even if they are focusable" . 



The end result is a listview item that has both clickable links and selectable rows:




Powered by Blogger.