Android: How to Add HTML Links Inside a Listview

Android: How can I add HTML links inside a ListView?

This is done using the autoLink attribute of a TextView. Took me some time to dig through the documentation so putting it here with an example in case someone else is looking for it:

Let us assume that you are binding your listview to a custom adapter. In that case, the following piece of code goes into your getView call:

Code:

textcontent.setText(Html.fromHtml(item.get_text()));
textcontent.setAutoLinkMask(Linkify.WEB_URLS);

Just put the link inside the text being passed to the setText call and you're done.

XML:

<TextView
android:id="@+id/txtview"
android:autoLink="web"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="put your link here"/>

Hope that helps...

Android ListView - add HTML links when activity starts

For any future viewers, here is a solution to the problem, and a brief description of how I found it.

I tried drschultz solution and noticed no change. He commented that using a textview with a string resource works. I tried this and realised the listview needed to be passed Spanned objects.

I found a solution for creating a Spanned Listview - here - and then added an onItemClickListener, which called setMovementMethod on the Textview object provided.

I realised that the previous solution wasn't working because the adapter's 'getview' method wasn't providing a reference to the listview object, instead it was providing a copy with the same data (I think). Overriding the onItemClick method is a quick solution to get a reference to the list object, which allows you to manipulate it directly and do whatever you like to it.

I've added the code I used to test this solution below. It creates a list of two html references. The first click of a textview calls the onItemClick method, then any more clicks will take you to the web page specified.

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);

String s = new String("<a href=\"http://www.facebook.com\">facebook</a>");
Spanned sp = Html.fromHtml(s);
String s1 = new String("<a href=\"http://www.google.com\">google</a>");
Spanned sp1 = Html.fromHtml(s1);

listValues = new ArrayList<>();
listValues.add(sp);
listValues.add(sp1);
setListAdapter(new SpannedAdapter(this, listValues));

ListView l = getListView();
l.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
try {
LinearLayout linearLayout = (LinearLayout) view;
TextView wantedView = (TextView) linearLayout.findViewById(R.id.rowTextView);
wantedView.setClickable(true);
wantedView.setMovementMethod(LinkMovementMethod.getInstance());
} catch (Exception e) {
e.printStackTrace();
}

}
});

}

Clickable Links inside listview

Got the answer here. I just had to change the constructor call from

CustomAdapter mAdapter = new CustomAdapter( mContext, itemList);

to

CustomAdapter mAdapter = new CustomAdapter( this, itemList);

URL as a link in listview in android

In your XMl wherever you define that particular TextView add the following to that TextView

 android:linksClickable="true"

Also see How do I make links in a TextView clickable? if you want to for example hide the URL and just show some text for the link.

Edit: From what I can tell...

MessageScraper is using ThreeLineAdapter - https://github.com/pradeepb6/TuCanMobile/blob/master/src/com/dalthed/tucan/adapters/ThreeLinesAdapter.java You can see all the TextViews here and then go to the res folder and change them accordingly.

SingleMessageScraper seems to be just using standard ArrayAdapter and Androids Simple_list_Item_1 for the layout. http://developer.android.com/reference/android/R.layout.html#simple_list_item_1 So if you need to you'll need to make your own list_item layout for this if it doesn't make sense to use one that was already created.

I don't think the UI classes need any changing.

Re: Comment - A single message (shown in your picture) is a listview of TextViews. You could also have a listview of custom views if you want more things in each cell, which is what the ThreeLineAdapter is handling.

Android web link in textView inside ListView

I think you can't accomplish what you want in this way.

I think the simplest solution is to separate your links in differents list items. Keep in mind that you could use different TextView with different heights for example

Alternatively you could pass to a custom view approach. If you create a custom view (for example MultiLinkView), then you could add this view to the ListView.
I suggest this solution because this approach allow you to add a powerful logic to the view item.

I can't give you the complete code because it should be too long, but I can put you in the right way.

A custom view is a real Java class that extends some Android view class. So when you instantiate a CustomView you can pass to its constructor all the params you want (references, links, arrays and so on).
Start here

My idea is to find a way to pass all the parameters you need to your custom view and then find a way to represent your data, mapping them to your links.

I think you should abandon html solution in favor to ClickableSpan.
This is a piece of code that I used in a project to make clickable a single part of my string:

        String text = "Hello <b>click me!</b> to go to internet!";

// create Spanned
Spanned spanned = Html.fromHtml(text);
// create SpannableString
SpannableString spanString = new SpannableString(spanned);

// set clickable part
ClickableSpan clickablePart = new ClickableSpan() {
@Override
public void onClick(View textView) {
if (connectionDetector.isConnectedToInternet()) {
// open browser or webview fragment
}
}

@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
ds.setColor(Color.WHITE);
}
};

int startClickMe = spanString.toString().indexOf(text);
spanString.setSpan(clickablePart, startClickMe, text.length() + startClickMe, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

Obviously in the onClick you should find a way to get the right link, but, as I said before, in a custom view you can put as many variables as you want. I'm sure that you can find a solution.

Let me know if it helps

Android: HTML links inside ListView - problem with highlighting

I've managed to solve this issue. Maybe not directly in such a way I wanted but it's enough for me. Instead of adding listener to TextView I add it to entire row. Highlighting is working as I expected. This behaviour is acceptable for my app but is some kind of workaround so I still would like to know whether it can be done better.

Create clickable links in a TextView in a ListView item

Your problem lies here:

String s = fromHtml(xmlItem.Description).toString();

The setText() method accepts anything that implements the CharSequence method. Most people usually pass String instances, which is totally fine since String implements CharSequence. But strings don't contain the information needed to handle links, so you should change that line to this instead:

Spanned s = fromHtml(xmlItem.Description);

Spanned also implements the CharSequence interface, so you can pass s to setText() just like before. And now you will see a highlighted link.

That's not the whole solution, though. You need your TextView to also know how to handle the link spans that fromHtml() creates. You use the setMovementMethod() call for this. You only need to call this once (e.g. when your convertView is null so you're creating a new view), but you should add this line as well:

myHolder.description.setMovementMethod(LinkMovementMethod.getInstance());


Related Topics



Leave a reply



Submit