override LinkMovementMethod textview android

2k views Asked by At

I need to override default LinkMovementMethod. But it's calling the default LinkMovementMethod. Here is the code:

class MovementCheck extends LinkMovementMethod {

    @Override
    public boolean onTouchEvent(TextView widget, 
        Spannable buffer, MotionEvent event ) {
        try {
            Log.d("sg", "check check check");
            return super.onTouchEvent( widget, buffer, event ) ;
        } catch( ActivityNotFoundException ex ) {
            Log.d("sg", "Error");
            return true;
        }
    }

...

Spanned spanned = Html.fromHtml(rules.getObjects().get(0).getDescription());
rulesBody.setText(spanned);
rulesBody.setMovementMethod(MovementCheck.getInstance());

Solution

Figured out the correct implementation.. Could be useful:

class MovementCheck extends LinkMovementMethod {

    private static MovementCheck sInstance;

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

    @Override
    public boolean onTouchEvent(TextView widget, 
        Spannable buffer, MotionEvent event ) {
        try {
            return super.onTouchEvent( widget, buffer, event ) ;
        } catch(ActivityNotFoundException ex) {
            CrashLytics.logException(ex);
            new UtilitySingleton().ShowToast("Link Broken...!!");
            return true;
        }
    }

}

I was missing the setInstance method which was creating a new instance of LinkMovementMethod.

0

There are 0 answers