Android: How to link a local HTML file from a TextView

welshk91

I know there's a lot of questions on here about reading local HTML files, but I haven't found any that satisfy my needs.

I would like to have a DialogFragment pop up, show a message with a link in it, click that link and have a local HTML file read (preferably from the default web browser).

What I have so far:

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
        alertDialogBuilder.setMessage(Html.fromHtml("I have read and agree to the " + "<a href=\"file:///android_asset/privacy.html\">Terms and Conditions</a>" + " and the " + "<a href=\"http://www.samsung.com/global/business/mobile/info/privacy.html\">Privacy Policy</a>"));
        alertDialogBuilder.setCancelable(false);

        //rest of code...
    }

The second link which points to the online version works like a charm. The first link should point to a local privacy.html file in my assets folder. When clicking on the first link, an error occurs:

    android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///android_asset/privacy.html (has extras) }

Anyone have any tips/advice for this situation? I've tried multiple different HTML files, all with the same result. Am I going to have to use my own WebView for something like this? Seems like there should be a way to ask Android to view a local HTML file from the default browser when clicking on a link...

samsad

Technically it won't work because Link is html tag. It will work in webview. Still you want to do this with textview you have customise your code Try this code :

public class CustomLinkMovementMethod extends LinkMovementMethod
{

private static Context movementContext;

private static CustomLinkMovementMethod linkMovementMethod = new CustomLinkMovementMethod();

public boolean onTouchEvent(android.widget.TextView widget, android.text.Spannable buffer, android.view.MotionEvent event)
{
    int action = event.getAction();

    if (action == MotionEvent.ACTION_UP)
    {
        int x = (int) event.getX();
        int y = (int) event.getY();

        x -= widget.getTotalPaddingLeft();
        y -= widget.getTotalPaddingTop();

        x += widget.getScrollX();
        y += widget.getScrollY();

        Layout layout = widget.getLayout();
        int line = layout.getLineForVertical(y);
        int off = layout.getOffsetForHorizontal(line, x);

        URLSpan[] link = buffer.getSpans(off, off, URLSpan.class);
        if (link.length != 0)
        {
            String url = link[0].getURL();
            if (url.startsWith("https"))
            {
                Log.d("Link", url);
                Toast.makeText(movementContext, "Link was clicked", Toast.LENGTH_LONG).show();
            } else if (url.startsWith("tel"))
            {
                Log.d("Link", url);
                Toast.makeText(movementContext, "Tel was clicked", Toast.LENGTH_LONG).show();
            } else if (url.startsWith("mailto"))
            {
                Log.d("Link", url);
                Toast.makeText(movementContext, "Mail link was clicked", Toast.LENGTH_LONG).show();
            }
            return true;
        }
    }

    return super.onTouchEvent(widget, buffer, event);
}

public static android.text.method.MovementMethod getInstance(Context c)
{
    movementContext = c;
    return linkMovementMethod;
}

This should be called from the textview in the following manner:

textViewObject.setMovementMethod(CustomLinkMovementMethod.getInstance(context));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to link to a local/intranet file within HTML?

How to link JS from an extern file in HTML

How to display a local html file in Android?

How to create link to local index.html from Sphinx documentation?

How to link to Jupter notebook from a local HTML page

how to access local drive file from server link

Why is not working the link from an html in a textview?

How to access a TextView from a non-parent file [Android]

Link to a local html file on RMarkdown with Shiny

Python: how to save a webpage (as html file) from the link in the Email

How to extract a link from an html file using bash

How to add link/href to html from global constants file

Load local HTML from file

How to Java Web Start from a local html file?

How to get server content from local html file?

how to save info from local html file in a text document with python?

How to open (or "download") a local file from a website using html?

How link to any local file with markdown syntax?

How build link to local file in richtextbox

How to link to a local SVG file in the Angular app?

How to link a local file on my personal portfolio?

How to link HTML file in python

how to link js to html file?

Include link to HTML/CSS validator in local HTML file

Link to PHP file from a HTML File

How to read from a local JSON file in android studio and add to an ArrayList?

How i save data from android game to a local json file

How to parse json from a local json file into a ListView in android?

How to exclude HTML from PHP and PHP file still able to link with the HTML file?