Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - ListView doesn't receive OnItemClick for textview with clickable links

Tags:

I have a ListView that contains a TextView in each row besides a number of other views. The TextView renders html content which may contain links.

The below code appears in the list adapter. m_textview.setMovementMethod(LinkMovementMethod.getInstance()); m_textview.setText(Html.fromHtml(myhtmlcontent));

This causes the listview to no longer receive on click events though. I decided to put the list onclick code on the view being returned by the adapter instead. This doesnt quite work as desired. Now I can launch another activity when I click anywhere in the row except the textview. I want users to be able to click on the non-links part of the textview and launch another activity.

If I move the onclick to the textview instead of its parent view, it works but now a click on the link fires two events - one for the click on the link and another for the textview (which is not desired).

I have noticed that google+ and peep on android work in the manner I want. I'm not sure how that can be achieved.

like image 555
user1018916 Avatar asked Oct 31 '11 20:10

user1018916


2 Answers

This is actually a BUG. To resolve this you can add android:descendantFocusability="blocksDescendants" you your ListView's rows layout xml. For e.g

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical" >

 <TextView
    android:id="@+id/lblStatusMessage"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:autoLink="web"
    android:focusable="false"
    android:textSize="15sp" />
</LinearLayout>

Source : this and this

Good Luck :)

like image 177
Vishal Vyas Avatar answered Oct 15 '22 09:10

Vishal Vyas


Focusable views inside a ListView item will disable the ability to select ListView items. Applying android:focusable="false" to the TextView will allow OnItemClick to work again. You may also need to apply android:focusableInTouchMode="false" to make the trackball ignore the links because clicking the trackball over a focusable element in a ListView can both click the link and the ListView item.

like image 34
rjschnorenberg Avatar answered Oct 15 '22 10:10

rjschnorenberg