stefanhaustein / nativehtml

Renders HTML to native Android components.
Apache License 2.0
45 stars 6 forks source link

Internal hyperlinks on Android #17

Open FriedrichFroebel opened 7 years ago

FriedrichFroebel commented 7 years ago

I tried to add internal hyperlinks (anchors) to my HTML file. Clicking on the hyperlink opens the same HTML file again, but does not jump to the desired anchor location.

Another strange behavior: When opening the activity, the "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy" text is shown at the top. Only after scrolling up a little bit, the hyperlink "Link to test" is visible. This does not seem to occur when clicking on the link.

My HTML file (with some lipsum text to see if clicking has an effect):

<html>
<body>
<p><a href="#test">Link to test</a></p>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
    eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
    voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
    clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit
    amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam
    nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,
    sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.
    Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor
    sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed
    diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam
    erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea
    rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum
    dolor sit amet.<br />
    Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse
    molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero
    eros et accumsan et iusto odio dignissim qui blandit praesent luptatum
    zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum
    dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod
    tincidunt ut laoreet dolore magna aliquam erat volutpat.<br />
    Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper
    suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel
    eum iriure dolor in hendrerit in vulputate velit esse molestie consequat,
    vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et
    iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis
    dolore te feugait nulla facilisi.</p>
<p id="test">A simple test text</p>
</body>
</html>

My Activity code:

import org.kobjects.nativehtml.android.HtmlView;
import java.io.*;
import java.net.*;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ScrollView;

public class MyActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        final ScrollView scrollView = findViewById(R.id.scrollView);
        final HtmlView htmlView = new HtmlView(this);

        try {
            final String filename = "html/index.html";
            final URI uri = new URI("file:///android_asset/" + filename);
            htmlView.addInternalLinkPrefix("file/android_asset/html");
            htmlView.loadHtml(uri);
        } catch (URISyntaxException exception) {
            super.onBackPressed();
            return;
        }

        scrollView.addView(htmlView);
    }
}
stefanhaustein commented 7 years ago

Probably will add something like "gotoLabel" to InternalLinkHandler and let openLink in DefaultRequestHandlder deal with the split. One problem might be to avoid a re-load for local links, so openLink probably needs the current URL, too....First step will probably be to rename the baseUrl to url in Document, reflecting its actual current usage.