0

I am using Jsoup to parse the a website, formatting it with Html.fromHtml()and displaying the formatted text in a textview.

Also, I'm using LinkMoveMentmethod.getInstance to make the links in the textview clickable.

When the links are clicked they fired up a chooser to choose browsers.

Please, how can I override this default behaviour.

For example, I would want to pass the clicked url to my own activity and use Jsoup to parse it also.


CODE

TextView pageContent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page_details);
        getWindow().getDecorView().setBackgroundColor(Color.WHITE);
        pageContent = (TextView) findViewById(R.id.dpage_content);
}
....

private void parseHtml(String response) {
        Log.d(TAG, "parsinghtml");
        Document document = Jsoup.parse(response);
        page_content = document.select("div.page-content").first().html();

        Spanned spanned = Html.fromHtml(page_content, new UILImageGetter(pageContent, this), null );
    }
X09
  • 3,827
  • 10
  • 47
  • 92

2 Answers2

2

You need to create your custom class which extends LinkMovementMethod.

public class LinkClickHandler extends LinkMovementMethod{
    private static LinkClickHandler sInstance;

    public static LinkClickHandler getInstance() {
        if (sInstance == null)
            sInstance = new LinkClickHandler();
        return sInstance;
    }

    @Override
    public boolean onTouchEvent(TextView widget, 
            Spannable buffer, MotionEvent event) {
        int action = event.getAction();
        if (action == MotionEvent.ACTION_UP) {
             //Implement your code for handling the click.
        }
         return super.onTouchEvent(widget, buffer, event);
    }
}

To use this, change LinkMovementMethod.getInstance to LinkClickHandler.getInstance

mhmt93t
  • 117
  • 1
  • 10
Rajen Raiyarela
  • 5,526
  • 4
  • 21
  • 41
  • Thanks. It's showing in logcat that a link was clicked, but how do I start an activity? – X09 May 13 '16 at 16:59
  • I saw noticed that the your method is fired where the clicked text is a link or just ordinary text. – X09 May 13 '16 at 21:07
-1

You can use clickable span

     ClickableSpan cs = new ClickableSpan() {  
   @Override
     public void onClick(View v) {  
      Log.d("main", "textview clicked");
        Toast.makeText(Main.this, "textview clicked",Toast.LENGTH_SHORT).show(); 
      } };
     // set the "test " spannable.
     span.setSpan(cs, 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(span);
  tv.setMovementMethod(LinkMovementMethod.getInstance());
Jitendra Prajapati
  • 1,002
  • 1
  • 10
  • 33
  • Android Studio could not resolve `span`. And please not that my textview is already formated this way: `Spanned spanned = Html.fromHtml(post_content, new UILImageGetter(mytv, this), null ); mytv.setText(spanned);` – X09 May 13 '16 at 17:38
  • use following line to create scannable string Spannable span = Spannable.Factory.getInstance().newSpannable("Your Text"); – Jitendra Prajapati May 13 '16 at 17:41
  • Thank you Sir, but like I wrote my textview already has this : `Spanned spanned = Html.fromHtml(post_content, new UILImageGetter(mytv, this), null ); mytv.setText(spanned);` so, can you please advice how I can make your answer compatible with that? – X09 May 13 '16 at 17:52
  • create a spannable object from text which you want to make clickable, please share your code, so that I can help you – Jitendra Prajapati May 13 '16 at 18:02
  • plz check without html.fromHtml() method, directly create scannable from post_content, whether is it working ? – Jitendra Prajapati May 13 '16 at 18:26
  • It's working of course but without the ` html.fromHtml() method` the text is raw html. – X09 May 13 '16 at 18:35