1

I want to put a web link in my TextView. I am using the TextView in my fragment. I think it does not work on an emulator. I changed the emulator and still got the same problem.

Here are the solutions I've tried:

  1. How to open URL from Fragment TextView?
  2. Android - Hyperlink is not clickable
  3. Why link does not work in the text view?

My TextView inside linearLayout

 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/privacy_policy"
        android:textColor="@color/colorPrimary"
        android:padding="10dp"
        android:textSize="17sp"
        android:textStyle="bold"
        android:autoLink="all"
        android:clickable="true"
        android:focusable="true"
        android:linksClickable="true"
        android:id="@+id/tv_privacy_policy" />

My string value

 <string name="privacy_policy">Privacy Policy<a href="https://csunix.mohawkcollege.ca/~000762465/Privacy%20Policy/ielts_up.html"></a></string>

My onCreateView()

      @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_aboutus, container, false);

        TextView tv_privacy_policy = (TextView)rootView.findViewById(R.id.tv_privacy_policy);

//        Linkify.addLinks(tv_privacy_policy, Linkify.WEB_URLS);
        // Inflate the layout for this fragment

        tv_privacy_policy.setMovementMethod(LinkMovementMethod.getInstance());
        String text = "<a href='http://www.google.com'>Pricacy Policy</a>";
        tv_privacy_policy.setText(Html.fromHtml(text));

        return rootView;
    }
Dan
  • 59,490
  • 13
  • 101
  • 110
Arpit Patel
  • 7,212
  • 5
  • 56
  • 67

2 Answers2

1

maybe this will help you:

    SpannableString string = new SpannableString("Privacy Policy");
    ClickableSpan clickableSpan = new ClickableSpan() {
        @Override
        public void onClick(View textView) {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://csunix.mohawkcollege.ca/~000762465/Privacy%20Policy/ielts_up.html"));
            startActivity(browserIntent);
        }
    };
    string.setSpan(clickableSpan, 0, string.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    TextView textView = (TextView) findViewById(R.id.text);
    textView.setText(string);
    textView.setMovementMethod(LinkMovementMethod.getInstance());
    textView.setHighlightColor(Color.TRANSPARENT);
serg3z
  • 1,852
  • 12
  • 28
0

You can try this following code and It's working fine, I have tested myself.

Don't use android:autoLink tag attribute with it. Because it causes LinkMovementMethod doesn't work.

    String textToShow="<a href=\"https://csunix.mohawkcollege.ca/~000762465/Privacy%20Policy/ielts_up.html\">Privacy Policy</a>";

        Spanned result;

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            result = Html.fromHtml(textToShow,Html.FROM_HTML_MODE_LEGACY);
        } else {
            result = Html.fromHtml(textToShow);
        }
        textView.setText(result);
        textView. setMovementMethod(LinkMovementMethod.getInstance());

If you are about to pass the string on XML then you can do something like this following

<TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/privacy_policy"
            android:textColor="@color/colorPrimary"
            android:padding="10dp"
            android:textSize="17sp"
            android:textStyle="bold"
            android:id="@+id/tv_privacy_policy" />

    TextView tv_privacy_policy= (TextView) findViewById(R.id.tv_privacy_policy);
    tv_privacy_policy.setMovementMethod(LinkMovementMethod.getInstance());

Here is following the list of flags that may help you:

    FROM_HTML_MODE_COMPACT = 63;
    FROM_HTML_MODE_LEGACY = 0;
    FROM_HTML_OPTION_USE_CSS_COLORS = 256;
    FROM_HTML_SEPARATOR_LINE_BREAK_BLOCKQUOTE = 32;
    FROM_HTML_SEPARATOR_LINE_BREAK_DIV = 16;
    FROM_HTML_SEPARATOR_LINE_BREAK_HEADING = 2;
    FROM_HTML_SEPARATOR_LINE_BREAK_LIST = 8;
    FROM_HTML_SEPARATOR_LINE_BREAK_LIST_ITEM = 4;
    FROM_HTML_SEPARATOR_LINE_BREAK_PARAGRAPH = 1;

Good luck. :)

ॐ Rakesh Kumar
  • 1,318
  • 1
  • 14
  • 24