0

I want to set html text in textview like this:

<a href="?id=1>Toast id1</a>hello there <a href="?id=2>Toast id2</a>
hello there <a href="?id=3> Toast id3</a>

I want to show the different toast after clicking on different link with different id(query string).

Manoj
  • 118
  • 12

2 Answers2

3
String htmlText = "<body><h1>Heading Text</h1><p>This tutorial "
            + "explains how to display "
            + "<strong>HTML </strong>text in android text view.&nbsp;</p>"
            + "Example from <a href=\"www.ushatek.com\">"
            + "Ushatek<a>,<a href=\"www.google.com\">"
            + "Google<a>,<a href=\"Male\">"
            + "Male<a>,<a href=\"Female\">"
            + "Female<a></body>";

setTextViewHTML(textView, htmlText);

    protected void setTextViewHTML(TextView text, String html) {
        CharSequence sequence = Html.fromHtml(html);
        SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence);
        URLSpan[] urls = strBuilder.getSpans(0, sequence.length(),
                URLSpan.class);
        for (URLSpan span : urls) {
            makeLinkClickable(strBuilder, span);
        }
        text.setText(strBuilder);
    }

protected void makeLinkClickable(SpannableStringBuilder strBuilder,
                final URLSpan span) {
            int start = strBuilder.getSpanStart(span);
            int end = strBuilder.getSpanEnd(span);
            int flags = strBuilder.getSpanFlags(span);
            ClickableSpan clickable = new ClickableSpan() {
                public void onClick(View view) {
                    Toast.makeText(getApplicationContext(), span.getURL(), Toast.LENGTH_LONG).show();
                }
            };
            strBuilder.setSpan(clickable, start, end, flags);
            strBuilder.removeSpan(span);
        }
Android Boy
  • 4,335
  • 6
  • 30
  • 58
0
TextView textView= (TextView)view.findViewById(R.id.textViewAboutUs);

SpannableString ss = new SpannableString("Your String");

ClickableSpan clickableSpan = new ClickableSpan() {
        @Override
        public void onClick(View textView) {
            Toast.makeText(getActivity().getApplicationContext(), "Working", Toast.LENGTH_SHORT).show();

        }
    };

    ss.setSpan(clickableSpan, starting_position, end_position, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //spanned string, for multiple string define multiple ss.setSpan

    textView.setText(ss);
    textView.setMovementMethod(LinkMovementMethod.getInstance());
Exigente05
  • 2,161
  • 3
  • 22
  • 42