2

I want to create a link in text view.

My link look like so:

The text to the link I get from array.xml

<item>Icons made by <a href="http://www.freepik.com" title="Freepik">Freepik</a></item>

I already read set movement method

textView.setMovementMethod(LinkMovementMethod.getInstance());

This has no impact

and

android:autoLink="web"

This works if the text is http://www.freepik.com, but not if I want to have a custom text as link.

        viewHolder.textView.setClickable(true);
    viewHolder.textView.setText(text);
    viewHolder.textView.setMovementMethod(LinkMovementMethod.getInstance());

This is a code which I am using to fill textView

I want in the end text looking like so:

Icons made by Freepik

3
  • can't you color the "Freepik" part and then manage click on the entire item? it should be more simple to code and to use for the user Commented Sep 18, 2017 at 17:07
  • The problem is that text contain more then one link. The whole is: >Icons made by <a href="freepik.com" title="Freepik">Freepik</a> from <a href="flaticon.com" title="Flaticon">www.flaticon.com</a> is licensed by <a href="creativecommons.org/licenses/by/3.0" title="Creative Commons BY 3.0" target="_blank">CC 3.0 BY</a>, so I need three links in one textView, but until now I tried just to get first part appearing as I want Commented Sep 18, 2017 at 17:11
  • i posted an answer, but i think the simplest solution is to design better your listview so you can use a single list item for each link Commented Sep 18, 2017 at 17:30

1 Answer 1

1

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

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

2 Comments

I finally decided to change the list view design because I didn't have too much time to work on it and this was faster, but thanks for your answer.
I think you choosen the best approach ;)

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.