1

I have spent a lot of time trying to create ListView with clickable item that starts new activity and URL link that opens browser page.

Should be following item:

Some text [URL is here]

By clicking on Some text new activity is starting

By clicking on [URL] web browser is starting

I tried to implement it like in here or here but anyway that doesn't work for me.

Maybe I don't set some properties which are not mentioned there.

Simple ListView:

<ListView
    android:id="@+id/news_list_view_full"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:divider="@android:color/transparent"
    android:dividerHeight="10dp"
    android:padding="@dimen/content_padding_small"
    android:layout_below="@id/news_header"/>

ListView's item:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/news_item_padding"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/news_text_view_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"    
        android:textColorLink="@color/sosbg_orange"
        android:textAppearance="@style/CustomRegularTextStyle"
        android:textColor="@color/sosbg_white" />

</LinearLayout>

And adapter:

public View getView(...View view...) {
    ...
    TextView text = (TextView) view.findViewById(R.id.news_text_view_text);
    text.setText(Html.fromHtml("Hello world <a href='http://google.com'>http://google.com</a>"));
    text.setMovementMethod(LinkMovementMethod.getInstance());
    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // start new activity
        }
    }
}

So currently, link is clickable and opens browser with appropriate page

But listview's item is not clickable. As a result new activity doesn't start.

Could you please advice some steps I can try to achive result I need?

Community
  • 1
  • 1
vetalitet
  • 703
  • 2
  • 10
  • 25
  • I once had a similar problem. Can you try putting `android:descendantFocusability="blocksDescendants"` on your `LinearLayout` and see if that helps? – Toon Borgers Oct 08 '14 at 11:17
  • in listview we use Onitemclickliatner – Naveen Tamrakar Oct 08 '14 at 11:20
  • using `android:descendantFocusability="blocksDescendants"` gives the same result. trying to add additional properties like `android:focusable="false"` - the same – vetalitet Oct 08 '14 at 12:21
  • Tried with listView.setOnItemClickListener(...). The same result. onItemClick is not executed. Really strange. – vetalitet Oct 08 '14 at 12:21

3 Answers3

1

Why you are using view as clickable.?

If you need text to clickable, use text object of TextView and not view.

Replace this as.

view.setOnClickListener(new View.OnClickListener() {

       }

like this..

text.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        // start new activity
    }
}
yuva ツ
  • 3,707
  • 9
  • 50
  • 78
Shadow
  • 6,864
  • 6
  • 44
  • 93
  • This case works but could be situation where text is fairly small and link is not too big. So, item could contain free space (in the end of, right side of item). When clicking on it will give similar result. Thats why I need to set clicklistener on the whole view (not just text) – vetalitet Oct 08 '14 at 12:37
0

Try by using autolink property of textview -

android:autoLink="web"

in your example -

<TextView
    android:id="@+id/news_text_view_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"    
    android:textColorLink="@color/sosbg_orange"
    android:textAppearance="@style/CustomRegularTextStyle"
    android:clickable="true"
    android:autoLink="web"
    android:textColor="@color/sosbg_white" />
yuva ツ
  • 3,707
  • 9
  • 50
  • 78
  • unfortunately, the same behaviour. I tried different combinations but all the time it's not clickable – vetalitet Oct 08 '14 at 12:38
  • In your code your are using view.setonclicklistener.Use text.setonclicklistener – yuva ツ Oct 08 '14 at 12:55
  • And also use holder class for accessing item elements. refer adapter class for that in this link -http://vaishalibhujbal.com/blogspot/?p=26 – yuva ツ Oct 08 '14 at 12:57
0

You are adding your code as view.setOnClickListener(..), Here what is view, I take it as text.setOnClickListener(..), If it is correct then add following line into your TextView

android:clickable="true"

And your layout should be

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/news_item_padding"
android:orientation="vertical" >

<TextView
    android:id="@+id/news_text_view_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"    
    android:textColorLink="@color/sosbg_orange"
    android:textAppearance="@style/CustomRegularTextStyle"
    android:clickable="true"
    android:textColor="@color/sosbg_white" />

</LinearLayout>

I hope this will help you.

Gunaseelan
  • 14,415
  • 11
  • 80
  • 128