0

I'm currently parsing JSON in my android app (note that I am quite new to this stuff; both android and JSON). Now I got a JSONObject and inside this object a JSONArray with links (strings) inside them. I'd like to put these links inside the array inside a Textview and make them also clickable.

Using Log.w i could retrieve the following output:

<a href="http://xxxx.xx/xxxx/test.xlsx">test.xlsx</a>

The URL seems valid, but everything I try to make them actually be links seem to fail. This is the code that I use to retrieve the links:

ArrayList<String> map; //note it's already filled and contains strings like the example
sb = new StringBuilder("Items in map:\n\n");
    for(int i=0;i<map.size();i++){
        sb.append(Html.fromHtml("&#8226; " + map.get(i) + "<br/>"));
    }

Now I "empty" the stringbuilder into my TextView:

   TextView overView= (TextView)findViewById(R.id.textOverView);
    overView.setText(sb.toString());
    //make links clickable?
    overView.setMovementMethod(LinkMovementMethod.getInstance());

I've also tried to use Linkify, the android:clickable properties, url encoders and many more, but unfortunately nothing seems to work. If I remove the setMoveMent method the links are not even blue (let alone clickable), the same goes for all the other methods.

Any ideas are most welcome!

1 Answer 1

1

MovementMethod makes links clickable and selectable. But you need ClickableSpan instances attached to your text first so the MovementMethod can work. This is done by Linkify and the MovementMethod is just so that it can be selected within a large body of text. I think what you are asking for is this,

android:autoLink="all"
android:linksClickable="true"

This was just have the textview link all links it recognizes with ClickableSpan instances and setup the proper MovementMethod.

Sign up to request clarification or add additional context in comments.

2 Comments

It didn't work. I've added these 2 to my TextView. The (even without these 2 lines added) links are not visible as link, but just as plain text. In other words <a href="google.com">Hey there!</a> shows as Hey there! yet the line is not clickable and not blue either.
so your string build is stripping out all the formatting. Don't use the fromHtml() call on each iteration. Use it on the StringBuilder after you are done appending everything.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.